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.