Mar-05-2023, 10:42 PM
(This post was last modified: Mar-06-2023, 12:25 AM by deanhystad.)
Maybe your writes/notify fails. If you only get one data back, the info array is only 4 long, and info[4] is out of range.
Why aren't you saving your data in your delegate? Something like this:
Why aren't you saving your data in your delegate? Something like this:
class MyDelegate(DefaultDelegate): def __init__(self): super().__init__() self.dd03_updates = 0 self.dd04_updates = 0 # If you need data as a dictionary, may as well save it as a dictionary. self.data = { "volts": None, "amps": None, "capacity": None, "remain": None, "cell1": None, "cell2": None, "cell3": None, "cell4": None, } def handleNotification(self, cHandle, data): hex_data = binascii.hexlify(data) if b'dd04' in hex_data: self.dd04_messages += 1 self.data.update(zip( ("volts", "amps", "capacity", "remain"), struct.unpack_from('>HHHH', data, 4) )) elif b'dd03' in hex_data: self.dd03_messages += 1 self.data.update(zip( ("cell1", "cell2", "cell3", "cell4"), struct.unpack_from('>HHHH', data, 4) )) def __str__(self): return "\n\t".join(( f'Messages dd03:{self.dd03_messages}, dd04{self.dd04_messages}', *[f'{key:10} = {value}' for key, value in self.data.items()] )) delegate = MyDelegate() bms.setDelegate(delegate) # setup bt delegate for notifications . . . while True: bms.writeCharacteristic(0x15, b'\xdd\xa5\x03\x00\xff\xfd\x77', False) bms.waitForNotifications(5) bms.writeCharacteristic(0x15, b'\xdd\xa5\x04\x00\xff\xfc\x77', False) bms.waitForNotifications(5) print(delegate) ret = mqtt.publish(topic, payload=json.dumps(delegate.data), qos=0, retain=False)