Project 7 : ESP32 Bluetooth
Hello everyone! Welcome back to the seventh chapter of me trying to learn how to use ESP32. In this chapter I’ll show you how I experimented with the Bluetooth and BLE (Bluetooth Low Energy) features of ESP32.
In this first part, I’ll show you how I did the setup and test with Bluetooth Classic.
Necessary Components
Note that this experiment unfortunately is possible for android phones only.
ESP32+Breadboard (1 each) — Here I used a longer breadboard, feel free adjust it to your needs
Micro USB Cable (1 pcs)
Laptop/PC (1 pcs)
Android Phone (1 pcs)
Device Set Up
To be able to use this Bluetooth feature on your mobile device, you need to download Serial Bluetooth Terminal on that device. You can look it up on Google Play Store and download it.
Code the Program
Open your Arduino IDE, and go to File > Examples > BluetoothSerial > SerialtoSerialBT.
The following code should load.
#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
#endifBluetoothSerial SerialBT;void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //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);
}
Compile the code and wait until it says ‘Done compiling’.
Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.
Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.
After it’s done uploading, go to Tools > Serial Monitor.
A new window will pop up, then set the serial to 115200 baud. The window should look like this.
If the message don’t show up, press the EN button on the ESP32 to restart the ESP32.
Now you need to pair the ESP32 to your mobile device. Turn on the Bluetooth on the device, and pair it with ‘ESP32test’.
Then open the Serial Bluetooth Terminal on your mobile device. Go to the menu on the top left (3 horizontal line icon) and go to Devices. Tap the ESP32test so that the gray line on the left of the device name turns to green.
Press back to the terminal and the terminal should show that the phone is connected to the ESP32test.
You can type in Hello in the space on the bottom of the screen and send it by pressing the send button on the bottom right.
Then check the serial monitor and the Hello should show on the serial monitor.
You can also do it the other way round by typing in the message on the message box on the serial monitor and click send.
The message should show on the terminal on your mobile device.
In this part, I’ll show you how to turn on and turn off LED light by your mobile device using Bluetooth Classic.
Necessary Components
ESP32+Breadboard (1 each) — Here I used a longer breadboard, feel free adjust it to your needs
Micro USB Cable (1 pcs)
Laptop/PC (1 pcs)
Male-to-Male Jumper cable (2 pcs)
5mm LED Light (1 pcs)
330 Ohm Resistor (1 pcs)
Android Phone (1 pcs)
Arrangements on the breadboard
Make sure the ESP32 is secured on the breadboard. Then attach one LED light on the breadboard, attach a 330 ohm resistor on the negative leg and connect it to the GND pin on the ESP32 using a jumper cable. On the positive leg, connect it to GPIO 22 on the ESP32. Here’s how my finished circuit looked like.
Code the program
After the arrangements in the circuit is completed, open the Arduino IDE. Make a new file then save it, and then copy and paste the code below.
#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
#endifBluetoothSerial SerialBT;
const int ledPin = 22;
String message = "";void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
SerialBT.begin("ESP32test"); //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 =="led_on"){
digitalWrite(ledPin, HIGH);
}
else if (message =="led_off"){
digitalWrite(ledPin, LOW);
}
delay(20);
}
Compile the code and wait until it says ‘Done compiling’.
Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.
Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.
After the code is uploaded, connect the mobile device with the ESP32 Bluetooth. Open the serial Bluetooth terminal, change so that the M1 and M2 button has value “led_on” and “led_off”.
After those steps, you can turn the LED light on and off simply by pressing the buttons on the Serial Bluetooth Terminal on your mobile device. Here’s my result on the video attached below.
In this part, I’ll show you how I tried using the Bluetooth Low Energy (BLE).
To be able to use BLE on your mobile device, you need to download nRF Connect for Mobile on that device. You can look it up on Google Play Store and download it.
Code the program
After the arrangements in the circuit is completed, open the Arduino IDE. Make a new file then save it, and then copy and paste the code below.
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>#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 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("Avengers, assemble!");
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);
}
Compile the code and wait until it says ‘Done compiling’.
Before uploading the code, don’t forget to make sure that the ESP32 is connected. Also make sure the Arduino IDE is set on the right board and port on the Tools menu.
Then click on the right arrow icon next to the tick icon to upload the code to the ESP32 and wait until it’s done uploading.
After it’s done uploading, go to Tools > Serial Monitor.
A new window will pop up, then set the serial to 115200 baud. The window should look like this.
If the message don’t show up, press the EN button on the ESP32 to restart the ESP32.
Now open the nRF Connect for Mobile on your mobile device, then tap on the SCAN button on the top right.
Since I set the name of the server as ‘ESP32-BLE Test’, find that server and click on the CONNECT button.
After the button is clicked, the application will display as below, tap on ‘Unknown Service’ to reveal the ‘Unknown Characteristic’ panel, the click on the download icon to show the message.
Since the message I decided to send was ‘Avengers, assemble’ and the message that shows up was the exact same message, we can conclude that this experiment was a success.
I didn’t face any significant struggles when doing this project, just when the serial monitor turned out blank, but I resolved it by pressing the EN button on the ESP32. I was satisfied that the experiment was successful on my first try, but I’d like to explore more regarding this feature because I feel like it has endless possibilities.
This is the end of the tutorial and I hope this helps!
See you in my next projects!