Bluetooth Classic on ESP32
The ESP32 is an IoT based development board with in-built WIFI and Bluetooth. Like many other boards, it can be programmed using the Arduino IDE by just making a few adjustments. For a more detailed description of the board and its Arduino IDE compatibility, please read this tutorial.
The ESP32 board has two Bluetooth modes, one is the Bluetooth Classic and the other is the Bluetooth Low Energy. In this article, we will be focusing on the Bluetooth Classic and how we can use it to communicate between the board and our smartphone. We will also interface an LED with the board to control it from our smartphone via Bluetooth.
Tools
- ESP32 DEVKIT V1
- LED
- Resistor (optional)
- Smartphone with Bluetooth capability
The circuit diagram of the project will consist as follows
This circuit can easily be built on a breadboard.
Android Bluetooth App
For Bluetooth communication to take place, we need a Bluetooth app which we can use to serially communicate between the board and our phone. I personally use the Serial Bluetooth Terminal app by Kai Morich available on the android app store.
After downloading the app and assuming that the code has been uploaded to the ESP32, first open the Bluetooth connectivity on your phone and see if the ESP32 Bluetooth is showing and pair your device with the board. Afterwards, open the Serial Bluetooth Terminal app and go to the menu option on the top left corner of the interface.
From the drop down menu select the Devices option and from there select the Bluetooth name of your board.
When you select the ESP32 Bluetooth, a message will appear on the serial communication window showing a confirmation message of successful Bluetooth connection
You can directly send the “1” and “0” to control the LED or you can program the keys available by configuring them to a certain value
Code
The whole code for the project is as shown below.
#include <BluetoothSerial.h>
const int LED=4;
char state;
BluetoothSerial SerialBT;
//To check if bluetooth is configured correctly
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32");//To initialise Bluetooth with name "ESP32"
pinMode(LED,OUTPUT);
digitalWrite(LED,LOW);
}
void loop() {
//To check for any data to be sent to the connected device
if(Serial.available())
{
SerialBT.write(Serial.read());
}
//To check for any data being received from the connected device
if(SerialBT.available())
{
state=SerialBT.read();
Serial.write(state);
}
//To check the data received and set the LED state accordingly
if(state=='1')
{
digitalWrite(LED,HIGH);
}
else if(state=='0')
{
digitalWrite(LED,LOW);
}
}
Now for the bit by bit explanation
We include the Bluetooth library
#include <BluetoothSerial.h>
This part is to check if the Bluetooth has been successfully configured
//To check if bluetooth is configured correctly
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
In the void setup() part, we initialise the bluetooth and set up “ESP32” as the bluetooth name
SerialBT.begin("ESP32")
In the void loop() section, we include the code to send data from the board to the connected device. The Serial.available() checks for any data present in the serial port. Serial.read() reads the serial data while SerialBT.write() sends it to the device.
if(Serial.available())
{
SerialBT.write(Serial.read());
}
Similarly, the SerialBT.available() checks if there is data being sent through bluetooth. SerialBT.read() reads the data while Serial.write() writes it to the serial monitor.
if(SerialBT.available())
{
state=SerialBT.read();
Serial.write(state);
}
Lastly, we check the data being sent from the device to see if it satisfies the condition for the LED to be ON or OFF.
if(state=='1')
{
digitalWrite(LED,HIGH);
}
else if(state=='0')
{
digitalWrite(LED,LOW);
}
Results
After uploading the code to the board, press the enable button on the ESP32 and connect it with your smartphone. Open the serial monitor at a baud rate of 115200 and send data through the serial bluetooth app. The data sent will appear on the serial monitor as shown.
Sending a “1” as a string will turn the LED ON
While sending a “0” will turn it OFF
P.S, for any paid content writing and/or article writing tasks, and for any free guidance about the ESP32 board, do reach out to me at khantalha7367@gmail.com