Skip to content

Latest commit

 

History

History
91 lines (60 loc) · 3.5 KB

005_bluetooth_tutorials.md

File metadata and controls

91 lines (60 loc) · 3.5 KB

Bluetooth Low Energy

Table of Contents

Prolog

IoT without Bluetooth is almost impossible these days. Current versions of MicroPython already support Bluetooth Low Energy (although this is still being developed). So that you can later for example control NeoPixel via BLE or transmit values from sensors via BLE, here some very first basic examples.

Scan for BLE devices

The simplest action is to find nearby BLE devices and then list them inside REPL (including some information).

# create new subdirectory
$ mkdir -p ~/Projects/ESP/examples/ble

# create script
$ touch ~/Projects/ESP/examples/ble/ble_scanner.py

Source Code for ble_scanner.py

Copy the script to the microcontroller as main.py.

# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/ble/ble_scanner.py /pyboard/main.py

# start repl
(venv) $ rshell -p [SERIAL-PORT] repl

Now press Control + d for a Soft Reboot of the device. After few seconds, you should see the final scan report inside the REPL. To stop the application plus to close REPL and rshell connection, press first Control + c and second Control+ x.

Own BLE service

The next example shows (just for understanding) how to let 2 devices communicate via BLE. Communication uses READ and NOTIFY (which is limited to certain ASCII character lengths).

This example does not use specified UUIDs (it will use random generated UUIDs)! To follow the example, a BLE Serial Terminal is required on a device (with Bluetooth Low Energy).

Here few examples for possible applications, you can try:

# create script
$ touch ~/Projects/ESP/examples/ble/ble_service.py

Source Code for ble_service.py

# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/ble/ble_service.py /pyboard/main.py

# start repl
(venv) $ rshell -p [SERIAL-PORT] repl

Press the keys Control + d or the reset button and observe the output. As soon as your ESP starts to advertise the BLE service, you can use the BLE Serial Terminal on a second device to communicate.

Here the example output from ESP:

[INFO] Start ble services and irq for SimpleBLE
[INFO] Start ble advertise as SimpleBLE...
[INFO] A central has connected to this peripheral
[INFO] A peripheral send notification: Hi from ESP32... to central
[INFO] A central has written this: test
[INFO] A central has disconnected from this peripheral

Here the example output from other device:

008_ble_serial_terminal.png

To stop the program, press keys Control + c. If you want to leave the REPL, press keys Control + x.

Additional information

Later examples then fall back on this basic examples. For example, the usage of sensors will be described.

Home | Previous | Next