Mar-06-2023, 03:16 PM
(This post was last modified: Mar-06-2023, 03:16 PM by deanhystad.)
When printing changes how things run, I first think of some sort of race condition. But you are waiting 5 seconds for a response, so that isn't likely. I think it more likely that printing didn't make any difference at all and it was just chance, and maybe tainted observation, that lead you to believe that printing was "fixing" the problem. That happens to me all the time. Your mind creates patterns regardless of the data. Sometimes this is insight and sometimes it is "That cloud looks like a bunny." You should really check the value retuned by the waitForNotifications(). If it is False, nothing was received.
I cannot test the code, so I am not surprised there are some bugs. This particular bug is a cut/paste error. This does not belong.
I will try to describe the code.
You sublcassed bluepy.btle.DefaultDelegate to make your MyDelegate class. Then you provided a custom handleNotification() method to use when a notification is received. In addition to custom methods, you can also provide custom attributes. Instead of making a global variable and having the delegate copy data to the global variable, it makes more sense to have the delegate keep the data. That is what I was doing by defining a bunch of attributes in the __init__() method. When MyDelegate's handleNotification() method is called, it stores the notification data in the delegate's instance attributes.
I saw that elsewhere in your code you were publishing a dictionary. I thought it made sense to save the notification data as a dictionary. That is why I did this in the __init__() method.
This is just a shorthand way of updating a dictionary.
Let me know if you have more questions.
I cannot test the code, so I am not surprised there are some bugs. This particular bug is a cut/paste error. This does not belong.
f'volts = {self.volts}',I updated my original post.
I will try to describe the code.
You sublcassed bluepy.btle.DefaultDelegate to make your MyDelegate class. Then you provided a custom handleNotification() method to use when a notification is received. In addition to custom methods, you can also provide custom attributes. Instead of making a global variable and having the delegate copy data to the global variable, it makes more sense to have the delegate keep the data. That is what I was doing by defining a bunch of attributes in the __init__() method. When MyDelegate's handleNotification() method is called, it stores the notification data in the delegate's instance attributes.
I saw that elsewhere in your code you were publishing a dictionary. I thought it made sense to save the notification data as a dictionary. That is why I did this in the __init__() method.
self.data = { "volts": None, "amps": None, "capacity": None, "remain": None, "cell1": None, "cell2": None, "cell3": None, "cell4": None, }The data starts out as None, but as notifications are received, the dictionary is updated.
This is just a shorthand way of updating a dictionary.
self.data.update(zip( ("volts", "amps", "capacity", "remain"), struct.unpack_from('>HHHH', data, 4) ))It is the same as doing this:
volts, amps, capacity, remain= struct.unpack_from('>HHHH', data, 4) self.data["volts"] = volts self.data["amps"] = amps self.data["capacity"] = capacity self/data["remain"] = remainAnd this is called unpacking:
volts, amps, capacity, remain= struct.unpack_from('>HHHH', data, 4)The first value returned by struct.unpack_from() is assigned to volts, the second to amps, the third to capacity, and fourth to remain. Packing and unpacking are very useful in Python. Well worth mastering.
Let me know if you have more questions.