Bluetooth version 4.0 introduced Bluetooth with low energy functionality, referred to as BLE, which gave developers the ability to create applications that can run on coin- cell batteries for months and even years at a time. Some of these applications are so efficient that the kinetic energy from just flipping a switch can provide operating power.
Choosing Bluetooth low energy technology is easily-based on the ultra-low power consumption which enables longer battery life.
BLE is increasingly powering many applications within the automotive industry too such as car access, car sharing, piloted parking and cable replacement systems allowing a new level of innovation with connected devices such as smartphones and smartwatches.
ESP32 Bluetooth Low Energy Applications using Zerynth
Zerynth r2.2.0 update enabled BLE for ESP32 based-boards.
BLE drivers are now available, including support for secure pairing and scanning; Refer to the official doc for more info.
Many examples are available – going from a simple scanner to beacons to a more advanced GATT server. To test the examples it is suggested to use a BLE app like the nRF Connect and a Beacon scanner like Beacon Scanner.
As an example, We are going to implement BLE Alerts with ESP32 using Zerynth.
We are going to use the DOIT ESP32 Devkit V1 alongside nRF Connect smartphone application.
- Connect and Virtualize the device.If this is your first time using Zerynth studio, follow this documentation on getting started with Zerynth studio. Make sure you choose the “BLE VM” when you virtualize the board, to gain access to the BLE features.
- Clone the example “BLE Alerts code“.
- Verify and Uplink the script into the DOIT ESP32 Devkit V1. To verify and uplink the script, press the “up arrow” icon found below:
- Once the code is uplinked, you will receive the message “Uplink done” in the terminal log of the Zerynth Studio. Open the console to debug the status of the request; press the “terminal” icon found below to open the console:
- Then you have to open the nRF Connect App on your smartphone and start scanning for the device.
- ”ZNotifier” will appear as a connectable device, click on the connect button. The device will acknowledge the connection between the two devices and print a “connected” message in the serial monitor.
- Information about the device will be shown, with the ability to control different attributes such as enabling/disabling notification.
The code starts with the initialization of the BLE driver for the ESP32 based-boards, in
in from espressif.esp32ble import esp32ble as bledrv.
from wireless import ble
Then we set the Generic Access Profile (GAP), which controls connections and advertising in Bluetooth.
Then we set two call-back functions on two events: on connection
connection_cb and on disconnection
After that, we set the Generic Attribute (GATT) with an alert notification service.
s = ble.Service(0x1811)
We define two GATT characteristics, one for counting new alerts and another for enabling/disabling notifications.
cn = ble.Characteristic(0x2A47, ble.NOTIFY | ble.READ,16,"New Alerts",ble.BYTES)
cc = ble.Characteristic(0x2A44, ble.WRITE ,2,"Alerts control",ble.BYTES)
Then, we add a callback for the change of enabling/disabling notifications.
Setup advertising, start BLE stack and start advertising.
Finally, in the main infinite loop, create a simple alert and notify the device if the notifications are enabled on the device.
On a final note
The docs provide a wide range of examples, like simple scanning of BLE devices, iBeacon beacon/reader, BLE secure alerts.
Check the Zerynth docs for Espressif ESP32 BLE for more info.
We hope that this tutorial was easy enough to follow and that you have successfully connected your IoT devices with BLE using Zerynth.
Stay tuned for more articles coming your way.