Bluetooth on Your ESP32

Andreana Hartadi Suliman
7 min readMar 29, 2022

Hello guys, back again with me on the II2260 Embedded System course assignment. Well, as usual, new week, new project. In this project, I will use the ESP-32 to be able to be connected via Bluetooth. What will happen next? Let's see!

Bluetooth Terminal Application

Bluetooth is a communication media equipment that can be used to connect a communication device with other communication devices, Bluetooth is generally used in cellphones, computers or PCs, tablets, and others. In this first part, we will make the ESP-32 and our cellphone interact.

Prepare the tools first.

  1. ESP-32

2. Micro USB Cable

3. Breadboard

Of course, a laptop or PC with Arduino IDE installed on it, also a smartphone.

Set up the Serial Bluetooth Terminal application to connect the ESP-32 with a smartphone via a Bluetooth connection. In this project, I use Android. Open the play store application, then search for a serial Bluetooth terminal in the search field. Click install, wait for it to install, and click open.

The steps for assembling the tool are just simply attaching the ESP-32 to the breadboard to make it look neat.

Open the Arduino application on your computer/laptop. Click on the File section, then click on Examples, then look for BluetoothSerial, and select SerialToSerialBT.

When the sketch is run, make sure the board and ports are correct. Then, verify the sketch to check whether there are still errors or not. Upload the sketch to the ESP-32 so that the program can run. After successfully uploading, click the Tools section, then select Serial Monitor. The following are the steps for connecting the ESP-32 so that it is detected as a device that has a Bluetooth connection. make sure that the baud used matches the numbers in the Begin Sketch series.

The serial monitor display would be blank at first. To bring up something, then on the ESP-32, the EN button must be pressed.

Execute The Project on Android

Turn on the bluetooth first, open the bluetooth serial terminal application, open the triple strip at the top left and select devices.

Pair the ESP-32 device on the smartphone by scanning and clicking ESP32Test.

Click ESP32Test on the device and the application will try to connect the smartphone with the ESP-32.

Trying to enter input from the application so that the message can be displayed on the serial monitor.

The next one, we will try to send a message from the serial monitor and expect the message to be displayed on the smartphone.

After doing all the steps without any errors and problems, that means we have completed the first part, the Bluetooth Terminal Application.

Exchange Data Using Bluetooth Serial

In the second part we will take advantage of messages from the serial bluetooth terminal application on a smartphone in order to change the state of a circuit that is connected via the ESP-32. Exchange data that will be used in the form of on and off LEDs.

Prepare additional things, such as :

  1. LED 3mm

2. 330 Ohm Resistor

3. Male to Male Cable

Install the LED on the breadboard that has the ESP-32 installed. The left leg of the LED is the negative pole and the right leg of the LED is the positive pole. Install the male to male jumper cable on the GND pin and then connect it to the negative pole of the breadboard. Install a 330 ohm resistor vertically parallel to the negative leg of the LED and connected to the negative pole of the breadboard. Install the male to male jumper cable vertically parallel to the positive leg of the LED and connect it to pin ESP-32.

Open the Arduino application on your laptop/computer. Create a new file by pressing CTRL+N on the keyboard. After that enter the code as follows:

// Load libraries
#include "BluetoothSerial.h"
// Check if Bluetooth configs are enabled
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
// Bluetooth Serial object
BluetoothSerial SerialBT;
// GPIO where LED is connected to
const int ledPin = 23;
// Handle received and sent messages
String message = "";
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
// Bluetooth device name
SerialBT.begin("ESP32");
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
// Read received messages (LED control command)
if (SerialBT.available()){
char incomingChar = SerialBT.read();
if (incomingChar != '\n'){
message += String(incomingChar);
}
else{
message = "";
}
Serial.write(incomingChar);
}
// Check received message and control output accordingly
if (message =="led_on"){
digitalWrite(ledPin, HIGH);
}
else if (message =="led_off"){
digitalWrite(ledPin, LOW);
}
delay(20);
}

To be able to run the command to turn the lights on and off, we return to the serial Bluetooth terminal application.

Change the name and value on M1 and M2. Rename M1 to “Turn on” and change the value to led_on. Next, rename M2 to “Turn off” and change the value to led_off. This is so that we don’t need to write a message when we want to turn the LED on or off, but we only press one of the desired shortcuts.

Try pressing each of the shortcuts to turn the LED on and off. The final state of the end will produce the following result.

Bluetooth Low Energy

Bluetooth Low Energy or BLE is the latest protocol from Bluetooth that optimizes Bluetooth performance in terms of effective use of power so that it consumes less power than conventional Bluetooth. Let's try using it at the end of this project.

Open the Arduino application on your computer/laptop. Click on the File section, then click on Examples, then look for ESP32 BLE Arduino, and select BLE_server.

Change the code in the print text section.

pCharacteristic->setValue(“Hello, back again with ESP32 tutorial!”);

After successfully uploading, just like in part one, we have to connect the ESP-32 so that it is detected as a Bluetooth-connected device. With Bluetooth still on, open the nRF Connect for Mobile app. Click scan at the top right, then wait for the ESP-32 device to appear. It will appear as “Long name works now”. Then, click connect on the ESP-32 device.

After successfully connecting, there is a special page for the device, click that section, then check the client section. There are generic attributes, generic access, and unknown services. If we click on the unknown service, the unknown characteristic will appear plus symbols such as download and upload.

Click the button that resembles a download, then the UUID, properties, and values ​​will appear. Look at the end of the value, the message that should appear is the same as the sketch setValue section that we entered earlier.

If this section goes well without any errors and the message that appears in the nRF Connect for Mobile application matches what we entered in the sketch, then this indicates that we have successfully used BLE in the third part of this project.

So, you reached the end of this tutorial.

Feel free to contact me at 18220027@std.stei.itb.ac.id for any questions or advice about this tutorial. Thank You!

Name : Andreana Hartadi Suliman

NIM : 18220027

--

--

Andreana Hartadi Suliman

If we are afraid of failure, it means we have limited our ability. — Hi, nice to see you here. Have a good day!