Are there any bugs in the BLE Client sketch?

Hello everyone, I’m Yudi. I’m learning ESP32 programming techniques for BLE Server and BLE Client. I found an interesting example to study, which is the following link :

In the sketch for the BLE Server, it works normally. As explained in the link, the BLE Server sketch is tested using a cell phone by downloading the Android app 'nRF Connect for Mobile.' And there is visible temperature and humidity data being read on the Serial Monitor.

However, when the BLE Client is run, the BLE Server board freezes (hang), as indicated by the absence of temperature and humidity data being sent to the Serial Monitor. The connection between the BLE Server and BLE Client is established briefly, but then both devices freeze. There is no temperature and humidity data received by the ESP32 Client, which should be displayed on the Serial Monitor and also shown on the SSD1306 OLED display module.

Is there a bug in the sketch for the ESP32 Client? Please help me find the bugs, I haven't been able to find them for 2 days.

Thank you very much

Yudi

Please post ALL code in code tags. Also post any error log in code tags.
BTW, just saying 'help me find the bugs' is NOT likely going to attract much attention.

The code for my ESP32 BLE Server and ESP32 BLE Client has been uploaded.
BLE_Suhu_Server.ino (3.5 KB)
BLE_Suhu_Client.ino (4.2 KB)

Error log before the ESP32 BLE Server module is reset :

Error log after the ESP32 BLE Server module is reset :

After resetting, the ESP32 BLE Server and ESP32 BLE Client hang.

Please follow instructions in post #2. Either use the code tag in ^^^ or in IDE as shown in pic


Your error is clearly marked in line 6 (5 is blank).

Did you not see the error message?
Screenshot 2025-02-12 at 18.19.11
Start adding Serial.println of the pointer involved in every alloc/malloc before and after as a start.

Yes, I saw the error message, but I don't know the exact meaning of it and I don't know which line in the ESP32 BLE Client sketch is causing the error.

Please help me so that the ESP32 BLE Client can receive data from the ESP32 BLE Server.

Your BLE Client might be crashing the ESP32 Server due to connection handling or data reception issues. First, check the watchdog timer—if the ESP32 freezes, it could be stuck in a loop. Debug logs can help pinpoint where it stops. BLE operations are memory-intensive, so ensure there’s enough heap available. Also, improper handling of disconnect events or read errors can cause crashes. Try adding small delays between connection attempts and data reads to prevent overload. If both devices freeze, unstable power might be the culprit. Have you tested with a minimal setup or checked debug output? Sharing your client-side code could help pinpoint the issue!

This is the code of ESP32 BLE Client :

type or paste code here#include "BLEDevice.h"
  #include <Wire.h>
  #include <Adafruit_SSD1306.h>
  
  #define temperatureCelsius
  #define BLE_server "ESP32_Server"
  
  static BLEUUID dhtServiceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59");
  #ifdef temperatureCelsius
  static BLEUUID temperatureCharacteristicUUID("cba1d466-344c-4be3-ab3f-189f80dd7518");
  #else
  static BLEUUID temperatureCharacteristicUUID("f78ebbff-c8b7-4107-93de-889a6a06d408");
  #endif
  static BLEUUID humidityCharacteristicUUID("ca73b3ba-39f6-4ab3-91ae-186dc9577d99");
  
  static boolean doConnect = false;
  static boolean connected = false;
  
  static BLEAddress *pServerAddress;
  static BLERemoteCharacteristic* temperatureCharacteristic;
  static BLERemoteCharacteristic* humidityCharacteristic;
  
  const uint8_t notificationOn[] = {0x1, 0x0};
  const uint8_t notificationOff[] = {0x0, 0x0};
  
  #define SCREEN_WIDTH 128 
  #define SCREEN_HEIGHT 64  
  Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  
  bool connectToServer(BLEAddress pAddress) {
  BLEClient* pClient = BLEDevice::createClient();
  
  pClient->connect(pAddress);
  Serial.println(" - Connected successfully to server");
  
  BLERemoteService* pRemoteService = pClient->getService(dhtServiceUUID);
  
  if (pRemoteService == nullptr) {
  Serial.print("Failed to find our service UUID: ");
  Serial.println(dhtServiceUUID.toString().c_str());
  return (false);
  }
  
  temperatureCharacteristic = pRemoteService->getCharacteristic(temperatureCharacteristicUUID);
  humidityCharacteristic = pRemoteService->getCharacteristic(humidityCharacteristicUUID);
  
  if (temperatureCharacteristic == nullptr || humidityCharacteristic ==nullptr) {
  Serial.print("Failed to find our characteristic UUID");
  return false;
  }
  Serial.println(" Characteristics Found!");
  
  temperatureCharacteristic->registerForNotify(temperatureNotifyCallback);
  humidityCharacteristic->registerForNotify(humidityNotifyCallback);
  }
  
  class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
  void onResult(BLEAdvertisedDevice advertisedDevice) {
  if (advertisedDevice.getName() == BLE_server) { 
  advertisedDevice.getScan()->stop();
  pServerAddress = new BLEAddress(advertisedDevice.getAddress());
  
  doConnect = true;
  Serial.println("Device found. Connecting!");
  }
  }
  };
  
  static void temperatureNotifyCallback(BLERemoteCharacteristic*pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify)
  {
  display.setCursor(34,10);
  display.print((char*)pData);
  Serial.print("Temperature: ");
  Serial.print((char*)pData);
  #ifdef temperatureCelsius
  display.print(" *C");
  //Serial.print(" *C");
  #else
  display.print(" *F");
  //Serial.print(" *F");
  #endif
  display.display();
  }
  
  static void humidityNotifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify) {
  display.setCursor(34,20);
  display.print((char*)pData);
  display.print(" %");
  display.display();
  Serial.print(" Humidity: ");
  Serial.print((char*)pData);
  Serial.println(" %");
  }
  
  void setup() 
  {
  Serial.begin(115200);
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(30,0);
  display.print("DHT22 Readings");
  display.display();
  //Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::init("");
  
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new
  MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true);
  pBLEScan->start(30);
  }
  void loop() {
  if (doConnect == true) {
  if (connectToServer(*pServerAddress)) {
  //Serial.println("We are now connected to the BLE Server.");
  temperatureCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)notificationOn, 2, true);
  humidityCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)notificationOn, 2, true);
  connected = true;
  } else {
  Serial.println("Failed to connect to server!");
  }
  doConnect = false;
  }
  delay(1000);
  }

Change the debug level SLOWLY and maybe add verbose until you can figure it out.

Hi @sonofcy ,

there is a thread from March 2024 based on the same error message:

https://forum.arduino.cc/t/assert-failed-heap-caps-free-heap-caps-c-381-heap-null-free-target-pointer-is-outside-heap-areas/1212795/7

The reason was a missing return in a function at that time.

Must not be the same issue here but it might be worth having a look ...

ec2021

[Edit] How about this part in the sketch from post #8 ... :wink:

bool connectToServer(BLEAddress pAddress) {
  BLEClient* pClient = BLEDevice::createClient();

  pClient->connect(pAddress);
  Serial.println(" - Connected successfully to server");

  BLERemoteService* pRemoteService = pClient->getService(dhtServiceUUID);

  if (pRemoteService == nullptr) {
    Serial.print("Failed to find our service UUID: ");
    Serial.println(dhtServiceUUID.toString().c_str());
    return (false);
  }

  temperatureCharacteristic = pRemoteService->getCharacteristic(temperatureCharacteristicUUID);
  humidityCharacteristic = pRemoteService->getCharacteristic(humidityCharacteristicUUID);

  if (temperatureCharacteristic == nullptr || humidityCharacteristic == nullptr) {
    Serial.print("Failed to find our characteristic UUID");
    return false;
  }
  Serial.println(" Characteristics Found!");

  temperatureCharacteristic->registerForNotify(temperatureNotifyCallback);
  humidityCharacteristic->registerForNotify(humidityNotifyCallback);
}

I do not see a

return true;

P.S.: Just for fun: ‘I think we have almost lost the ability to make new mistakes ...’

@yudilubis GOOD CATCH. I don't know for sure if that is the cause, but it is an error.
NOTE to @yudilubis add a return (true) ; as the last line of the connectToServer function. Post the changed code in code tags so we can verify the change is correct.

Sometimes Google can be your friend, you just have to find the right words ... :wink:

YES. That 'right words' WAS 100% art or luck or in my case perseverance, but I think AI may actually be useful in the AI trying various search phrases from our AI request. I have started using much longer search phrases and when I see google beginning to reply with an AI result I know I am likely going to learn something. I think my new approach works at least 75% of the time.

Thank you Sonofcy and ec2021. I tried many things last night based on your suggestions, including commenting out all the Serial.print() function lines. It worked as expected for a while, and I was happy. But when I tried to open each Serial.print() function one by one, the client and server froze again. Strange. I closed the Serial.print() functions again, but it still froze even though it was working fine earlier.

This morning I tried Sonofcy's suggestion and changed the debug level. I tested it with various options, such as 'error', 'warn', and 'verbose', and the result provided the following information:

Even though I commented out all the Serial.print() functions, and commented out lines in the sketch that I thought could be temporarily disabled with '//', the error still appeared.

I don't understand where the mistake is from the project example given by this link:

It’s impossible that they provided a project example with an error.

Thank you, ec2021. I have tried adding the return(true); line as you suggested. The message still appears as if the memory is insufficient.

Dear ec2021, last night I found something odd because the project example I got from the link: ESP32 BLE Server Client Communication using Arduino IDE worked normally when I commented out all the lines in the sketch containing the Serial.print() functions. It worked fine, and I was so happy because I had been puzzled for 10 days wondering why it was freezing.

I was curious, so I uncommented each Serial.print() function one by one. I found that the error happened again on the line where I re-enabled the function. So, I commented it out again with "//". But strangely, the error and freeze still occurred. Finally, I commented out all the Serial.print() function lines again, but the error remained as before.

Why could this be happening? Is there still some instability with the ESP32 WROOM 32D board I’m using, or could there still be bugs in some of the libraries being used?

It is important when you change a sketch; you save it before doing a verify. There have been some recent issues around that phenomenon. I am NOT sure it is true here, but it is a good habit to get into.

Is there also a bug in the Arduino IDE?

Adding the '"return true" is surely ok. In your case it looks like an additional issue with the memory partitioning. That's what the message points to. Did you follow the suggestion that comes with the error message?

I just googled for "text section exceeds' and found this

https://arduino.stackexchange.com/questions/90925/text-section-exceeds-available-space-in-board

There are further hits on Google which you may check for possible solutions if this doesn't work.

As I wrote in my last post: It's quite unlikely to be the first to encounter a certain problem. Therefore a thorough search is always a good idea.

Good luck!
ec2021

P S.: This post explains where the partition scheme can be changed in the Arduino IDE (just where you may look, it finally depends on the board you've chosen)

https://forum.arduino.cc/t/is-there-a-partition-scheme-option-for-esp32/1270751/12?u=ec2021

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.