ESP32 Bluetooth classic. Callback. ESP32 CAM

ESP32 supports BLE and classic Bluetooth.

In this topic we can find several examples of BLE: BLE ESP32. Bluetooth. Send. Receive. Arduino IDE.

Here ESP32 and classic Bluetooth: Wemos D1 R32 ESP32. Bluetooth. App Inventor. Enciende/apaga LED12/LED13. Obtiene el estado de los pulsadores. Envía un mensaje a LCD.

Now we are going to see several examples of classic Bluetooth, but using a Callback.

In these examples I will use an ESP32 CAM card, but we can use any other card with ESP32.

I got the idea for these examples from: ESP32CAM | BLUETOOTH CLASSIC | FLUTTER - TWO-WAY Serial BT Communication (ft. uint8_t* buffer)🖼️ - YouTube

Bluetooth library used in these examples:
https://github.com/espressif/arduino-esp32/tree/master/libraries/BluetoothSerial

1 Like

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

2.- App sends a message to ESP32.

p62D_esp32_bluetooth_2.aia (2.9 KB)

  • If message contains "Set LED4 ON" then LED4 ON. Return response.
  • If message contains "Set LED4 OFF" then LED4 OFF. Return response.
  • If message contains "How are you?" then get status LED4.

#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 received = bluetoothReadLine();
        Serial.println(received);
        if(received.indexOf("Set LED4 ON") != -1){digitalWrite(LED4, HIGH); writeSerialBT("LED 4 is ON.");}
        else if(received.indexOf("Set LED4 OFF") != -1){digitalWrite(LED4, LOW); writeSerialBT("LED 4 is OFF.");}
        else if(received.indexOf("How are you?") != -1){
          if(digitalRead(LED4)){writeSerialBT("LED 4 now is ON.");}
          else {writeSerialBT("LED 4 now is OFF.");}
        }
        else {writeSerialBT("Message received: " + received);}
    }
}

String bluetoothReadLine(){
    String text_received = "";
    while(SerialBT.available())
    {
        byte r = SerialBT.read();
        if(r!=13 && r!=10 && char(r)!='\0')
            text_received = text_received + char(r);
    }
    return text_received;      
}


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


void loop() { }
1 Like

3.- Application sends three numbers, receives double of each.

p62D_esp32_bluetooth_3.aia (3.4 KB)

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  initBT();
}

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){
        while(SerialBT.available())
             {
              String a =  SerialBT.readStringUntil(',');
              String b =  SerialBT.readStringUntil(',');
              String c =  SerialBT.readStringUntil('\n');
              String retorno = (String)(2 * a.toInt()) +","+ (String)(2 * b.toInt()) +","+ (String)(2 * c.toInt());
              SerialBT.println(retorno);
            } 
    }
}

void loop() { }
1 Like

what is the name of the Arduino library of "bluetoothserial.h"?

4.- Esp32 sends by classic Bluetooth, two random numbers to the app, they are displayed in a graph.

p62D_esp32_bluetooth_5.aia (16.5 KB)

ESP32 Code.

// Juan A. Villalpando.
// http://kio4.com/arduino/180_Wemos_Bluetooth_LED_Pulsadores.htm

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth no activado! Activa la conexion Bluetooth.
#endif

BluetoothSerial SerialBT;

int alea_1;
int alea_2;
unsigned long tiempo_actual_1 = 0;
unsigned long tiempo_actual_2 = 0;

void setup() {
  Serial.begin(9600);
  SerialBT.begin("ESP32test");
}
 
void loop() {
 if((millis()-tiempo_actual_1)>= 200){
    int alea_1 = random(0,100);
    int alea_2 = random(0,1000);
    tiempo_actual_1 = millis();
    SerialBT.println(String(alea_1) + "," + String(alea_2));

How should I do?

5.- App gets the text of a QR using BarcodeScanner. Send that text to ESP32.

p62D_esp32_bluetooth_6.aia (2.8 KB)

  • BluetoohClassic. ESP32.
  • BarcodeScanner gets the text of a QR.
  • Send that text to ESP32 via Bluetooth.
  • ESP32 returns the same text to the app.


#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  initBT();
}

void initBT(){
  if(!SerialBT.begin("ESP32CAM-CLASSIC-BT2")){
    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){
        while(SerialBT.available())
             {
                String received = SerialBT.readStringUntil('\n');
                Serial.println(received);
                SerialBT.println(received); // Return
            } 
    }
}

void loop() { }

Hi Juan - there is more than one BluetoothSerial.h, is this the one you are using?

Correct, I am using that library.
https://github.com/espressif/arduino-esp32/tree/master/libraries/BluetoothSerial

Hi Juan,
with this example I was able to adapt it to my project.

Tks!!!!