Python Forum
Read 2 Value with bleak via BLE notify from ESP32
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read 2 Value with bleak via BLE notify from ESP32
#1
Hello, when I read out 2 characteristics via Bleak and BLE, via the command:

async def notification_handler(sender, data, callback):
     spo2_value = numpy.frombuffer(data, dtype=numpy.uint32)

     print("Red values:")
     print(spo2_value)

     # Update the label text
     callback(spo2_value)
... and

async def notification_handler(sender, data, callback):
     red_value = numpy.frombuffer(data, dtype=numpy.uint32)

     print("Red values:")
     print(red_value)

     # Update the label text
     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
Reply
#2
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.
Reply
#3
(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:

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:
        # Abonniere beide Charakteristiken
        await client.start_notify("beb5483e-36e1-4688-b7f5-ea07361b26a8", notification_handler)
        await client.start_notify("beb5483e-36e1-4688-b7f5-ea07361b26a9", notification_handler)
        # Warte auf Daten
        while True:
            await asyncio.sleep(1)


async def notification_handler(sender, data):
    # Daten in numpy-Array umwandeln
    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:

async def notification_handler(sender, data):
    # Daten in numpy-Array umwandeln
    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?
Reply
#4
What happens if you run this?
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)  # You assigned this a name.  Use the name.
        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
Reply
#5
I assume that the last tip should solve the problem
Reply
#6
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 ?
Reply
#7
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.
Reply
#8
(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.
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

    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:

class BLEThread(threading.Thread):
    def __init__(self, address):
        threading.Thread.__init__(self)
        self.address = address
Then i make a start threading function:

def start_threads():
    ble_thread = BLEThread(address)
    tkinter_thread = TkinterThread()

    ble_thread.start()
    tkinter_thread.start()
and start them

if __name__ == '__main__':
    start_threads()
Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [ESP32 Micropython]Total noob here, I don't understand why this while loop won't run. wh33t 9 1,780 Feb-28-2023, 07:00 PM
Last Post: buran
  How to continuously receive messages until specified to stop using Bleak jacobbreen25 3 2,187 Dec-28-2022, 04:25 PM
Last Post: jacobbreen25
  Help with bleak - how to send test to BLE device? korenron 1 1,757 Aug-28-2022, 11:28 PM
Last Post: Larz60+
  bleak library RuntimeError: This event loop is already running alice93 3 4,124 Sep-30-2021, 08:06 AM
Last Post: alice93
  Notify when a new file lands in a directory fioranosnake 1 1,920 Jul-16-2020, 12:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020