ESP32 Project #7 Bluetooth

Ardi Avicenna
5 min readMar 30, 2022

--

Simple Bluetooth

Tools

For this experiment you won’t need any additional physical tools beside your ESP32 board. You will need “Serial Bluetooth Terminal” app available on android’s playstore.

Code and Mechanism

//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#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("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);
}

Now we will try to breakdown this block of 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;

it starts with code including Bluetoothserial library. Then the code have error checking mechanism to ensure if Bluetooth is properly enabled. At the las, we create an instance of BluetoothSerial called SerialBT

void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}

Now, at the setup block, we intialize a serial communication. Then, we initialize the Bluetooth serial device and pass as an argument the bLuetooth Device name. As you can see, it’s called ESP32test but you can change it up to your creativity

void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}

Now, on the loop block, we wrote conditional code to check if there are bytes being received in the serial port. If the condition is satisfied, the information could be send via Bluetooth to the connected device. There are two interesting code we could dissect,

  1. SerialBT.write() sends data using bluetooth serial.
  2. Serial.read() returns the data received in the serial port.

Then, the next conditional will check if there are bytes available to read in the bluetooth serial port. If the condition is sastified, we’ll write those bytes in the Serial Monitor.

Now, connect your device to esp32 and open your serial bluetooth application ,then connect the app by opening device and choose esp32 like this picture below!

If you are successfully connected to the esp32, your app terminal should look like this

Demonstration

This is the comparison of my terminal and serial monitor,

Troubleshooting

I encountered problem when my serial monitor upload speed isn’t the same like the upload speed stated in the code (115200 baud), please make ensure all upload speed are synced!

Low Energy Bluetooth

Tools

Still the same like the first experiment, we only need our board and additional software called “nRF Connect for Mobile”.

Code and Mechanism

We can obtain example code from ile > Examples > ESP32 BLE Arduino dan pilih BLE_server example. The code will be like this

/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
Ported to Arduino ESP32 by Evandro Copercini
updates by chegewara
*/#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("Long name works now");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);pCharacteristic->setValue("Hello World says Neil");
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);
}

We will dissect the code, but… let me change the code into my name

#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("Avicenna");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);pCharacteristic->setValue("Hello Ardi Avicenna, have a nice day!");
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);
}

okay, let’s dissect the code! Code above are necessary to make a BLE Server. There are a few important step in creating BLE Server,

  1. Create a BLE Server. In this case, the ESP32 acts as a BLE server.
  2. Create a BLE Service.
  3. Create a BLE Characteristic on the Service.
  4. Create a BLE Descriptor on the Characteristic.
  5. Start the Service.
  6. Start advertising, so it can be found by other devices.

Now upload the code into ESP32 and open the installed app. A successful uploaded code should have serial monitor looks like this

Now you can connect to esp32 in the app

Demonstration

A successful connection could be demonstrated through clicking the circled interface

And a successfull indicator should display whatever message you wrote in the code like this

Troubleshooting

Fortunately, I didn’t have any major trouble for this experiment. Just ensure that your esp32 is connected with firm cable.

--

--