
The ESP32, a powerful microcontroller from Espressif, is well-known for its built-in Bluetooth and Wi-Fi capabilities. One of its most versatile features is Bluetooth Low Energy (BLE), which allows for energy-efficient wireless communication. BLE is perfect for IoT applications where power conservation is essential. In this blog, we will guide you through the basics of setting up and using BLE on the ESP32.
What is Bluetooth Low Energy (BLE)?
Bluetooth Low Energy is a power-efficient version of Bluetooth. It’s designed for applications that don’t need continuous data streaming but rather periodic data exchange, making it ideal for IoT devices, sensors, and wearables. Unlike classic Bluetooth, BLE devices can remain in sleep mode for long periods and only activate when necessary, saving significant power.
Why Use BLE with ESP32?
Energy Efficiency: BLE uses significantly less power than traditional Bluetooth, making it ideal for battery-powered projects.
Wide Compatibility: BLE is supported by most modern smartphones and devices, making it easier to create apps and interfaces for your projects.
Robust Connectivity: With BLE, ESP32 can communicate with a wide range of devices and sensors with minimal power consumption.
Requirements to Start To start with BLE on ESP32, you’ll need the following:
1)ESP32 Development Board
2)Arduino IDE or Espressif’s ESP-IDF (development frameworks)
3)BLE-compatible smartphone or device (for testing)
4)BLE library (available in the Arduino IDE)
Setting Up the Environment
1)Install Arduino IDE and ESP32 Libraries
>Download and install the Arduino IDE from arduino.cc.
>In Arduino IDE, go to File -> Preferences and enter https://dl.espressif.com/dl/package_esp32_index.json in the “Additional Board Manager URLs” field.
>Open the Board Manager via Tools -> Board -> Board Manager, search for ESP32, and install the board package.
2)Install BLE Libraries
>In the Arduino IDE, navigate to Sketch -> Include Library -> Manage Libraries and search for BLE. Install the ESP32 BLE Arduino library.
Basic BLE Code for ESP32
To get started, we will create a simple BLE server using the ESP32. The server will advertise its presence, and a BLE-compatible device (like a smartphone) can connect to it and read or write data.
Example Code
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
BLEServer *pServer = NULL;
BLECharacteristic *pCharacteristic = NULL;
#define SERVICE_UUID "12345678-1234-1234-1234-123456789012"
#define CHARACTERISTIC_UUID "87654321-4321-4321-4321-210987654321"
void setup() {
Serial.begin(115200);
// Initialize BLE
BLEDevice::init("ESP32-BLE-Server");
pServer = BLEDevice::createServer();
// Create a BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
Serial.println("BLE server is now advertising");
}
void loop() {
// Do nothing here
}
Understanding the Code
BLEDevice::init(): Initializes the BLE functionality on the ESP32.
BLEServer: Creates a BLE server that will handle incoming connections.
BLEService: A service defines a specific set of functionalities or data. In our case, it’s a generic service.
BLECharacteristic: A characteristic is a piece of data that can be read or written by a BLE client.
This code makes the ESP32 act as a BLE server that advertises itself. A BLE client can then connect, read data from, or write data to the ESP32.
Testing with a Smartphone
1)Install a BLE scanner app (available on both iOS and Android).
2)Scan for BLE devices, and you should see a device called ESP32-BLE-Server.
3)Connect to the ESP32 and view its services and characteristics. You can also write data to the characteristic from the app and read it back on the ESP32.
Next Steps
After setting up the basic server, you can:
>Add more characteristics to the service for handling multiple data points.
>Set up notifications for real-time data updates.
>Use the ESP32 as a BLE client to connect to other BLE devices like sensors.
With BLE, the ESP32 becomes a powerful tool for creating energy-efficient IoT applications. Whether you’re building wearables, sensors, or home automation systems, BLE can provide robust and low-power wireless communication.
In this guide, we covered the basics of getting started with BLE on ESP32. With the groundwork in place, you can explore more advanced features and build applications tailored to your specific needs.