Posts: 17
Threads: 7
Joined: May 2023
May-02-2023, 12:17 PM
(This post was last modified: May-02-2023, 12:17 PM by Nietzsche.)
Hello, when I read out 2 characteristics via Bleak and BLE, via the command:
1 2 3 4 5 6 7 8 |
async def notification_handler(sender, data, callback):
spo2_value = numpy.frombuffer(data, dtype = numpy.uint32)
print ( "Red values:" )
print (spo2_value)
callback(spo2_value)
|
... and
1 2 3 4 5 6 7 8 |
async def notification_handler(sender, data, callback):
red_value = numpy.frombuffer(data, dtype = numpy.uint32)
print ( "Red values:" )
print (red_value)
callback(red_value)
|
Then only one is displayed to me, since both have the same name: notification_handler. If I rename it to notification_handler2, for example, the function no longer works. What can I do there?
Link to the library bleak: https://github.com/hbldh/bleak
GitHub
Posts: 6,826
Threads: 20
Joined: Feb 2020
The two functions you posted are identical. If they really do what you want, you only need one function (which is what you got when you gave them the same name). Same name or not, the functions do exactly the same thing and there is no need for a second.
You need to post your code for anyone to be able to help you. Also describe exacty what you are trying to accomplish.
Posts: 17
Threads: 7
Joined: May 2023
(May-02-2023, 03:09 PM)deanhystad Wrote: The two functions you posted are identical. If they really do what you want, you only need one function (which is what you got when you gave them the same name). Same name or not, the functions do exactly the same thing and there is no need for a second.
You need to post your code for anyone to be able to help you. Also describe exacty what you are trying to accomplish.
Hi,
i have an ESP 32 and here i read a Sensor with 2 value, i collect 100 readings in an array. To the 2 value i add a Characteristic and then i start a notify after collect 100 readings. In my python app i want to print the value with the titel. But at the moment i can only print 1 array with the function i postet above or i can print all together.
Here is my ESP32 Code: https://github.com/ImmanuelT8/Smart-Heal...te_aus.ino
This is the code to get 1 sensor value:
https://github.com/ImmanuelT8/Smart-Heal..._notify.py
With this Code i get both arrays but together and i can't add them a titel:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import asyncio
from bleak import BleakClient
import numpy
address = "CC:50:E3:9C:15:02"
MODEL_NBR_UUID = "beb5483e-36e1-4688-b7f5-ea07361b26a8"
MODEL_NBR_UUID2 = "beb5483e-36e1-4688-b7f5-ea07361b26a9"
async def read_data(address):
async with BleakClient(address) as client:
await client.start_notify( "beb5483e-36e1-4688-b7f5-ea07361b26a8" , notification_handler)
await client.start_notify( "beb5483e-36e1-4688-b7f5-ea07361b26a9" , notification_handler)
while True :
await asyncio.sleep( 1 )
async def notification_handler(sender, data):
values = numpy.frombuffer(data, dtype = numpy.uint32)
print (values)
async def main(address):
await read_data(address)
asyncio.run(main(address))
|
This code does not work:
1 2 3 4 5 6 7 8 9 |
async def notification_handler(sender, data):
values = numpy.frombuffer(data, dtype = numpy.uint32)
if sender = = "beb5483e-36e1-4688-b7f5-ea07361b26a8" :
print ( "SPO2 Werte:" )
print (values)
elif sender = = "beb5483e-36e1-4688-b7f5-ea07361b26a9" :
print ( "IR Werte:" )
print (values)
|
Is this information enough?
Posts: 6,826
Threads: 20
Joined: Feb 2020
What happens if you run this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import asyncio
from bleak import BleakClient
UID_1 = "beb5483e-36e1-4688-b7f5-ea07361b26a8"
UID_2 = "beb5483e-36e1-4688-b7f5-ea07361b26a9"
async def read_data(address):
async with BleakClient(address) as client:
await client.start_notify(UID_1, notification_handler)
await client.start_notify(UID_2, notification_handler)
while True :
await asyncio.sleep( 1 )
async def notification_handler(sender, data):
print ( "Sender:" , sender)
async def main(address):
await read_data(address)
asyncio.run(main( "CC:50:E3:9C:15:02" ))
|
Do you see it printing the both UUID's? Do you only see one? If so, which one?
Nietzsche likes this post
Posts: 17
Threads: 7
Joined: May 2023
May-03-2023, 05:29 AM
(This post was last modified: May-03-2023, 05:32 AM by Nietzsche.)
I get the code running, thanks for help!
Btw if i want to get the data into a table, how would you handle it? At the moment i have 2 threads, one for the BLE and the other for my tkinter window, but how i can show the data in a tabel? First safe them into a svg or mySQL ?
Posts: 6,826
Threads: 20
Joined: Feb 2020
Could I impose upon you to answer the questions I asked in my post? I don't really want to try to duplicate the experiment myself and am curious about what you learned. It could be useful for others trying to use the bleak library. I have serious doubts about your threading. It makes no sense to make a thread for tkinter. You need 2 threads, not 3. Use the main thread for tkinter and create a new thread to read BT.
Posts: 17
Threads: 7
Joined: May 2023
May-04-2023, 06:42 AM
(This post was last modified: May-04-2023, 06:42 AM by Nietzsche.)
(May-03-2023, 04:00 PM)deanhystad Wrote: Could I impose upon you to answer the questions I asked in my post? I don't really want to try to duplicate the experiment myself and am curious about what you learned. It could be useful for others trying to use the bleak library. I have serious doubts about your threading. It makes no sense to make a thread for tkinter. You need 2 threads, not 3. Use the main thread for tkinter and create a new thread to read BT.
Hi,
to get the value from 2 parameters with notify you need to make 2 handle_notify functions and 1 read_data.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
async def handle_uuid1_notify( self , sender, data):
spo2_values = numpy.frombuffer(data, dtype = numpy.uint32)
print ( "Red: {0}" . format (spo2_values))
for spo2_value in spo2_values:
table.add_row([spo2_value, ""])
print_table()
async def handle_uuid2_notify( self , sender, data):
ir_values = numpy.frombuffer(data, dtype = numpy.uint32)
print ( "IR: {0}" . format (ir_values))
for ir_value in ir_values:
table.add_row(["", ir_value])
print_table()
|
and
1 2 3 4 5 6 7 8 |
async def read_data( self ):
UUID1 = "beb5483e-36e1-4688-b7f5-ea07361b26a8"
UUID2 = "beb5483e-36e1-4688-b7f5-ea07361b26a9"
async with BleakClient( self .address) as client:
await client.start_notify(UUID1, self .handle_uuid1_notify)
await client.start_notify(UUID2, self .handle_uuid2_notify)
while True :
await asyncio.sleep( 1 )
|
Because i want a GUI i had make multithreading, so the functions are in the class BLEThread:
1 2 3 4 |
class BLEThread(threading.Thread):
def __init__( self , address):
threading.Thread.__init__( self )
self .address = address
|
Then i make a start threading function:
1 2 3 4 5 6 |
def start_threads():
ble_thread = BLEThread(address)
tkinter_thread = TkinterThread()
ble_thread.start()
tkinter_thread.start()
|
and start them
1 2 |
if __name__ = = '__main__' :
start_threads()
|
Posts: 17
Threads: 7
Joined: May 2023
May-04-2023, 09:31 PM
(This post was last modified: May-04-2023, 09:31 PM by Nietzsche.)
(May-02-2023, 06:41 PM)deanhystad Wrote: What happens if you run this?
Sry for my lat reply to your question. If i run this i get this:
Quote:Sender: beb5483e-36e1-4688-b7f5-ea07361b26a8 (Handle: 41): Unknown
Sender: beb5483e-36e1-4688-b7f5-ea07361b26a9 (Handle: 44): Unknown
Sender: beb5483e-36e1-4688-b7f5-ea07361b26a8 (Handle: 41): Unknown
Sender: beb5483e-36e1-4688-b7f5-ea07361b26a9 (Handle: 44): Unknown
If this should be the BLE Devie name it should be ESP:
ESP_SPO2 (CC:50:E3:9C:15:02)
I will try to restart the ESP and check it again.
|