fditz
fditz

Reputation: 882

Micropython and Bluetooth on ESP32

I know the support for bluetooth is still under development but it seems to cover everything I need at this point so I decided to give it a try.

I just want to simulate reading from a source of data (a EKG machine) so I came up with this code:

from ubluetooth import BLE
from ubluetooth import FLAG_READ, FLAG_NOTIFY, FLAG_WRITE
import time

ekg_data = [-305,-431,-131,440 ,1158,1424,1445,1623,1500,1018,142 ,-384,-324,-414,-77 ,334 ,-372,-154,366 ,7613,1461,1403,6133,-179,-381,-224,-135,-168,-208,-187,-181,-180,-160,-160,-151,-150,-151,-138,-141,-128,-118,-106,-798,-677,-430,-253,-122,98  ,133 ,281 ,354 ,390 ,519 ,475 ,558 ,565 ,533 ,593 ,458 ,377 ,107 ,-335,-719,-116,-129,-132,-131,-119,-122,-111,-106,-105,-935,-971,-877,-841,-841,-725,-757,-660,-641,-660,-554,-592,-496,-473,-486,-387,-431,-350,-364,-347,-208,-365,-362]


bt = BLE()
bt.active(True)


print('----')
print(bt.config('mac'))
print(bt.config('gap_name'))

HR_UUID = bluetooth.UUID(0x180D)
HR_CHAR = (bluetooth.UUID(0x2A37), bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY,)
HR_SERVICE = (HR_UUID, (HR_CHAR,),)


SERVICES = (HR_SERVICE,)
((ekg,),) = bt.gatts_register_services(SERVICES)


# bt.gap_advertise(100, 'MicroPython EKG')


count = 0
while True:
    if count >= len(ekg_data):
        count = 0

    bt.gatts_write(ekg, ekg_data[count].to_bytes(2, 'big'))
    print(ekg_data[count])
    time.sleep_ms(1000)
    count += 1

Now the code compiles and runs (I can see the output on the console) but I cannot find the device in my bluetooth app (I am using the nordic app)

Does anyone with more knowledge on that area can tell me if I am overlooking something? I tried to take the advertising off and on because I thought I might be overriding something with it but that didn't help too...

Upvotes: 2

Views: 8430

Answers (1)

ahmed ashref
ahmed ashref

Reputation: 155

I think your code is missing multiple things. First, you are not setting (irq) which is (Event Handling) for Micropython(As you can see from the docs or in their Github codes. Also, I can't see you setting the buffer or any stuff like that, please revise the examples for what you asking here. Good job btw.

Upvotes: 0

Related Questions