Controlling lights with ESP 32 BLE

I can only controlled lights with the app via Bluetooth and it uses ble .I figured i can use BLE in esp32 to control them so that i can add it to google home later.
I was able to contole the lights with nRF connect.
I got the uuid of the service and the characteristic and looks like its connecting but its crashing for some reason I am fairly new to Arduino so not sure how to fix this.

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

BLEClient* pClient;
BLEAddress pServerAddress("a4:c1:38:9b:d9:91");  
#define SERVICE_UUID "00010203-0405-0607-0809-0a0b0c0d1910"
#define CHARACTERISTIC_UUID "00010203-0405-0607-0809-0a0b0c0d2b11"

void sendDataToServer() {
  if (!pClient->isConnected()) {
    Serial.println("Client not connected. Cannot send data.");
    return;
  }

  // Command to turn on the led
  uint8_t turnOnCommand[] = {0x33, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33};

  // Get the service
  BLERemoteService* pRemoteService = pClient->getService(BLEUUID(SERVICE_UUID));
  
  if (pRemoteService == nullptr) {
    Serial.println("Failed to find the remote service");
    return;
  }

  // Get the characteristic
  BLERemoteCharacteristic* pCharacteristic = pRemoteService->getCharacteristic(BLEUUID(CHARACTERISTIC_UUID));
  
  if (pCharacteristic == nullptr) {
    Serial.println("Failed to find the characteristic");
    return;
  }

  Serial.println("Sending turn-on command to the server...");
  pCharacteristic->writeValue(turnOnCommand, sizeof(turnOnCommand));
}

void setup() {
  Serial.begin(115200);
  BLEDevice::init("esp");

  // Connect to the BLE server
  Serial.print("Connecting to the server...");
  pClient = BLEDevice::createClient();
  pClient->connect(pServerAddress);
  Serial.println("Connected!");

  // Call the function to send data to the server
  sendDataToServer();
}

void loop() {
  // Your main code here
}

DEBUG
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:1344
load:0x40078000,len:13516
load:0x40080400,len:3604
entry 0x400805f0
E (214) psram: PSRAM ID read error: 0xffffffff
Connecting to the server...lld_pdu_get_tx_flush_nb HCI packet count mismatch (0, 1)
Connected to the server!
Guru Meditation Error: Core 0 panic'ed (Double exception).

Core 0 register dump:
PC : 0x40093ac3 PS : 0x00040f36 A0 : 0x8010d820 A1 : 0x3ffd02b0
A2 : 0x00060d33 A3 : 0x00040023 A4 : 0x3ffc3744 A5 : 0x00000000
A6 : 0x00000000 A7 : 0x3ffb6c20 A8 : 0x00000000 A9 : 0x3ffd0290
A10 : 0x00000008 A11 : 0x00001400 A12 : 0x3ffb6388 A13 : 0x3ffd8399
A14 : 0x00000006 A15 : 0x003fffff SAR : 0x00000002 EXCCAUSE: 0x00000002
EXCVADDR: 0xffffffd0 LBEG : 0x400910dc LEND : 0x400910f8 LCOUNT : 0x00000000

I'd add another serial print after the createClient line to try and narrow down where it crashes. I'll guess that the pClient value isn't valid, but more prints should help figure it out.

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