BLE + freeRTOS + ESP32 + Arduino

Tom451
Posts: 1
Joined: Sat Feb 06, 2021 4:10 am

BLE + freeRTOS + ESP32 + Arduino

Postby Tom451 » Sat Feb 06, 2021 5:45 am

I am trying to use freeRTOS and BLE client/server with Arduino. What I have is basically working but I am confused about the interaction between the tasks and the callbacks used for BLE. There seems to be instances when the callbacks are called it “suspends” or stops the task that created it. Specifically if you have a premature disconnect from the server while in a BLE client task that is updating the server’s remote characteristic.

Replicate with 2 esp32’s. One runs the stock example BLE_server sketch. The other runs the code below which is a combination of the standard BLE_client example and the freeRTOS example. I added an absurdly long delay loop after connecting so you can disconnect the power from the BLE server to mimic a “moving out of range situation while connected”. Unplug when you see the dots on the Serial output. The onDisconnect callback will execute and it looks like the rest of the TaskBLEscanAndWrite loop finishes. But then the task is “frozen”. Only the other two tasks continue in their infinite loops.

I am trying to code a graceful recovery / clean up for a “moving out of range situation” and assumed all tasks would continue after callback was finished per their priority but the BLE task is “stopped”. Is this expected behavior?

At a higher level wondering if there is a best practices example of incorporating BLE (presumably with callbacks) and freeRTOS with Arduino. A lot of BLE examples. Some freeRTOS examples, but I couldn’t find any combined in the Arduino format. Any help appreciated.

Code: Select all

#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif

#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif

#include "BLEDevice.h"

// The remote service we wish to connect to.
static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
// The characteristic of the remote service we are interested in.
static BLEUUID    charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");

static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;

// ********* CALLBACKS AND METHODS  **************

static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
    Serial.print("Notify callback for characteristic ");
    Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
    Serial.print(" of data length ");
    Serial.println(length);
    Serial.print("data: ");
    Serial.println((char*)pData);
}

class MyClientCallback : public BLEClientCallbacks {
  void onConnect(BLEClient* pclient) {
  }

  void onDisconnect(BLEClient* pclient) {
    connected = false;
    Serial.println("onDisconnect");
  }
};

bool connectToServer() {
    Serial.print("Forming a connection to ");
    Serial.println(myDevice->getAddress().toString().c_str());
    
    BLEClient*  pClient  = BLEDevice::createClient();
    Serial.println(" - Created client");

    pClient->setClientCallbacks(new MyClientCallback());

    // Connect to the remove BLE Server.
    pClient->connect(myDevice);  // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
    Serial.println(" - Connected to server");

    // Obtain a reference to the service we are after in the remote BLE server.
    BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
    if (pRemoteService == nullptr) {
      Serial.print("Failed to find our service UUID: ");
      Serial.println(serviceUUID.toString().c_str());
      pClient->disconnect();
      return false;
    }
    Serial.println(" - Found our service");


    // Obtain a reference to the characteristic in the service of the remote BLE server.
    pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
    if (pRemoteCharacteristic == nullptr) {
      Serial.print("Failed to find our characteristic UUID: ");
      Serial.println(charUUID.toString().c_str());
      pClient->disconnect();
      return false;
    }
    Serial.println(" - Found our characteristic");

    // Read the value of the characteristic.
    if(pRemoteCharacteristic->canRead()) {
      std::string value = pRemoteCharacteristic->readValue();
      Serial.print("The characteristic value was: ");
      Serial.println(value.c_str());
    }

    if(pRemoteCharacteristic->canNotify())
      pRemoteCharacteristic->registerForNotify(notifyCallback);

    connected = true;
    return true;
}
/**
 * Scan for BLE servers and find the first one that advertises the service we are looking for.
 */
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 /**
   * Called for each advertising BLE server.
   */
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.print("BLE Advertised Device found: ");
    Serial.println(advertisedDevice.toString().c_str());

    // We have found a device, let us now see if it contains the service we are looking for.
    if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {

      BLEDevice::getScan()->stop();
      myDevice = new BLEAdvertisedDevice(advertisedDevice);
      doConnect = true;
      doScan = true;

    } // Found our server
  } // onResult
}; // MyAdvertisedDeviceCallbacks

//  ******* TASK PROTOTYPES ***********

// define two tasks for Blink & AnalogRead
void TaskBLEscanAndWrite( void *pvParameters );
void TaskCountSlowAndOutput( void *pvParameters );
void TaskCountFastAndOutput( void *pvParameters );

// the setup function runs once when you press reset or power the board
void setup() {
  
  // initialize serial communication at 115200 bits per second:
  Serial.begin(115200);
  
  // Now set up two tasks to run independently.
  xTaskCreatePinnedToCore(
    TaskBLEscanAndWrite
    ,  "BLEscanAndWrite"   // A name just for humans
    ,  4096  // This stack size can be checked & adjusted by reading the Stack Highwater
    ,  NULL
    ,  2  // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    ,  NULL 
    ,  ARDUINO_RUNNING_CORE);

  xTaskCreatePinnedToCore(
    TaskCountSlowAndOutput
    ,  "TaskCountSlowAndOutput"
    ,  1024  // Stack size
    ,  NULL
    ,  1  // Priority
    ,  NULL 
    ,  ARDUINO_RUNNING_CORE);

  xTaskCreatePinnedToCore(
    TaskCountFastAndOutput
    ,  "TaskCountFastAndOutput"
    ,  1024  // Stack size
    ,  NULL
    ,  1  // Priority
    ,  NULL 
    ,  ARDUINO_RUNNING_CORE);

  // Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}

void loop()
{
  // Empty. Things are done in Tasks.
}

/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/

void TaskBLEscanAndWrite(void *pvParameters)  // This is a task.
{
  (void) pvParameters;

  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::init("");

  // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 5 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(5, false);
 // End of setup.

  for (;;) // A Task shall never return or exit.
  {
             Serial.println("TaskBLEscanAndWrite - Loop Start ");
            // If the flag "doConnect" is true then we have scanned for and found the desired
            // BLE Server with which we wish to connect.  Now we connect to it.  Once we are 
            // connected we set the connected flag to be true.
            if (doConnect == true) {
              if (connectToServer()) {
                Serial.println("We are now connected to the BLE Server.");
              } else {
                Serial.println("We have failed to connect to the server; there is nothin more we will do.");
              }
              doConnect = false;
            }
          
            // If we are connected to a peer BLE Server, update the characteristic each time we are reached
            // with the current time since boot.
            if (connected) {
              Serial.println("*******************************************************");
              Serial.println("Doing some calculations before we send characteristic");
              Serial.println("and disconnect BLE server to simulate moving out of range ");
              Serial.println("*******************************************************");
              for (int x=0; x<1000 ; x++)
              {
                vTaskDelay(50);
                Serial.print(".");
              }
              Serial.println();
              String newValue = "Time since boot: " + String(millis()/1000);
              Serial.println("Setting new characteristic value to \"" + newValue + "\"");
              
              // Set the characteristic's value to be the array of bytes that is actually a string.
             pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
            }else if(doScan){
              BLEDevice::getScan()->start(0);  // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
            }
            Serial.println("TaskBLEscanAndWrite - 1 Sec Task Delay");
            vTaskDelay(1000); // Delay a second between loops.
  }
}

void TaskCountSlowAndOutput(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
  
  for (;;)
  {

    Serial.println("SLOW Count!!" );
    vTaskDelay(20000);  
  }
}

void TaskCountFastAndOutput(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
  
  for (;;)
  {

    Serial.println("FAST Count!!" );
    vTaskDelay(5000);  
  }
}

tanmay_adhikari
Posts: 1
Joined: Fri May 28, 2021 6:21 am

Re: BLE + freeRTOS + ESP32 + Arduino

Postby tanmay_adhikari » Fri May 28, 2021 6:33 am

Have you solved the problem?
I have the same problem.

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: BLE + freeRTOS + ESP32 + Arduino

Postby chegewara » Fri May 28, 2021 5:00 pm

I would suggest to change value from "forever" to something like 1000 or so:
https://github.com/espressif/arduino-es ... OS.cpp#L68

Who is online

Users browsing this forum: No registered users and 68 guests