esp32 ble client example

ESP32 BLE Client: Connect, Scan and Pair Example

By Laura Brown
AD

In the realm of Internet of Things (IoT) development, the ESP32, with its massive community and endless capabilities, stands out as a popular choice. One of its compelling features is its support for Bluetooth Low Energy (BLE), enabling ESP32 to communicate with other BLE devices seamlessly. This article explores a practical example of setting up an ESP32 as a BLE client, communicating with a BLE server to control a simple LED.

Before diving into the example, ensure you have the following: an ESP32 development board, a USB-to-serial converter or a USB cable, and an Arduino IDE configured for ESP32 boards, along with the necessary BLE libraries.

Setting Up the ESP32 Board

The first step involves preparing the ESP32 board by installing the necessary firmware and connecting it to your computer.

AD
ESP32 BLE Server and Client (Bluetooth Low Energy) | Random Nerd Tutorials
ESP32 BLE Server and Client (Bluetooth Low Energy) | Random Nerd Tutorials
ESP32 Web Server: Display Sensor Readings in Gauges | Random Nerd Tutorials
ESP32 Web Server: Display Sensor Readings in Gauges | Random Nerd Tutorials
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn

1. Open the Arduino IDE and go to Tools > Board and select your ESP32 board. Then, select the correct COM port under Tools > Port.

Installing the BLE Library

The ESP32 requires the BLE Primer Library to function as a BLE client. You can install it via the Library Manager in the Arduino IDE as follows:

ESP32 BLE (Bluetooth Low Energy) - The Engineering Projects
ESP32 BLE (Bluetooth Low Energy) - The Engineering Projects
the esp2 server is connected to an esp2 router and access point
the esp2 server is connected to an esp2 router and access point
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes

1. Go to Sketch > Include Library > Manage Libraries. Search for "BLE Primer" and install it.

Connecting to the BLE Server

Once the library is installed, we'll use it to connect the ESP32 to a BLE server, like a smartphone app, that acts as a central device. We'll be using a simple LED for this example, which the server will control.

AD

1. Write an Arduino sketch on your ESP32 board that initializes the BLE client, scans for the server, and connects to it.

ESP32 BLE (Bluetooth Low Energy) using Arduino IDE
ESP32 BLE (Bluetooth Low Energy) using Arduino IDE
ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
ESP32 Bluetooth Low Energy (BLE) on Arduino IDE | Random Nerd Tutorials
an esp2 server with the text web server littles next to it and another electronic device
an esp2 server with the text web server littles next to it and another electronic device
Anatomy esp32
Anatomy esp32
MicroPython: ESP32 - Getting Started with Bluetooth Low Energy (BLE) | Random Nerd Tutorials
MicroPython: ESP32 - Getting Started with Bluetooth Low Energy (BLE) | Random Nerd Tutorials
Create a Web Server with ESP32 - The Engineering Projects
Create a Web Server with ESP32 - The Engineering Projects

2. Upon connecting, the ESP32 should read the BLE characteristic (a specific value sent by the server) and control the LED accordingly. Here's a simple sketch to get you started:

```cpp #include BLEClient BLEClient; BLEAdvertisedDevice serverDevice; void setup() { Serial.begin(115200); pinMode(LED_BUILTIN, OUTPUT); BLEClient.begin(); BLEClient.setAdvertisedDeviceFilter(&serverDevice); Serial.println("Scanning..."); } void loop() { BLEClient.scanFor materDevices(); BLEClient.connect(serverDevice); if (BLEClient.connected()) { BLEClient.discoverServices(); BLECharacteristic ledCharacteristic = BLEClient.getCharacteristic("LED"); // Replace with the server's LED characteristic name ledCharacteristic.readValue(); int ledValue = ledCharacteristic.getValue(); if (ledValue == 1) { digitalWrite(LED_BUILTIN, HIGH); } else { digitalWrite(LED_BUILTIN, LOW); } delay(200); // Wait for 200ms to read the value again } } ```

Upload this sketch to your ESP32 board, and once it's connected to the BLE server, the built-in LED should turn on or off based on the server's instructions.

Testing the BLE Client

To test the ESP32 BLE client, you can use a BLE server app on your smartphone. There are various apps available, like "LightBlue" for iOS or "nRF Connect" for Android, that allow you to create and control BLE services.

1. Create a new service in the app and add a characteristic named "LED" with an integer data type. Set the initial value to 0.

2. Start the advertising process in the app, then open the Arduino IDE running the ESP32 sketch. The ESP32 should connect to the app and control the LED accordingly.

With this example, you have now created a simple ESP32 BLE client capable of communicating with a BLE server. You can build upon this foundation to create more complex IoT devices, integrating various sensors and actuators. Happy tinkering!

how to send sms from esp32 on the breadboard with an esp23
how to send sms from esp32 on the breadboard with an esp23
the temperature monitor is displayed in this screenshote image, and it shows different types of
the temperature monitor is displayed in this screenshote image, and it shows different types of
Mobile Controlled ESP32 Two-Wheel Drive Robot
Mobile Controlled ESP32 Two-Wheel Drive Robot
ESP32: BME680 Environmental Sensor using Arduino IDE | Random Nerd Tutorials
ESP32: BME680 Environmental Sensor using Arduino IDE | Random Nerd Tutorials
How to Power the ESP32 Correctly ⚡
How to Power the ESP32 Correctly ⚡
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 BLE (Bluetooth Low Energy) using Arduino IDE
ESP32 BLE (Bluetooth Low Energy) using Arduino IDE
💡 ESP32 Web Server – Control an LED from Your Browser!
💡 ESP32 Web Server – Control an LED from Your Browser!
ESP32 with Bluetooth and Bluetooth Low Energy: The Ultimate Guide | Random Nerd Tutorials
ESP32 with Bluetooth and Bluetooth Low Energy: The Ultimate Guide | Random Nerd Tutorials
Introduction to the ESP32 - From Prototype to Production
Introduction to the ESP32 - From Prototype to Production
A Complete Beginner’s Tutorial on How to Create ESP32 Web Server
A Complete Beginner’s Tutorial on How to Create ESP32 Web Server
AD