0 - Introduction
One of the main benefits of the Esp32 is the included bluetooth connectivity. In this article we will use ‘Bluetooth Serial’ to connect a phone to the Esp and send and receive messages.
1 - Code
For this small project, we will need two header files, ‘Arduino.h’ and ‘BluetoothSerial.h’. Start by creating a variable of the type ‘BluetoothSerial’ and in setup, begin it with the name you want to be visible when pairing, on loop check if there is anything to be read, and if there is, echo it (write what you read):
#include <Arduino.h>
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
void setup()
{
SerialBT.begin("TMV Esp32");
}
void loop()
{
if (SerialBT.available())
SerialBT.write(SerialBT.read());
}
As you can see, the functions in BluetoothSerial are very similar to the ones on Serial, which means you can use libraries that use USB or uart Serial and also make them output to the bluetooth serial.
3 - Pair
To connect to the Esp32, go to your phone’s settings, then bluetooth, scan for new devices and look for the name you set in code.
If you have a Xiaomi phone, like i do, you might need to click on ‘Rarely used devices’ and it will be there.
After clicking on the device, click on Pair and you should be done.
Now, with the help of an app like Serial Bluetooth Terminal, you can select your device, connect to it, and start sending messages.
If everything works correctly, you should see your message being sent in blue and the Esp32 sending you your message back in green.
And that’s all, thanks for reading and stay tuned for more tech insights and tutorials. Until next time, and keep exploring the world of tech!