ESP32 Chapter 7: ESP32 Bluetooth Low Energy
Hello and Welcome!
Having been said in the ESP32 Chapter 6 post, it is time to explore the second part of the Bluetooth module, BLE (Bluetooth Low Energy).
Bluetooth Low Energy (BLE)
What exactly is a BLE?
BLE is a power-conserving variant of Bluetooth whose 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 which makes it consume very low power, approximately 100x less power than Bluetooth (randomnerdtutorials.com)
This feature is useful for data exchange, much more common for small amounts of data exchange between devices.
The ESP32 can act either as a client or a server. As a server, it advertises its existence so that the device can be found by other devices to pass the data to the client. As a client, it scans all the devices nearby, and when it finds the server it refers to, it receives the incoming data.
First, we need to look up at the default code of the ESP32 for each role as a client and server consecutively.
// BLE SERVER#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);
}// BLE SCAN#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>int scanTime = 5; //In seconds
BLEScan* pBLEScan;class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};void setup() {
Serial.begin(115200);
Serial.println("Scanning...");BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}void loop() {
// put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
To do this, we need only two ESP32 boards connected to their own PCs. One acts as the server, the other as the client. In this case, we paired up with Adi’s group.
The ESP32 connected to Adi’s laptop acted as a server. We gave a name to the device “PATURIKU PATURIKU PATURIKU”.
When a message shown in the Serial Monitor as above, that device is ready to be advertised. Here is the look of my Serial Monitor searching for nearest devices from the client point of view.
If you look closely, after the scanning finished, there were 17 devices found, one of them being the “PATURIKU PATURIKU PATURIKU” device, also with their addresses and manufacturer data. It indicated that our ESP32 was able to connect with Adi’s ESP32.
Although the whole process was quite simple, I am looking forward to what ESP32 and Bluetooth combination’s other interesting capabilities other than this and the data transmission in ESP32 Chapter 6.
Christovito Hidajat
18218043