PROJECT VII : Bluetooth Classic dan Bluetooth Low Energy (BLE) ESP32

Rayhan Naufal Luthfi
6 min readMar 30, 2022

Welcome to my seventh project of ESP32. This project is a form of implementation of the knowledge that i got from Embedded System course. In this next project, we’re going to learn the Bluetooth feature in ESP32.

#What is Bluetooth?

Based on Wikipedia, Bluetooth is short-range wireless technology that is used for exchanging data between fixed and mobile devices over short distances using UHF radio waves in the ISM bands, from 2.402 to 2.48 GHz, and building personal area networks. How about Bluetooth Low Energy or BLE? Bluetooth Low Energy is a power-conserving variant of Bluetooth. BLE’s primary application is short distance transmission of small amounts of data (low bandwidth). Unlike Bluetooth that is always on, BLE remains in sleep mode constantly except for when a connection is initiated. This makes it consume very low power. BLE consumes approximately 100x less power than Bluetooth.

#Required Software

ESP32 can connect to other device using Bluetooth. To do that, you will need serial monitor apps on your mobile device. You can search for “Serial Bluetooth Terminal” in playstore and download it.

Open the application and this is the first look of the application

Now you need to setup your Bluetooth on the ESP32, to do so we need to upload a certain code to the ESP32. You can use this code to setup the Bluetooth. I named the ESP32’s Bluetooth, ESP32Bluetooth. You can change the bluetooth name in the code

#include "BluetoothSerial.h"#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32Bluetooth"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}

Now, you need to pair your mobile phone to your ESP32. To do so, open the bluetooth setting on your phone and tap connect to “ESP32Blueetooth” device.

After you pair the bluetooth, open the Serial Bluetooth Terminal and go to the device option in the left bar. Now, search your Bluetooth name and connect it

#It’s Experiment Timee!!

To test our bluetooth feature, we can send a message between the ESP32 and our mobile phone. To do so, just type a message on your mobile phone and click send. You can see the message on the serial monitor on the ArduinoIDE application

You can also reply the message from the ESP32 to the mobile phone simply by just type the message and click send like this

#Remoting LED Light Using Bluetooth

We can combine the bluetooth feature to LED. In this experiment i’ll turn on the LED using my mobile phone. To do that, we need to assemble a simple circuit like our first project and paste the code

And here is the code, feel free to customize this code

#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
const int ledPin = 18;
String message = "";
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
SerialBT.begin("ESP32Bluetooth"); //Bluetooth device name
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 =="turn_on"){
digitalWrite(ledPin, HIGH);
}
else if (message =="turn_off"){
digitalWrite(ledPin, LOW);
}
delay(20);

After you uploaded the code, connect your phone with your ESP32 Bluetooth. Then open the Serial Bluetooth Terminal on your phone, bind the M1 and M2 button with value “turn_on” and “turn_off”. And here is an example video of our experiment that i made

#Bluetooth Low Energy using ESP32

After we try the classic type of Bluetooth, now we’re going to try the Bluetooth Low Energy (BLE) type. For the hardware, we need a laptop and 2 ESP32s. But alternatively we can use our smartphone (IOS or Android). We need to download nRF Connect for Mobile (you can download it from PlayStore or AppStore).

Now we’re going to set our ESP32 as server. To do this, you can go to File > Examples > ESP32 BLE Arduino > BLE_server or you can simply copy this code below

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("ESP32 BLE Server Test");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("MALAM MALAM MALAM LUAR BIASA!!!");
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read it in your phone!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}

Now open nRF Connect and turn on Bluetooth on your mobile phone. After that follow these steps to try the BLE Feature

#1 Click The Scan Button

#2 After the ESP32 Bluetooth name popped up, click connect button and open the “client” tab. Mine ESP32 Bluetooth name is “ESP32 BLE Server Test”. You can change the name in your code.

#3 The client tab should look like the picture below. If you press the download button, it will receive message that I have coded before. there should be written a text “MALAM MALAM MALAM LUAR BIASA!!!”.

And so that’s my experience in working on the sixth project. Thank you for reading this article and I hope this article can be helpful for people who want to start trying to learn ESP32.

Rayhan Naufal Luthfi
Information System Technology ITB 2020
18220048

--

--