1

I want to search for bt devices in each loop(), store them and query them one by one. This works fine so far. However, when I try to reset the corresponding variables, I get the following error: "Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled."

This is to ensure that new devices are found rather than iterating over old (no longer existing) devices.

// BT #include "BluetoothSerial.h" BluetoothSerial SerialBT; #define BT_DISCOVER_TIME 10000 //BT_DEVICES BTScanResults* pScanResults; String** deviceNames; int numDevices; void setup() { Serial.begin(9600); //BT SerialBT.begin(DEVICE_NAME, true); // device is master } void loop() { btDiscover(); requestData(); } void btDiscover() { pScanResults = SerialBT.discover(BT_DISCOVER_TIME); if (pScanResults != nullptr && pScanResults->getCount() > 0) { for (int i = 0; i < pScanResults->getCount(); i++) { BTAdvertisedDevice* pDevice = pScanResults->getDevice(i); if (pDevice != nullptr) { String deviceName = pDevice->getName().c_str(); if (deviceName.startsWith(SLAVE_NAME_PATTERN)) { addDeviceName(deviceName.c_str()); } } } } SerialBT.discoverClear(); } bool deviceNameExists(const String& name) { for (int i = 0; i < numDevices; i++) { if (deviceNames[i]->equals(name)) { return true; } } return false; } void addDeviceName(const String& name) { if (!deviceNameExists(name)) { deviceNames = (String**)realloc(deviceNames, (numDevices + 1) * sizeof(String*)); deviceNames[numDevices] = new String(name); numDevices++; } } void requestData() { for (int i = 0; i < numDevices; i++) { SerialBT.connect(*deviceNames[i]); SerialBT.println("REQUEST"); delay(100); String message = SerialBT.readStringUntil('\n'); StaticJsonDocument<200> jsonDocument; DeserializationError error = deserializeJson(jsonDocument, message); if (error == DeserializationError::Ok) { serializeJson(jsonDocument, Serial); Serial.println(); } else { // Fehler beim Parsen der JSON-Nachricht Serial.println("ERROR"); } SerialBT.disconnect(); } } 

i tried something similar to this:

delete pScanResults; pScanResults = nullptr; } delete[] deviceNames; deviceNames = nullptr; numDevices = 0; 

how can i realize this? i tried vectors instead of String**, but that triggered the problem instantaneously (without the ressetting "function")

I have also tried the library "ListLib.h". When I use the Clear() function of this library, I get no messages after the first 2 lines. (hangs up?)

mh ok... if i use btDiscover() only (without arrays and so), it works as expected. but if i add my requestData() inside of btDiscover(), i get only once message from each client... idk

ok maybe discover and connect/disconnect dont work together?

mh when i use addDeviceName(deviceName.c_str()) with a variable, i get only one time the messages of clients. if replace the variable with a String, then it works as expected.

3
  • for now, i have moved the discover part to setup() and every 30minutes the esp is restarted... it's not the best approach, but it works... conclusion: discover and connect do not work together Commented Jul 24, 2023 at 7:38
  • Welcome to SE/Arduino! Please take the tour to learn how this site works, and read "How to Ask". -- This sounds like a memory overflow problem. What exception is unhandled? Doesn't the ESP tell you more? Are you sure your memory management is correct? (I do not look through code without knowing the exact error message.) -- Please edit your question if you have new information, do not post comments. This is not a forum. Commented Jul 25, 2023 at 7:54
  • Hi @the busybee, sorry for that misguided post...I will do that again. I was a bit stumped and desperate. My troubleshooting started with the memory overflow (I "fixed" it with delay(XXX)) until I finally came to the conclusion that the discover() function is not usable after a connect(). Because of that i moved the discover()-function to setup() and for the moment I restart the ESP32 regularly. Not nice but it works. The memory overload was not the real problem. Commented Jul 25, 2023 at 18:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.