Reconnect if the BLE connection dies

This commit is contained in:
Odd Stråbø 2021-11-02 23:48:06 +01:00
parent 2578fdca75
commit 06453f90e1
1 changed files with 18 additions and 25 deletions

View File

@ -230,24 +230,13 @@ def write(fh, data):
dlgt = Delegate() dlgt = Delegate()
prev = time.time() - INTERVAL prev = time.time() - INTERVAL
with btle.Peripheral(MAC).withDelegate(dlgt) as dev:
# for svc in dev.services:
# print(svc, svc.uuid)
# print(dir(dev)) while True:
try:
# wd = dev.getServiceByUUID(write_service) with btle.Peripheral(MAC).withDelegate(dlgt) as dev:
# print(wd)
wd = dev.getCharacteristics(uuid=write_device)[0] wd = dev.getCharacteristics(uuid=write_device)[0]
# print(cs)
# print(dir(cs))
# print(wd.write(b'\xf0\x03\x00\x0c\x00\x08\x91\xd1'))
# rd = dev.getCharacteristics(uuid=read_device)[0]
# print(rd.read())
while True: while True:
dev.waitForNotifications(1) dev.waitForNotifications(1)
@ -267,3 +256,7 @@ with btle.Peripheral(MAC).withDelegate(dlgt) as dev:
# write(wd, CMD_DISABLE_LOAD) # write(wd, CMD_DISABLE_LOAD)
# else: # else:
# write(wd, CMD_ENABLE_LOAD) # write(wd, CMD_ENABLE_LOAD)
except btle.BTLEDisconnectError:
print(datetime.datetime.utcnow().isoformat(" "), "ERROR: Disconnected")
time.sleep(1)