BLE to send a large amount of data via notify

techtoys
Posts: 28
Joined: Sat Jun 01, 2019 11:11 am

BLE to send a large amount of data via notify

Postby techtoys » Fri Jul 14, 2023 3:34 am

Hi

hardware: esp32 pico d4
IDE: Microsoft Visual Studio with VMicro plugin + Arduino IDE 2.1.1
esp32-arduino version 2.0.5

I am trying to send a large amount of data from SD card of 100bytes per line with more than 3000 lines to a smartphone by BLE. The MTU size set 128.
Command to use some dummy data for testing purpose is shown below:

Code: Select all

for (int i = 0; i < 1048; i++) {
                        int ts = 62300 + i ; //a simple time stamp index just to know the program is working
                        std::string str_ts = std::to_string(ts);
                        s = str_ts + ",97,0.04,0.13,0,0,0,0,0,-8.91,0.42,-50.19,-9.033,-163.818,1036.621,0,0,0"; //suppose to contain some real data here with size smaller than 128 bytes (MTU)
                        pLogDataCharacteristic->setValue((uint8_t*)s.c_str(), s.length());
                        pLogDataCharacteristic->notify();
                        vTaskDelay(20); //this delay is adjustable to avoid jamming the notification buffer
                    }
Unfortunately, this snippet does not run as expected. It hangs up after something like 100 lines. I suspect it is the notification buffer being full or something.

Questions:
1) Is there any way that I can check if notify() is ready to accept a new data line?
2) If I want to increase the speed of transmission, how to increase the BLE connection interval and what is the command to use?

Any suggestion is welcome.

John

bidrohini
Posts: 202
Joined: Thu Oct 27, 2022 12:55 pm

Re: BLE to send a large amount of data via notify

Postby bidrohini » Fri Jul 14, 2023 12:32 pm

To check if the notify() function is ready to accept new data, you can use the canNotify() function provided by the BLECharacteristic class. Here's an example of how you can use it:

Code: Select all

if (pLogDataCharacteristic->canNotify()) {
  // Perform the notify operation
  pLogDataCharacteristic->setValue((uint8_t*)s.c_str(), s.length());
  pLogDataCharacteristic->notify();
} else {
  // Handle the case when the buffer is full
  // You can either wait until it becomes available or implement some buffering mechanism
}
By checking canNotify() before calling notify(), you can ensure that the buffer is not full before attempting to send data.

To increase the speed of transmission by adjusting the BLE connection interval, you can modify the connection parameters of the BLE connection. However, it's important to note that both the central device (smartphone) and the peripheral device (ESP32) must support the desired connection interval.
In the ESP32 Arduino framework, you can adjust the connection interval using the setConnectionParams() function provided by the BLEDevice class. Here's an example of how you can increase the connection interval:

Code: Select all

#include <BLEDevice.h>

// ...

// Increase connection interval to 30 milliseconds (30 * 1.25 ms)
BLEDevice::setConnectionParams(30, 30, 0, 0);
By calling setConnectionParams() with the desired values (in units of 1.25 ms), you can modify the connection interval. The first parameter represents the minimum connection interval, the second parameter represents the maximum connection interval, and the last two parameters represent the slave latency and connection supervision timeout (both set to 0 in the example).

Keep in mind that modifying the connection interval affects the power consumption and responsiveness of the Bluetooth connection, so you should find a balance that meets your requirements.

techtoys
Posts: 28
Joined: Sat Jun 01, 2019 11:11 am

Re: BLE to send a large amount of data via notify

Postby techtoys » Tue Jul 18, 2023 4:11 am

bidrohini wrote:
Fri Jul 14, 2023 12:32 pm
To check if the notify() function is ready to accept new data, you can use the canNotify() function provided by the BLECharacteristic class.
I found canNotify() is only available from the BLERemoteCharacteristic class but not BLECharacteristic class.

To use notify() I need to change from

Code: Select all

BLECharacteristic* pLogDataCharacteristic
to

Code: Select all

BLERemoteCharacteristic* pLogDataCharacteristic
What is the difference?

Who is online

Users browsing this forum: No registered users and 45 guests