I never intend for the script to ever complete execution. I've wrapped it in a little loop now. # The main code starts running from here
async def mqtt_loop():
async with Client(hostname="127.0.0.1",port=1883,username=MQTT_USERNAME,password=MQTT
_PASSWORD) as client:
async with client.unfiltered_messages() as messages:
# Subscribe to add the cameras we are interested in
for camera_serial in inventory:
print(f"Subscribing to camera {camera_serial}")
await client.subscribe(f"/merakimv/{camera_serial}/0")
async for message in messages:
asyncio.ensure_future(mqtt_on_message(message))
async def main():
reconnect_interval = 3
...
# Start the main loop. If an error happens, reconnect
while True:
try:
await mqtt_loop()
except MqttError as error:
print(f'Error "{error}". Reconnecting in {reconnect_interval} seconds.')
finally:
await asyncio.sleep(reconnect_interval)
if __name__ == "__main__":
# Execute only if run as a script
asyncio.run(main())
... View more