Bluetooth Low Energy (BLE) is a power-efficient version of the classic Bluetooth technology, which is used for connecting devices over short distances. ESP32 is a low-cost, low-power system on a chip (SoC) with Wi-Fi and Bluetooth capabilities. In this article, we'll explore how to use ESP32's Bluetooth Low Energy capabilities to build a BLE device. We'll also delve into common questions that arise when working with ESP32 and BLE.
To follow along with this guide, you will need:
Additionally, if you want to test the device, you will need a second BLE-capable device, such as a smartphone.
Bluetooth Low Energy (BLE) operates in two main modes: Advertising mode and Connection mode. When a device wants to be discovered by other devices, it enters the Advertising mode. When a device wants to interact with another device, it enters the Connection mode.
On ESP32, the BLE functionality is provided by the ESP32 BLE Arduino library. You should install this library via the Arduino Library Manager (Sketch -> Include Library -> Manage Libraries in the Arduino IDE, then search for ESP32 BLE Arduino).
Here's how you might setup the basic sketch:
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer *pServer = NULL;
BLECharacteristic * pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
// Create the BLE Device
BLEDevice::init("ESP32-BLE-Example");
// ...
// Start the service
pService->start();
}
void loop() {
// ...
}
On the server side, you will create a BLE server, define a service and a characteristic for this service. A service could be thought as a group of characteristics. You could have, for instance, a Light service with characteristics like brightness, color, status, etc.
Here's a simple server setup:
void setup() {
// ...
// Create the BLE Server
pServer = BLEDevice::createServer();
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);
// ...
// Start the service
pService->start();
}
On the client side, you connect to the server and can read, write or subscribe to a characteristic.
class MyClientCallback : public BLEClientCallbacks {
void onConnect(BLEClient* pclient) {
// ...
}
void onDisconnect(BLEClient* pclient) {
// ...
}
};
void setup() {
// ...
// Create the BLE Client
BLEClient* pClient = BLEDevice::createClient();
// Connect to the BLE Server
pClient->connect(myDevice);
// Obtain a reference to the service we are after in the remote BLE server
BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
// Obtain a reference to the characteristic in the service of the remote BLE server
BLERemoteCharacteristic* pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
}
Q: What's the range of ESP32 Bluetooth?
A: The theoretical range in open space is around 100m. However, in practice, the range can be much less due to obstacles and interference.
Q: Can I use Wi-Fi and Bluetooth at the same time on ESP32?
A: Yes, it's possible to use both Wi-Fi and Bluetooth at the same time on ESP32 thanks to the chip's dual-core processor.
Q: How much power does ESP32 consume when using Bluetooth?
A: ESP32 is designed to be a low-power device. When using Bluetooth, the device typically consumes around 0.01-0.03 W, but this can vary depending on the distance and data transmission rate.
Q: Is it possible to connect to multiple BLE devices at the same time?
A: Yes, ESP32 is capable of connecting with multiple BLE devices at the same time. The exact number of connections that can be maintained simultaneously depends on the amount of available memory in the ESP32.
We hope this article has provided a comprehensive look at how to use the ESP32's Bluetooth Low Energy capabilities to build a BLE device. Whether you're creating a BLE server or client, the ESP32 makes it easy and efficient to handle Bluetooth communications. Always refer to the official ESP32 BLE Arduino documentation for the most up-to-date and comprehensive information.