NimBLE causes crash on ESP32

Hello everyone,. I'm working on a project that ESP32 connects to the internet and to 2 BLE beacons to read temperature and send it to an MQTT broker. To handle the BLE connections i'm using the NimBLE library v1.4.0 in arduino IDE v1.8.19. My ESP connects to the internet, stay some time working fine, then suddenly crashes and sometimes it reconnects to the internet by itself.
I'm getting the following problem:

Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception). 
14:24:01.390 -> Debug exception reason: Stack canary watchpoint triggered (conectarEnviarR) 
14:24:01.390 -> Core  1 register dump:
14:24:01.390 -> PC      : 0x40093b3e  PS      : 0x00060a36  A0      : 0x40084cd6  A1      : 0x3ffe7940  
14:24:01.390 -> A2      : 0x3ffe812c  A3      : 0x3ffe7d40  A4      : 0x3ffe7c2c  A5      : 0x3f41af20  
14:24:01.390 -> A6      : 0x00000002  A7      : 0x3ffe7c64  A8      : 0x2d646900  A9      : 0x7fffffff  
14:24:01.390 -> A10     : 0x00000003  A11     : 0x00060a23  A12     : 0x00060a20  A13     : 0x1ab111c9  
14:24:01.437 -> A14     : 0x00000000  A15     : 0x00000000  SAR     : 0x00000008  EXCCAUSE: 0x00000001  
14:24:01.437 -> EXCVADDR: 0x00000000  LBEG    : 0x4009150d  LEND    : 0x4009151d  LCOUNT  : 0xffffffff  
14:24:01.437 -> 
14:24:01.437 -> 
14:24:01.437 -> Backtrace:0x40093b3b:0x3ffe79400x40084cd3:0x3ffe7a30 0x40183ac5:0x3ffe7d40 0x400ee3d2:0x3ffe7e00 0x400e70fa:0x3ffe7e50 0x400dfbe5:0x3ffe7eb0 0x400dfc9d:0x3ffe7ef0 0x400dfefe:0x3ffe7f30 0x400d361e:0x3ffe7f90 0x400d4139:0x3ffe8020 
14:24:01.437 -> 
14:24:01.437 -> 
14:24:01.437 -> 
14:24:01.437 -> 
14:24:01.437 -> ELF file SHA256: 0000000000000000
14:24:01.437 -> 
14:24:01.437 -> Rebooting...

I read in a forum that this occurs when trying to read/write invalid memory address. So i used ESPExceptionDecoder to help me identify the stack traces, and got this:

PC: 0x40093b3e
EXCVADDR: 0x00000000

Decoding stack results
0x40183ac5: sprintf at /builds/idf/crosstool-NG/.build/HOST-i686-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/sprintf.c line 618
0x400ee3d2: ble_uuid_to_str at C:\Users\MYUSER\Documents\Arduino\libraries\NimBLE-Arduino\src\nimble\nimble\host\src\ble_uuid.c line 117
0x400e70fa: ble_gattc_disc_chrs_by_uuid at C:\Users\MYUSER\Documents\Arduino\libraries\NimBLE-Arduino\src\nimble\nimble\host\src\ble_gattc.c line 2538
0x400dfbe5: NimBLERemoteService::retrieveCharacteristics(NimBLEUUID const*) at C:\Users\MYUSER\Documents\Arduino\libraries\NimBLE-Arduino\src\NimBLERemoteService.cpp line 228
0x400dfc9d: NimBLERemoteService::getCharacteristic(NimBLEUUID const&) at C:\Users\MYUSER\Documents\Arduino\libraries\NimBLE-Arduino\src\NimBLERemoteService.cpp line 107
0x400dfefe: NimBLERemoteService::getCharacteristic(char const*) at c:\users\MYUSER\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\gcc8_4_0-esp-2021r2-patch3\xtensa-esp32-elf\include\c++\8.4.0\ext/new_allocator.h line 79
0x400d361e: connectToServer() at C:\Users\MYUSER\Desktop\Patrick\Projetos\BLE\2_Funcional-BLE/0_Conecta_BLE.ino line 135
0x400d4139: conectarEnviarRTOS(void*) at C:\Users\MYUSER\Desktop\Patrick\Projetos\BLE\2_Funcional-BLE/1_Loop.ino line 25

As ExceptionDecoder indicates, i searched for line 135:

/** Now we can read/write/subscribe the charateristics of the services we are interested in */
        NimBLERemoteService* pSvc = nullptr;
        NimBLERemoteCharacteristic* pChr = nullptr;
        NimBLERemoteDescriptor* pDsc = nullptr;

        
        
        pSvc = pClient->getService("aa20");  
        pChr = pSvc->getCharacteristic("aa21"); /*Line 135*/

I'm really stuck here and i'd be really thankfull if someone could help me :slight_smile:

The code?

sprintf.c might not like the thing.

And the code, could you post it?

Hi. Here is a little snippet of the code:

bool connectToServer() 
{
    NimBLEClient* pClient = nullptr;

    /** Percorre a lista de sensores definida no Array de MACs **/
    for (int i=0; i<sizeof Addr/sizeof Addr[0]; i++)
    {  
      /** Check if we have a client we should reuse first **/
      
      if(NimBLEDevice::getClientListSize()) 
      {
         /** Special case when we already know this device, we send false as the
         *  second argument in connect() to prevent refreshing the service database.
         *  This saves considerable time and power. */
         
        pClient = NimBLEDevice::getClientByPeerAddress(NimBLEAddress(Addr[i][0]));
  
        if(pClient)
        {
            if(!pClient->connect(false)) 
            {
                NimBLEDevice::deleteClient(pClient); 
                Serial.println("     - ReconexĂŁo falhou...");
            }
            else
              Serial.println("     - Reconectado ao Sensor...");
        }
        /** We don't already have a client that knows this device,
         *  we will check for a client that is disconnected that we can use.
         */
        else 
            pClient = NimBLEDevice::getDisconnectedClient();
      }

      /** No client to reuse? Create a new one. */
      if(!pClient) 
      {
      
        if(NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS) 
            Serial.println("     - Número máximo de Sensores conectados! Impossível Conectar"); 
            
        pClient = NimBLEDevice::createClient(NimBLEAddress(Addr[i][0]));
        Serial.println("     - Novo Sensor foi criado...");

        pClient->setClientCallbacks(&clientCB, false);
        /** Set initial connection parameters: These settings are 15ms interval, 0 latency, 120ms timout.
         *  These settings are safe for 3 clients to connect reliably, can go faster if you have less
         *  connections. Timeout should be a multiple of the interval, minimum is 100ms.
         *  Min interval: 12 * 1.25ms = 15, Max interval: 12 * 1.25ms = 15, 0 latency, 51 * 10ms = 510ms timeout
         */
        pClient->setConnectionParams(12,12,0,51);

        /** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */
        pClient->setConnectTimeout(10);    //5    

        if (!pClient->connect(false)) 
        {
            /** Created a client but failed to connect, don't need to keep it as it has no data */
           //NimBLEDevice::deleteClient(pClient);
           Serial.println("     - ConexĂŁo falhou, vai apagar o Sensor...");
        };
      }
     
                       
      /** Se conseguiu conectar, entĂŁo exibe os resultados **/
      if (pClient->isConnected())
      {     
        Serial.println("===========================================");
        Serial.print("1. Conectado em: ");
        Serial.println(Addr[i][1]);
//        Serial.print("2. MAC.........: ");
//        Serial.println(pClient->getPeerAddress().toString().c_str());
//        Serial.print("3. RSSI........: ");
//        Serial.print(pClient->getRssi());
  
        /** Now we can read/write/subscribe the charateristics of the services we are interested in */
        NimBLERemoteService* pSvc = nullptr;
        NimBLERemoteCharacteristic* pChr = nullptr;
        NimBLERemoteDescriptor* pDsc = nullptr;

        
        
        pSvc = pClient->getService("aa20"); 
        pChr = pSvc->getCharacteristic("0xaa21");// Exception Decoder Indicates that here is the problem

        String newValue = "Canopus: " + String(millis()/1000);
        
        pChr->writeValue(newValue.c_str(), newValue.length());    
        std::string value = pChr->readValue();

        float temp = value[1]*100+value[2];
        if(value[1]==1)
          temp=temp*-1;

           float tempDef = temp/100;
           
              if(Addr[i][0] == addr0)
                    {
                       TempRet = tempDef;
                      Serial.printf("ret: ");
                      Serial.println(TempRet);
                    }
                    else if (Addr[i][0] == addr1)
                    {
                         TempIns = tempDef;
                        Serial.printf("ins: ");
                         Serial.println(TempIns); 
                    }//else if

         
      }   // if client connect
    } //Loop For
    
    return true;
}

Thank you for trying help me :slight_smile:

the only thing I can see is the above snippet might be the source of the issue but as code snippets are not really all that helpful, they lack context, I can only guess. Good luck.

1 Like

I think the rest of the code isn't that important, because it's not related to BLE. It's stuff like internet connection, and sending data to a broker.

Couldn't the error be related to a file or something? Because exception decoder says there is an error in the source file of the library. But i really don't know how to fix.

Note: I'm a newbie in programming and ESP32. Maybe i'm wrong.

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