Greetings!
I'm getting this error and I don't know why. I have added some debugging statements but didn't work.
Can someone look into this.
Device found. Connecting!
- Connected to server
- Found our characteristics
We are now connected to the BLE Server.
Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x40090eb6 PS : 0x00060930 A0 : 0x800d7eeb A1 : 0x3ffcfa60
A2 : 0x3ffe0800 A3 : 0x00000000 A4 : 0x00000001 A5 : 0x0000000c
A6 : 0xb33fffff A7 : 0xb33fffff A8 : 0x00000000 A9 : 0x00000000
A10 : 0x00000001 A11 : 0x00001800 A12 : 0x00000000 A13 : 0x3ffb657c
A14 : 0x00001004 A15 : 0x00000001 SAR : 0x00000014 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000 LBEG : 0x40090f10 LEND : 0x40090f1b LCOUNT : 0x00000000
Backtrace: 0x40090eb3:0x3ffcfa60 0x400d7ee8:0x3ffcfa80 0x400d800a:0x3ffcfaa0 0x400d4b51:0x3ffcfac0 0x400d5363:0x3ffcfb10 0x400d3ab5:0x3ffcfb30 0x400d4325:0x3ffcfbc0 0x400ea0b6:0x3ffcfc10 0x400ea76a:0x3ffcfc30 0x40115dd9:0x3ffcfc80 0x40118b79:0x3ffcfca0 0x40095d76:0x3ffcfcd0
ELF file SHA256: 266b32c50dffd1b9
Rebooting...
ets Jul 29 2019 12:21:46
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1448
load:0x40078000,len:14844
ho 0 tail 12 room 4
load:0x40080400,len:4
load:0x40080404,len:3356
entry 0x4008059c
Client Device started working.
Here is the code:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
#include <BLEClient.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
#define bleServerName "ESP32_BLE_JSON_Server"
// Flags stating if should begin connecting and if the connection is up
static bool doConnect = false;
static bool connected = false;
BLEAddress *pServerAddress = nullptr; // Declare pServerAddress
BLEClient* pClient;
BLERemoteCharacteristic* pRemoteCharacteristic;
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) override {
Serial.println("Client connected");
}
void onDisconnect(BLEClient* pclient) override {
connected = false;
Serial.println("Client disconnected");
}
};
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) override {
Serial.print("Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
if (advertisedDevice.getName() == bleServerName) { // Check if the name of the advertiser matches
advertisedDevice.getScan()->stop(); // Scan can be stopped, we found what we are looking for
pServerAddress = new BLEAddress(advertisedDevice.getAddress()); // Address of advertiser is the one we need
doConnect = true; // Set indicator, stating that we are ready to connect
Serial.println("Device found. Connecting!");
}
}
};
// Connect to the BLE Server that has the name, Service, and Characteristics
bool connectToServer(BLEAddress pAddress) {
Serial.println("Attempting to connect to server...");
BLEClient* pClient = BLEDevice::createClient();
pClient->setClientCallbacks(new MyClientCallback());
// Connect to the remote BLE Server.
pClient->connect(pAddress);
Serial.println(" - Connected to server");
// Obtain a reference to the service we are after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(SERVICE_UUID);
if (pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
Serial.println(SERVICE_UUID); // Print directly
pClient->disconnect();
return false;
}
// Obtain a reference to the characteristic in the service of the remote BLE server.
pRemoteCharacteristic = pRemoteService->getCharacteristic(CHARACTERISTIC_UUID);
if (pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID");
pClient->disconnect();
return false;
}
Serial.println(" - Found our characteristics");
connected = true;
return true;
}
void setup() {
Serial.begin(115200);
BLEDevice::init("ESP32_BLE_JSON_Client");
Serial.println("Client Device started working.");
pClient = BLEDevice::createClient();
pClient->setClientCallbacks(new MyClientCallback());
BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->start(30);
}
void loop() {
if (doConnect) {
if (connectToServer(*pServerAddress)) {
Serial.println("We are now connected to the BLE Server.");
} else {
Serial.println("We have failed to connect to the server; there is nothing more we will do.");
}
doConnect = false;
}
if (connected) {
if (pRemoteCharacteristic != nullptr && pRemoteCharacteristic->canRead()) {
std::string value = pRemoteCharacteristic->readValue(); // Use std::string
Serial.print("Received value: ");
Serial.println(value.c_str()); // Convert std::string to C string for printing
} else {
Serial.println("Characteristic is not readable or is null.");
}
delay(1000); // Add a delay to avoid flooding the Serial Monitor
}
}