ESP32 Bluetooth classic. Callback. ESP32 CAM

1.- App sends a LED4 on/LED4 off to ESP32 by classic Bluetooth.

p62D_esp32_bluetooth_1.aia (2.7 KB)

App sends "1", LED4 ON, and get a response: "LED4 is ON".
App sends "0", LED4 OFF, and get a response: "LED4 is OFF".
App sends "3", get status LED4.

ESP32, look loop():

#include "BluetoothSerial.h"
#define LED4 4

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  initBT();
  pinMode(LED4, OUTPUT);
}

void initBT(){
  if(!SerialBT.begin("ESP32CAM-CLASSIC-BT")){
    Serial.println("An error occurred initializing Bluetooth");
    ESP.restart();
  }else{
    Serial.println("Bluetooth initialized");
  }

  SerialBT.register_callback(btCallback);
  Serial.println("The device started, now you can pair it with bluetooth");
}

void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){
  if(event == ESP_SPP_SRV_OPEN_EVT){
    Serial.println("Client Connected!");
  }else if(event == ESP_SPP_DATA_IND_EVT){
    Serial.printf("ESP_SPP_DATA_IND_EVT len=%d, handle=%d\n\n", param->data_ind.len, param->data_ind.handle);
        String stringRead = String(*param->data_ind.data);
        int paramInt = stringRead.toInt() - 48;
        Serial.printf("paramInt: %d\n", paramInt);
        if(paramInt == 1){digitalWrite(LED4, HIGH); writeSerialBT("LED 4 is ON.");}
        if(paramInt == 0){digitalWrite(LED4, LOW); writeSerialBT("LED 4 is OFF.");}
        if(paramInt == 3){
          if(digitalRead(LED4)){writeSerialBT("LED 4 now is ON.");}
          else {writeSerialBT("LED 4 now is OFF.");}
        }
    }
}

void writeSerialBT(String respuesta){
  SerialBT.println(respuesta);
  SerialBT.flush();
}

void loop() { }
2 Likes