0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino Tutorial: 39. Serial over Bluetooth - Client disconnection event

DFRobot Feb 20 2019 2060

Introduction

In this tutorial we will check how to detect the client disconnection event when using the Bluetooth Serial library of the Arduino core, running on the ESP32.

In this previous tutorial we have already covered how to configure a callback function to handle the Bluetooth Serial Port Profile (SPP) events and, in particular, to catch the client connected event. Now, we will enhance the code covered on that tutorial to also handle the client disconnected event.

The tests shown here were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The code

The first thing we will do is including the BluetoothSerial.h library, so we have access to all the Bluetooth features we will need.

#include "BluetoothSerial.h"

After that, we will need an object of class BluetoothSerial, which allows to initialize the ESP32 Bluetooth interface and establish the Serial over Bluetooth communication.

BluetoothSerial SerialBT;

Moving on to the setup function, we will start by opening a regular serial connection, to print the results of our program. We are not going to change any actual data over the Bluetooth connection, so all the prints will be done on the wired serial connection.

You should keep this in mind when testing the code, and leave the wired serial connection opened during the tests.

Serial.begin(115200);

Then we will register our SPP event callback function by calling the register_callback method of the BluetoothSerial object. This method receives as input the event callback function, which we will define below. We will call this function callback.

SerialBT.register_callback(callback);

To finalize the setup function, we will call the begin method on the BluetoothSerial object, to initialize the Bluetooth interface. As input, we can pass a string with the name to be assigned to the ESP32 as Bluetooth device, which will be seen by other devices when performing the discovery procedure.

if(!SerialBT.begin("ESP32")){
    Serial.println("An error occurred initializing Bluetooth");
}else{
    Serial.println("Bluetooth initialized");
}

The full setup function can be seen below.

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

  SerialBT.register_callback(callback);

  if(!SerialBT.begin("ESP32")){
    Serial.println("An error occurred initializing Bluetooth");
  }else{
    Serial.println("Bluetooth initialized");
  }
}

Moving on to the callback function, recall from the previous tutorial that it needs to follow a predefined signature: it must return void, receive as first parameter a variable of type esp_spp_cb_event_t and as second a variable of type esp_spp_cb_param_t *.

void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){
// Callback function implementation
}

In our case, we are only going to make use of the first argument of the callback function, which corresponds to an enumerated value representing the SPP event type.

In our case, since we want to detect the client disconnection, we need to look for the ESP_SPP_CLOSE_EVT value. So, in case our callback function is called and this value is passed as first argument, we know that a client disconnected and thus we will print a message to the serial port indicating that.

if(event == ESP_SPP_CLOSE_EVT ){
    Serial.println("Client disconnected");
}

The full callback function can be seen below. Additionally to the client disconnection event, we will also handle the client connection event and print a message when that happens, to help debugging. Naturally, it is expected that first we get a client connection and only then a client disconnection.

void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){
  if(event == ESP_SPP_SRV_OPEN_EVT){
    Serial.println("Client Connected");
  }

  if(event == ESP_SPP_CLOSE_EVT ){
    Serial.println("Client disconnected");
  }
}

The final code can be seen below.

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){
  if(event == ESP_SPP_SRV_OPEN_EVT){
    Serial.println("Client Connected");
  }

  if(event == ESP_SPP_CLOSE_EVT ){
    Serial.println("Client disconnected");
  }
}

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

  SerialBT.register_callback(callback);

  if(!SerialBT.begin("ESP32")){
    Serial.println("An error occurred initializing Bluetooth");
  }else{
    Serial.println("Bluetooth initialized");
  }
}

void loop() {}

Testing the code

To test the code, simply compile it and upload it to your ESP32 using the Arduino IDE. After the procedure is finished, open the serial monitor and wait for the initialization of the Bluetooth interface to finish.

When the procedure finishes, your ESP32 should be discoverable to other Bluetooth enabled devices. So, pair it with a Bluetooth enabled computer.

After the paring procedure is complete, you should have a new Serial Port available in your computer. If you are on Windows, you should be able to go to the device manager and check which serial ports are operating over Bluetooth, which should be the case of the ESP32 port, as shown here.

After identifying which port corresponds to the ESP32, simply establish a serial connection to it using a software of your choice. You can open another instance of the Arduino IDE and use the serial monitor or use a software such as Putty.

Note that these programs are not aware that the connection will be operating over Bluetooth, so you should be able to establish the connection with the ESP32 like you would do for a regular wired serial connection.

After establishing the connection, simply disconnect it, to generate the SPP client disconnection event on the ESP32. Then, go back to the serial monitor where you have opened the wired connection with the ESP32. You should have an output similar to figure 1, which shows the message indicating the client disconnection event, as expected.

Printing a message on the Bluetooth client disconnection event, using the ESP32 and the Arduino core

Figure 1 – Client disconnection event.