ESP32 Bluetooth Low Energy (BLE) on Arduino IDE
The ESP32 comes not only with Wi-Fi but also with Bluetooth and Bluetooth Low Energy (BLE). This post is a quick introduction to BLE with the ESP32.
What is Bluetooth Low Energy?
Bluetooth Low Energy, BLE for short, is a power-conserving variant of Bluetooth. BLE’s primary application is short distance transmission of small amounts of data (low bandwidth). Unlike Bluetooth that is always on, BLE remains in sleep mode constantly except for when a connection is initiated.
This makes it consume very low power. BLE consumes approximately 100x less power than Bluetooth (depending on the use case).
Take a look at the table below that compares BLE and Bluetooth in more detail.
Bluetooth Low Energy (LE) | Bluetooth Basic Rate/ Enhanced Data Rate (BR/EDR) |
|
---|---|---|
Optimized For… | Short burst data transmission | Continuous data streaming |
Frequency Band | 2.4GHz ISM Band (2.402 – 2.480 GHz Utilized) | 2.4GHz ISM Band (2.402 – 2.480 GHz Utilized) |
Channels | 40 channels with 2 MHz spacing (3 advertising channels/37 data channels) |
79 channels with 1 MHz spacing |
Channel Usage | Frequency-Hopping Spread Spectrum (FHSS) | Frequency-Hopping Spread Spectrum (FHSS) |
Modulation | GFSK | GFSK, π/4 DQPSK, 8DPSK |
Power Consumption | ~0.01x to 0.5x of reference (depending on use case) |
1 (reference value) |
Data Rate | LE 2M PHY: 2 Mb/s LE 1M PHY: 1 Mb/s LE Coded PHY (S=2): 500 Kb/s LE Coded PHY (S=8): 125 Kb/s |
EDR PHY (8DPSK): 3 Mb/s EDR PHY (π/4 DQPSK): 2 Mb/s BR PHY (GFSK): 1 Mb/s |
Max Tx Power* | Class 1: 100 mW (+20 dBm) Class 1.5: 10 mW (+10 dbm) Class 2: 2.5 mW (+4 dBm) Class 3: 1 mW (0 dBm) |
Class 1: 100 mW (+20 dBm) Class 2: 2.5 mW (+4 dBm) Class 3: 1 mW (0 dBm) |
Network Topologies | Point-to-Point (including piconet) Broadcast Mesh |
Point-to-Point (including piconet) |
for more information please visit https://www.bluetooth.com/
BLE with ESP32
The ESP32 can act as a BLE server or as a BLE client. There are several BLE examples for the ESP32 in the ESP32 BLE library for Arduino IDE. This library comes installed by default when you install the ESP32 on the Arduino IDE.
Note: You need to have the ESP32 add-on installed on the Arduino IDE. Follow one of the next tutorials to prepare your Arduino IDE to work with the ESP32, if you haven’t already.
Visit this Article https://iotbyhvm.ooo/arduino-esp32-support-on-windows-and-ubuntu/
In your Arduino IDE, you can go to File > Examples > ESP32 BLE Arduino and explore the examples that come with the BLE library.
Note: to see the ESP32 examples, you must have the ESP32 board selected on Tools > Board. and Select correct port.
ESP32 BLE Server
To create an ESP32 BLE Server, open your Arduino IDE and go to File > Examples > ESP32 BLE Arduino and select the BLE_server example.
I explain Code how the Code Works
For creating a BLE server, the code should follow the next steps:
- Create a BLE Server. In this case, the ESP32 acts as a BLE server.
- Create a BLE Service.
- Create a BLE Characteristic on the Service.
- Create a BLE Descriptor on the Characteristic.
- Start the Service.
- Start advertising, so it can be found by other devices.
How the code works
Let’s take a quick look at how the BLE server example code works.
It starts by importing the necessary libraries for the BLE capabilities.
#include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h>
Then, you need to define a UUID for the Service and Characteristic.
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b" #define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
You can leave the default UUIDs, or you can go to uuidgenerator.net to create random UUIDs for your services and characteristics.
In the setup(), it starts the serial communication at a baud rate of 115200.
Serial.begin(115200);
Then, you create a BLE device called “MyESP32”. You can change this name to whatever you like.
// Create the BLE Device BLEDevice::init("MyESP32");
In the following line, you set the BLE device as a server.
BLEServer *pServer = BLEDevice::createServer();
After that, you create a service for the BLE server with the UUID defined earlier.
BLEService *pService = pServer->createService(SERVICE_UUID);
Then, you set the characteristic for that service. As you can see, you also use the UUID defined earlier, and you need to pass as arguments the characteristic’s properties. In this case, it’s: READ and WRITE.
BLECharacteristic *pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE );
After creating the characteristic, you can set its value with the setValue() method.
pCharacteristic->setValue("Hello World says Neil");
In this case we’re setting the value to the text “Hello World says Neil”. You can change this text to whatever your like. In future projects, this text can be a sensor reading, or the state of a lamp, for example.
Finally, you can start the service, and the advertising, so other BLE devices can scan and find this BLE device.
BLEAdvertising *pAdvertising = pServer->getAdvertising(); pAdvertising->start();
This is just a simple example on how to create a BLE server. In this code nothing is done in the loop(), but you can add what happens when a new client connects (check the BLE_notify example for some guidance).
ESP32 BLE Scanner
Creating an ESP32 BLE scanner is simple. Grab another ESP32 (while the other is running the BLE server sketch). In your Arduino IDE, go to File > Examples > ESP32 BLE Arduino and select the BLE_scan example.
Once the code is uploaded and you should have the two ESP32 boards powered on:
- One ESP32 with the “BLE_server” sketch;
- Other with ESP32 “BLE_scan” sketch.
Go to the Serial Monitor with the ESP32 running the “BLE_scan” example, press the ESP32 (with the “BLE_scan” sketch) ENABLE button to restart and wait a few seconds while it scans.
Testing the ESP32 BLE Server with Your Smartphone
Download BLE Scanner App https://play.google.com/store/apps/details?id=com.macdom.ble.blescanner&hl=en_IN Or use nRF Connect for Mobile Apphttps://play.google.com/store/apps/details?id=no.nordicsemi.android.mcp&hl=en_IN
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