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:
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
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;
}
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.
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.