Bluetooth classic communication between to boards

TomasCZ
Posts: 39
Joined: Tue Apr 25, 2023 5:19 am

Bluetooth classic communication between to boards

Postby TomasCZ » Fri Jun 30, 2023 8:05 am

Hello guys, I would like to ask you for some help or guide me to some examples or guides how to make an easy classic BT communication between two ESP32 boards beacuse I am still bit lost in ESP-IDF. Several months ago I wrote the code in Arduino IDE for the server/client communication via BT classic between two boards and I would need to re-write it for IDE.
It just connect the two boards via BT classic and indicate the connection state via LEDs and sends messages using serial monitor.
If the connection is lost it tries to reconnect.

The Sever code:

Code: Select all

#include "BluetoothSerial.h"                                                              // BT: Include the Serial bluetooth library
#define LED_BT_BLUE 2                                                                     // BT: Internal LED (or LED on the pin D2) for the connection indication (connected LED ON / disconnected LED OFF)
#define LED_BT_RED 15                                                                     // BT: LED (LED on the pin D4) for the connection indication (connected LED OFF / disconnected LED ON)
unsigned long previousMillisReconnect;                                                    // BT: Variable used for comparing millis counter for the reconnection timer
bool ledBtState = false;                                                                  // BT: Variable used to chage the indication LED state
bool SlaveConnected;                                                                      // BT: Variable used to store the current connection state (true=connected/false=disconnected)
int recatt = 0;                                                                           // BT: Variable used to count the reconnection attempts

// BT: Bluetooth availabilty check
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

String myName = "ESP32-BT-Master";                                                        // BT: Variable used to store the SERVER(Master) bluetooth device name; just for prinitng
String slaveName = "ESP32-BT-Slave";                                                      // BT: Variable used to store the CLIENT(Slave) bluetooth device name; just for prinitng; just for printing in this case
String MACadd = "03:B4:16:72:36:9C";                                                      // BT: Variable used to store the CLIENT(Slave) bluetooth device Mac address; just for prinitng; just for printing in this case
uint8_t address[6] = { 0x03, 0xB4, 0x16, 0x72, 0x36, 0x9C };                              // BT: Variable used to store the CLIENT(Slave) MAC address used for the connection; Use your own andress in the same format

BluetoothSerial SerialBT;                                                                 // BT: Set the Object SerialBT


// BT: Bt_Status callback function
void Bt_Status(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
  if (event == ESP_SPP_OPEN_EVT) {                                                        // BT: Checks if the SPP connection is open, the event comes// event == Client connected
    Serial.println ("Client Connected");                                                  // BT: Write to the serial monitor
    digitalWrite (LED_BT_BLUE, HIGH);                                                     // BT: Turn ON the BLUE bluetooth indication LED (solid light)
    digitalWrite (LED_BT_RED, LOW);                                                       // BT: Turn OFF the RED bluetooth indication LED
    SlaveConnected = true;                                                                // BT: Set the variable true = CLIENT is connected to the SERVER
    recatt = 0;                                                                           // BT: Reset the reconnect attempts counter
  }
  else if (event == ESP_SPP_CLOSE_EVT) {                                                  // BT: event == Client disconnected
    Serial.println("Client Disconnected");                                                // BT: Write to the serial monitor
    digitalWrite(LED_BT_RED, HIGH);                                                       // BT: Turn ON the RED bluetooth indication LED (solid light)
    digitalWrite(LED_BT_BLUE, LOW);                                                       // BT: Turn OFF the BLUE bluetooth indication LED
    SlaveConnected = false;                                                               // BT: Set the variable false = CLIENT connection lost
  }
}

void setup() {
  pinMode(LED_BT_BLUE, OUTPUT);                                                           // BT: Set up the onboard LED pin as output
  pinMode(LED_BT_RED, OUTPUT);                                                            // BT: Set up the onboard LED pin as output
  digitalWrite(LED_BT_RED, HIGH);                                                         // BT: Turn ON the RED LED = no connection established
  SlaveConnected = false;                                                                 // BT: Set the variable false = CLIENT is not connected
  Serial.begin(115200);                                                                   // Sets the data rate in bits per second (baud) for serial data transmission
  
  // BT: Define the Bt_Status callback
  SerialBT.register_callback(Bt_Status);
  // BT: Starts the bluetooth device with the name stored in the myName variable as SERVER(Master)
  SerialBT.begin(myName, true);
  Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
  SlaveConnect();                                                                         // BT: Calls the bluetooth connection function to cnnect to the CLIENT(Slave)
}

void SlaveConnect() {                                                                     // BT: This function connects/reconnects to the CLIENT(Slave)
  Serial.println("Function BT connection executed");                                      // BT: Write to the serial monitor
  Serial.printf("Connecting to slave BT device named \"%s\" and MAC address \"%s\" is started.\n", slaveName.c_str(), MACadd.c_str());  // BT: Write to the serial monitor
  SerialBT.connect(address);                                                              // BT: Establishing the connection with the CLIENT(Slave) with the Mac address stored in the address variable
}

void loop() {
  
  if (!SlaveConnected) {                                                                  // BT: Condition to evalute if the connection is established/lost 
    if (millis() - previousMillisReconnect >= 10000) {                                    // BT: Check that 10000ms is passed
      previousMillisReconnect = millis();                                                 // BT: Set previousMillisReconnect to current millis
      recatt++;                                                                           // BT: Increase the the reconnection attempts counter +1 
      Serial.print("Trying to reconnect. Attempt No.: ");                                 // BT: Write to the serial monitor
      Serial.println(recatt);                                                             // BT: Write the attempts count to the serial monitor
      Serial.println("Stopping Bluetooth...");                                            // BT: Write to the serial monitor
      SerialBT.end();                                                                     // BT: Close the bluetooth device
      Serial.println("Bluetooth stopped !");                                              // BT: Write to the serial monitor
      Serial.println("Starting Bluetooth...");                                            // BT: Write to the serial monitor
      SerialBT.begin(myName, true);                                                       // BT: Starts the bluetooth device with the name stored in the myName variable as SERVER(Master)
      Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
      SlaveConnect();                                                                     // BT: Calls the bluetooth connection function to cnnect to the CLIENT(Slave)
    }
    
  }
  // BT: Data send/receive via bluetooth
  if (Serial.available()) {                                                               // BT: Checks if there are data from the serial monitor available
    SerialBT.write(Serial.read());                                                        // BT: Sends the data via bluetooth
  }
  if (SerialBT.available()) {                                                             // BT: Checks if there are data from the bluetooth available
    Serial.write(SerialBT.read());                                                        // BT: Write the data to the serial monitor
  }
}
The Client code:

Code: Select all

#include "BluetoothSerial.h"                                                  // BT: Include the Serial bluetooth library 
#define LED_BT 2                                                              // BT: Internal LED (or LED on the pin D2) for the connection indication (connected solid/disconnected blinking)
unsigned long previousMillis;                                                 // BT: Variable used for comparing millis counter (LED blinking)
bool ledBtState = false;                                                      // BT: Variable used to chage the indication LED state
bool MasterConnected;                                                         // BT: Variable used to store the current connection state (true=connected/false=disconnected)
String device_name = "ESP32-BT-Slave";                                        // BT: Variable used to store the CLIENT(slave) bluetooth device name
String MACadd = "03:B4:16:72:36:9C";                                          // BT: Variable used to store the CLIENT(slave) bluetooth Mac address; Use your own MAC address

// BT: Bluetooth availabilty check
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)                           
#error Bluetooth is not enabled! Please run make menuconfig to and enable it
#endif
// BT: Serial Bluetooth availabilty check
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif

BluetoothSerial SerialBT;                                                     // BT: Set the Object SerialBT

// BT: Bt_Status callback function
void Bt_Status (esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {

  if (event == ESP_SPP_SRV_OPEN_EVT) {                                        // BT: Checks if the SPP Server connection is open, the event comes 
    Serial.println ("Client Connected");                                      // BT: Write to the serial monitor
    digitalWrite (LED_BT, HIGH);                                              // BT: Turn ON the bluetooth indication LED (solid light)
    MasterConnected = true;                                                   // BT: Set the variable true = SERVER is connected to the CLIENT
  }
  else if (event == ESP_SPP_CLOSE_EVT ) {                                     // BT: Checks if the SPP connection is closed, the event comes
    Serial.println ("Client Disconnected");                                   // BT: Write to the serial monitor
    digitalWrite (LED_BT, LOW);                                               // BT: Turn OFF the bluetooth indication LED
    MasterConnected = false;                                                  // BT: Set the variable false = SERVER connection lost
  }
}

void setup() {
  pinMode(LED_BT,OUTPUT);                                                     // BT: Set up the onboard LED pin as output
  Serial.begin(115200);                                                       // Sets the data rate in bits per second (baud) for serial data transmission
  
  // BT: Define the Bt_Status callback
  SerialBT.register_callback (Bt_Status);
  // BT: Starts the bluetooth device with the name stored in the device_name variable
  SerialBT.begin(device_name);
  Serial.printf("The device with name \"%s\" and MAC address \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), MACadd.c_str());
}

void loop() {
  // BT: Blinking the bluetooth indication LED if the connection is not established
  if (!MasterConnected) {
    if (millis() - previousMillis >= 500) {                                     // BT: Checks if 500ms is passed
      if (ledBtState == false) {                                                // BT: Checks the leddState and toggles it 
      ledBtState = true;
    }
    else {
      ledBtState = false;
    }
    digitalWrite(LED_BT, ledBtState);                                             // BT: Set LED ON/OFF based onthe ledBtState variable
    previousMillis = millis();                                                    // BT: Set previousMillis to current millis
    }
    
  }
  // BT: Data send/receive via bluetooth
  if (Serial.available()) {                                                       // BT: Checks if there are data from the serial monitor available
    SerialBT.write(Serial.read());                                                // BT: Sends the data via bluetooth
  }
  if (SerialBT.available()) {                                                     // BT: Checks if there are data from the bluetooth available
    Serial.write(SerialBT.read());                                                // BT: Write the data to the serial monitor
  }
}
Some description is here: https://ok1tk.com/two-way-serial-blueto ... 32-boards/

MicroController
Posts: 1708
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Bluetooth classic communication between to boards

Postby MicroController » Fri Jun 30, 2023 10:42 am


TomasCZ
Posts: 39
Joined: Tue Apr 25, 2023 5:19 am

Re: Bluetooth classic communication between to boards

Postby TomasCZ » Sun Jul 02, 2023 11:45 am

Thank you, not easy for a beginner as me. I am trying to make working the the bt_spp_initiator.

When building the project a got en error:
fatal error: esp_bt.h: No such file or directory
I found the solution here and it helped:
https://community.platformio.org/t/blue ... tory/12723


Now I have a new one:

Code: Select all

C:/Users/konec/ESP-IDF/bt_spp_initiator/bt_spp_initiator/main/main.c:22:10: fatal error: console_uart.h: No such file or directory
   22 | #include "console_uart.h"
      |          ^~~~~~~~~~~~~~~~
compilation terminated.
[1113/1119] Linking C static library esp-idf\wifi_provisioning\libwifi_provisioning.a
ninja: build stopped: subcommand failed.

 *  The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command ninja " terminated with exit code: 1. 
But I can't find any solution. It might be similar with as the first issue but I am not sure how ot fix it.

Who is online

Users browsing this forum: No registered users and 113 guests