BlueTooth Low Energy (BLE)
The ESP32 provides a BlueTooth interface and MicroPython implements a low level interface tto BlueTooth Low Energy or BLE.
Some applications may prefer using BLE instead of WiFi for their commnunication with the outside world and you may want to port applications written for other processors that only support BLE.
I found this
YouTube video, demonstrating how to communicate between an ESP32 and a smart phone using a serial BLE protocol (
Nordic UART Service NUS). It uses the user LED and the GPIO-0 button, used be some CPU boards to put the ESP32 into flash programming mode. If not used for flash programming the button can be used by any program. Unfortunately I could not find the program source any longer and I therefore decided to re-create it myself. You can find the source code here:
https://github.com/uraich/IoT-Course/tree/master/exercises/solutions/exercise_22
For experiments with
TinyML I bought an ESP32-S3FH4R2 CPU board, which has both, the GPIO-0 button as well as the user LED, which however is implemented as a ws2812 neopixel rgb LED.
As you can see on the photo, the ws2812 is connected to GPIO-47.
The program advertises the NUS service and waits for connections. In this state it flashes the LED at high frequeny ( 10 Hz) .You can use Serial BlueTooth app to connect to it. As soon as the BLE central has connected the LED switched to steady on. Now you can read the LED state from the BLE terminal with the command:
read_LED. If the button is pressed, the LED toggles its state and the BLE terminal gets the notification message:
LED state will be toggled.
The result
The BLE program on the ESP32 can be accessed through the BLE terminal app on the smart phone.
|
|
The device is discovered |
The communication |
On the ESP32, this is what we see:
The black text is the log from the ESP32, the blue text has been added for explication later.
Running a BLE terminal on the PC
In order to run a BLE terminal on the PC several steps are needed:
- ble-serial must be installed on a Python virtual environment
- you must run ble-scan to check that the ESP32 BLE program is detected
- you must run ble-serial and find the BLE MAC address. This will connect the BLE serial protocol onto a /dev/pts/x terminal (x must be replaced by the number indicated)
- finallly run minicom on the pts device and set local echo on (<ctrl> a E)
--
Uli Raich - 2023-10-30
Comments