ESP32 BLE Client Setup: A Comprehensive IoT Tutorial

The ESP32 is a versatile microcontroller that supports Bluetooth Low EnergyNative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. (BLE), making it an excellent choice for IoT applications requiring low-power wireless communication. This guide provides a comprehensive overview of setting up the ESP32 as a BLE client, enabling it to scan, connect, and interact with BLE peripherals such as heart rate monitors, environmental sensors, or other IoT devices.

Table of Contents🔗

Introduction to BLE Client Communication🔗

BLE client communication involves the ESP32 acting as a central device that discovers and interacts with BLE peripherals (servers). The client scans for nearby devices, connects to them, and reads or writes data to their GATT (Generic Attribute ProfileNative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects.) characteristics. This setup is ideal for applications like health monitoring, home automation, and industrial sensor networks.

BLE Client Workflow on ESP32🔗

The typical workflow for a BLE client on the ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. involves the following steps:

1. Initialize the BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. stack and set up the client.

2. Scan for nearby BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. devices and filter them based on their advertised services or names.

3. Connect to the desired peripheral and discover its GATTNative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. services and characteristics.

4. Read or write data to the peripheral’s characteristics.

5. Handle notifications or indications for real-time data updates.

6. Disconnect when the interaction is complete.

Prerequisites🔗

1. Hardware: ESP32 boardSetting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips., BLE peripheral (e.g., pulse sensor).

2. Libraries: Arduino IDE with ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. BLE Arduino or NimBLE library installed.

3. Basic Knowledge: Familiarity with GATTNative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. structure (Services, Characteristics, UUIDs).

Setting Up the BLE Client🔗

To begin, ensure you have the ESP32 Arduino coreZigbee Over-the-Air (OTA) Firmware Updates with ESP32 CoordinatorsZigbee Over-the-Air (OTA) Firmware Updates with ESP32 CoordinatorsSecure your IoT network with OTA firmware upgrades using an ESP32 coordinator. Our guide details firmware setup, packaging, security, and troubleshooting. installed. The ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. BLE Arduino or NimBLE library is recommended for BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. communication due to its efficiency and ease of use.

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
void setup() {
  Serial.begin(115200);
  BLEDevice::init("ESP32_BLE_Client");
}

This code initializes the BLE stack and sets up the ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. as a BLE client.

Scanning for BLE Devices🔗

The next step is to scan for nearby BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. devices. Use the BLEScan class to start scanning and filter devices based on their advertised data.

void scanForDevices() {
  BLEScan* pScan = BLEDevice::getScan();
  pScan->setActiveScan(true); // Enable active scanning for faster discovery
  pScan->start(5, false); // Scan for 5 seconds
  BLEScanResults foundDevices = pScan->getResults();
  for (int i = 0; i < foundDevices.getCount(); i++) {
    BLEAdvertisedDevice device = foundDevices.getDevice(i);
    Serial.printf("Found Device: %s\n", device.toString().c_str());
  }
}

Connecting to a BLE Peripheral🔗

Once you’ve identified the target device, connect to it using its address.

BLEClient* pClient = BLEDevice::createClient();
if (pClient->connect(device.getAddress())) {
  Serial.println("Connected to BLE device");
}
  • Tip: Use BLEDevice::getAddress() to reconnect to known devices.

Discovering Services and Characteristics🔗

After connecting, query the server’s GATTNative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLENative Protocols: Wi-Fi (2.4 GHz), Bluetooth Classic, and BLEExplore ESP32 connectivity with Wi-Fi, Bluetooth Classic, and BLE. Learn implementation tips and best practices for IoT projects. table to find available services:

BLERemoteService* pRemoteService = pClient->getService(SERVICE_UUID);
if (pRemoteService != nullptr) {
  BLERemoteCharacteristic* pChar = pRemoteService->getCharacteristic(CHAR_UUID);
}
Service NameUUID
Heart Rate0x180D
Environmental Sensing0x181A

Reading and Writing Characteristics🔗

Read sensor dataSigfox Message Encoding: Packing Sensor Data into 12-byte PayloadsSigfox Message Encoding: Packing Sensor Data into 12-byte PayloadsLearn efficient data encoding techniques for Sigfox's constrained 12-byte payloads. Discover bitwise operations, structured encoding & CBOR strategies. or send commands:

// Read
std::string value = pChar->readValue();
Serial.printf("Value: %s\n", value.c_str());
// Write
pChar->writeValue("ON", 2);

Handling Notifications and Indications🔗

Subscribe to characteristic updates for real-time data:

pChar->registerForNotify([](BLERemoteCharacteristic* pChar, uint8_t* data, size_t length, bool isNotify) {
  Serial.printf("New value: %.*s\n", length, data);
});
// Enable notifications on the server
pChar->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)notificationOn, 2, true);

Practical Example: Interfacing with a Heart Rate Monitor🔗

1. Scan for Devices: Filter by heart rate service UUID (0x180D).

2. Connect: Use the sensor’s MAC address.

3. Subscribe: Enable notifications for the Heart Rate Measurement characteristic (0x2A37).

// Simplified callback
void notifyCallback(BLERemoteCharacteristic* pChar, uint8_t* data, size_t length, bool isNotify) {
  uint8_t heartRate = data[1];
  Serial.printf("Heart Rate: %d BPM\n", heartRate);
}

Troubleshooting Common Issues🔗

By following this guide, you can effectively set up and use the ESP32 as a BLE client, enabling seamless communication with a wide range of IoT devicesConnecting ESP32 to Cloud Services via Wi-FiConnecting ESP32 to Cloud Services via Wi-FiDiscover how to connect your ESP32 to AWS, Azure, and Google Cloud using secure Wi-Fi. This guide covers setup, error handling, and low power strategies.. Whether you’re building a health monitoring system or a smart home solution, the ESP32’s BLE capabilities provide a robust foundation for your projects.

Happy coding!

Author: Marcelo V. Souza - Engenheiro de Sistemas e Entusiasta em IoT e Desenvolvimento de Software, com foco em inovação tecnológica.

References🔗

Share article

Related Articles