ubluetooth - Question regarding event codes and subscribing to notifying characteristics

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
bart
Posts: 2
Joined: Tue Jun 16, 2020 10:07 pm

ubluetooth - Question regarding event codes and subscribing to notifying characteristics

Post by bart » Tue Jun 16, 2020 11:50 pm

I am trying to control a BLE-accessible motor with an ESP32 board. I am quite new to microcontroller programming and first tried to use the Arduino IDE, soon to find out that C programming is still not much fun (for me). As this is part of a home improvement project I decided it should be fun and switched to MicroPython as python is pretty native to me.

I have most of the basic building blocks working at the moment, including uasyncio to allow for cooperative multi-tasking and scheduling, an asynchronous webserver to provide a GUI and ubluetooth actually talking to the motor. There are a few things however that I don't seem to grasp on the bluetooth part.

My setup:
  • Board: ESP WROOM 32 development board from Geekcreit
  • Build: "MicroPython v1.12 on 2019-12-20; ESP32 module with ESP32" with IDF v4, no SPIRAM (esp32-idf4-20191220-v1.12.bin)
1. Event codes seem to differ depending on which documentation you use
When I started playing around with this a few months ago all event codes where declared with a statement using bit shifting, like

Code: Select all

_IRQ_SCAN_RESULT = const(1 << 4) #16
In the official documentation I now find a different way to declare the event codes that doesn't use bit shifting anymore and also declares different integer values for the events:

Code: Select all

_IRQ_SCAN_RESULT = const(5) #5
I also see that some event codes have been added w.r.t. the info I found a few months ago, like _IRQ_SCAN_DONE, _IRQ_GATTC_SERVICE_DONE and _IRQ_GATTC_CHARACTERISTIC_DONE.

Question: my software version (v1.12) still seems to be the latest stable build for ESP32, but the event codes in the documentation do not match the event codes working with this build. Where would I find any documentation regarding this change and when can I expect these new event codes to be usable in a stable build for ESP32?

2. How do I subscribe to notifications from a peripheral device?
I have set up my ESP32 to act as a BLE Central, connecting to two peripheral motors.

Using the code I currently use I can successfully:
  • Scan for available devices [_IRQ_SCAN_RESULT, _IRQ_SCAN_COMPLETE]
  • Connect to the motors [_IRQ_PERIPHERAL_CONNECT, _IRQ_PERIPHERAL_DISCONNECT]
  • Discover services and characteristics [_IRQ_GATTC_SERVICE_RESULT, _IRQ_GATTC_CHARACTERISTIC_RESULT]
  • Read values from characteristics (motor position, battery level, etc) [_IRQ_GATTC_READ_RESULT]
  • Write values to characteristics (motor position target, motor move up, motor move down, stop, etc) [_IRQ_GATTC_WRITE_RESULT]
I am struggling with the event _IRQ_GATTC_NOTIFY though. I would like the uPython ESP32 Central to be notified of a change of the motor position but don't know how to subscribe to this event. The _IRQ_GATTC_NOTIFY event never gets fired.

On my Android phone I can use nRF connect to see that the motor position characteristic does have properties READ and NOTIFY.

Question: how do I subscribe to notify events for a certain characteristic? I did have a look at some example ble_temperature_central.py but it also doesn't seem to actively subscribe to the notifications.

I hope someone can shed some light on these questions. Anyway, thanks for all effort put into MicroPython and all the useful info on this forum!

Kind regards,
Bart

redyellow
Posts: 12
Joined: Tue Jun 30, 2020 10:19 am

Re: ubluetooth - Question regarding event codes and subscribing to notifying characteristics

Post by redyellow » Tue Jun 30, 2020 10:33 am

You are raising some interesting points here.
I am also lookin into ublueooth. Mainly due to the fact that multiple roles should be supported concurrently.

Would you mind sharing your code?
It could be quite interesting also for me.

Looking at the BLE_temperature (peripheral) example, the temperature characteristics seems subscribable. I am going to test this.
Interestingly the central example does not include a method for subscribing. It might be possible to achieve this still with the given methods. For that I do unfortunately not yet have in depths understanding of low-level BLE interactions.

Have you considered creating an issue regrading the event codes on https://github.com/micropython/micropython?

redyellow
Posts: 12
Joined: Tue Jun 30, 2020 10:19 am

Re: ubluetooth - Question regarding event codes and subscribing to notifying characteristics

Post by redyellow » Tue Jun 30, 2020 12:03 pm

I have just tried ble_temperature (peripheral) with the NRFconnect app.
Notifications work.

Actually it should also work with the central code as you do not need a specific subscription.

From the documentation:

Code: Select all

BLE.gatts_notify(conn_handle, value_handle[, data])
Notifies a connected central that this value has changed and that it should issue a read of the current value from this peripheral.

If data is specified, then the that value is sent to the central as part of the notification, avoiding the need for a separate read request. Note that this will not update the local value stored.
In the ble_temperature example data is not specified so a separate read request would be needed.
However, if I understand correctly

Code: Select all

_IRQ_GATTC_NOTIFY = const(18)
is never triggered despite the peripheral sending notifications?

Once I have multiple ESP32 I will test this also myself.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ubluetooth - Question regarding event codes and subscribing to notifying characteristics

Post by jimmo » Tue Jun 30, 2020 1:02 pm

bart wrote:
Tue Jun 16, 2020 11:50 pm
Question: my software version (v1.12) still seems to be the latest stable build for ESP32, but the event codes in the documentation do not match the event codes working with this build. Where would I find any documentation regarding this change and when can I expect these new event codes to be usable in a stable build for ESP32?
The ubluetooth module is still under development, so there's still some things that are changing.

If you're using 1.12, then the documentation is here -- http://docs.micropython.org/en/v1.12/li ... tooth.html

The documentation by default links to "latest" which tracks the current in-development version (soon to be 1.13). https://docs.micropython.org/en/latest/ ... tooth.html

(Sorry I know this is confusing, it's on the TODO list to make this clearer in the published docs as to which version you're looking at).
redyellow wrote:
Tue Jun 30, 2020 10:33 am
Question: how do I subscribe to notify events for a certain characteristic? I did have a look at some example ble_temperature_central.py but it also doesn't seem to actively subscribe to the notifications.
To enable notifications you need to set the "notifications enabled" bit on the CCCB descriptor. (This is what nRF Connect does when you toggle notifications). https://www.bluetooth.com/wp-content/up ... ration.xml

Someone was actually talking about this recently on a github issue: https://github.com/micropython/micropython/issues/6185

For completeness, the ble_temperature_central.py demo should do this too, I will add that to my TODO list. (More accurately, the ble_temperature.py demo should not send notifications unless this bit is enabled).

redyellow
Posts: 12
Joined: Tue Jun 30, 2020 10:19 am

Re: ubluetooth - Question regarding event codes and subscribing to notifying characteristics

Post by redyellow » Wed Jul 01, 2020 7:37 pm

To enable notifications you need to set the "notifications enabled" bit on the CCCB descriptor. (This is what nRF Connect does when you toggle notifications).
Thank you!
How do find the value handle for the CCCB descriptor for a specific characteristic?
Do I simply look for the 0X2902 UUID following the actual characteristic?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ubluetooth - Question regarding event codes and subscribing to notifying characteristics

Post by jimmo » Thu Jul 02, 2020 6:33 am

redyellow wrote:
Wed Jul 01, 2020 7:37 pm
How do find the value handle for the CCCB descriptor for a specific characteristic?
Do I simply look for the 0X2902 UUID following the actual characteristic?
Yep, similar to how you find the characteristic value handle, you can use ble.gattc_discover_descriptors.

redyellow
Posts: 12
Joined: Tue Jun 30, 2020 10:19 am

Re: ubluetooth - Question regarding event codes and subscribing to notifying characteristics

Post by redyellow » Thu Jul 02, 2020 4:36 pm

I have noticed that scan results only works for me for UUID16.
Devices for with UUID32 or UUID128 are not detected.
I suspect that is a similar problem with the IRQs.

These constants are not correct for 1.12 are they?

Code: Select all

 Advertising payloads are repeated packets of the following form:
#   1 byte data length (N + 1)
#   1 byte type (see constants below)
#   N bytes type-specific data

_ADV_TYPE_FLAGS = const(0x01)
_ADV_TYPE_NAME = const(0x09)
_ADV_TYPE_UUID16_COMPLETE = const(0x3)
_ADV_TYPE_UUID32_COMPLETE = const(0x5)
_ADV_TYPE_UUID128_COMPLETE = const(0x7)
_ADV_TYPE_UUID16_MORE = const(0x2)
_ADV_TYPE_UUID32_MORE = const(0x4)
_ADV_TYPE_UUID128_MORE = const(0x6)
_ADV_TYPE_APPEARANCE = const(0x19)

What is the difference between UUID COMPLETE and UUID MORE?
EDIT: found a good explanation here: https://support.dialog-semiconductor.co ... st-128-bit
Last edited by redyellow on Thu Jul 02, 2020 8:12 pm, edited 1 time in total.

redyellow
Posts: 12
Joined: Tue Jun 30, 2020 10:19 am

Re: ubluetooth - Question regarding event codes and subscribing to notifying characteristics

Post by redyellow » Thu Jul 02, 2020 7:50 pm

The constants seems to be correct after checking the bluetooth specification again.
Nonetheless I am only returning empty services for the scanned devices except for the Corona warn characteristic of a nearby smartphone.

Code: Select all

[]
[]
[UUID16(0xfd6f)]
[]
[]
[]
[]
[]
[]
Is there some possibility of logging in MicroPython?
Up to now I am using print which is not really great.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ubluetooth - Question regarding event codes and subscribing to notifying characteristics

Post by jimmo » Fri Jul 03, 2020 5:52 am

redyellow wrote:
Thu Jul 02, 2020 7:50 pm
Nonetheless I am only returning empty services for the scanned devices except for the Corona warn characteristic of a nearby smartphone.
I'm not quite sure what question you're asking. Earlier in the thread we were talking about finding the CCCB descriptor on a gatt server, but sounds now like we're talking about extracting fields from an advertising payload?

Can you maybe post code that doesn't do what you expect.

redyellow
Posts: 12
Joined: Tue Jun 30, 2020 10:19 am

Re: ubluetooth - Question regarding event codes and subscribing to notifying characteristics

Post by redyellow » Fri Jul 03, 2020 6:02 am

Sure you are right.

The reason why I posted it here was that I thought it was related to version and documentation differences.
I will start a new thread for the problem.

Post Reply