ESP32 Bluetooth Low Energy (BLE) on Arduino IDE
Introduction
The ESP32 is a versatile microcontroller that supports both Wi-Fi and Bluetooth functionalities. Among its standout features is Bluetooth Low Energy (BLE), a power-efficient wireless communication protocol ideal for IoT applications. BLE allows data exchange between devices over short distances with minimal energy consumption.
In this detailed guide, we’ll walk through setting up ESP32 BLE using the Arduino IDE. You’ll learn how to build a BLE server, communicate with a smartphone app, and establish a seamless data exchange system.
Prerequisites
Before proceeding, ensure you have the following:
- ESP32 Development Board
- Arduino IDE (latest version recommended)
- ESP32 Board Package installed in Arduino IDE
- Smartphone or Tablet with a BLE scanner app (e.g., nRF Connect, BLE Scanner)
Step 1: Install the ESP32 Board Package
If you haven’t installed the ESP32 board package in Arduino IDE yet:
- Open Arduino IDE.
- Go to File → Preferences.
- In the “Additional Board Manager URLs” field, add the following link:
https://dl.espressif.com/dl/package_esp32_index.json
- Go to Tools → Board → Board Manager.
- Search for ESP32 and click Install.
Step 2: ESP32 BLE Code for Data Communication
Create a new sketch in Arduino IDE and paste the following code:
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define SERVICE_UUID "12345678-1234-5678-1234-56789abcdef0"
#define CHARACTERISTIC_UUID "abcdef01-1234-5678-1234-56789abcdef0"
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE Service...");
// Initialize BLE
BLEDevice::init("ESP32_BLE");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create BLE Characteristic for Data Communication
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->addDescriptor(new BLE2902());
// Start the service
pService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pServer->getAdvertising()->start();
Serial.println("BLE Service is now active");
}
void loop() {
if (deviceConnected) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.print("Received Value: ");
Serial.println(rxValue.c_str());
pCharacteristic->setValue("Received: " + rxValue);
pCharacteristic->notify();
}
}
delay(1000);
}
Step 3: Upload Code to ESP32
- Connect your ESP32 board to your computer via USB.
- Go to Tools → Board and select ESP32 Dev Module.
- In Tools → Port, select the correct COM port for your ESP32.
- Click the Upload button to flash the code.
Step 4: Testing BLE Communication
To test the BLE communication:
- Install a BLE scanner app like nRF Connect or BLE Scanner on your smartphone.
- Power up your ESP32 board. It will appear as ESP32_BLE in the BLE scanner app.
- Connect to ESP32_BLE and locate the characteristic with UUID
abcdef01-1234-5678-1234-56789abcdef0
. - Send a string (e.g., “Hello ESP32”) from the BLE scanner app.
- The string should appear on the Serial Monitor in Arduino IDE.
- The ESP32 will respond back with “Received: [your message]” via BLE.
Step 5: Troubleshooting Common Issues
If you encounter issues, consider the following solutions:
- ESP32 Not Visible on BLE Scanner: Ensure BLE is initialized correctly in
setup()
and verify BLE services are active. - Data Not Received: Verify the BLE characteristic UUID is correctly defined in the code and matches the scanner app.
- Serial Monitor Not Displaying Data: Ensure your Serial Monitor baud rate is set to 115200.
- Connection Drop Issues: Verify your smartphone is within range and Wi-Fi/BLE interference is minimized.
Step 6: Applications of ESP32 BLE
The ESP32 BLE functionality is useful in various scenarios such as:
- IoT Device Control (e.g., smart home automation)
- Wireless Sensor Data Transmission
- BLE-Based Data Logging Systems
- Communication Between Multiple ESP32 Devices
Conclusion
Using ESP32 BLE on Arduino IDE offers a powerful and efficient solution for wireless data exchange in IoT applications. This guide covers essential steps to build and test a BLE-based communication system for your ESP32 projects.
Pingback: ESP32 BLE with DHT11 - ESP8266 - IoTbyHVM - Explore TechBytes
Pingback: Failed to connect ESP32: Timed out waiting for packet header
Pingback: MQTT Broker on Android | How To Run MQTT Broker in Android
Pingback: How to Use I2C LCD with ESP32 Using Arduino IDE
Pingback: IoT OS and RTOS for Internet of Things devices - IoTbyHVM
Pingback: Etcher alternatives | OS Image Flasher - IoTbyHVM - Bits & Bytes of IoT
Pingback: ROCK Pi 4 Backup and Restore uSD card or eMMC module
Pingback: Interface MQ135 (Gas Sensor) with NodeMCU - IoTbyHVM
Pingback: Top 10 IoT Cloud Platforms - CompileIoT
Pingback: Interface LDR module with NodeMCU - IoTbyHVM - Bits & Bytes of IoT
Pingback: Physical and Logical Design of IoT - IoTbyHVM