diff --git a/main.py b/main.py index 477a24c..19f1765 100644 --- a/main.py +++ b/main.py @@ -66,14 +66,20 @@ async def process_mqtt(message, p): async def fetch_mqtt(p): await asyncio.sleep(3) - async with Client( - hostname='172.17.17.181', - port=1883, - ) as client: - await client.subscribe('#') - async for message in client.messages: - loop = asyncio.get_event_loop() - loop.create_task(process_mqtt(message, p)) + while True: + try: + async with Client( + hostname='172.17.17.181', + port=1883, + ) as client: + logging.info('MQTT client connected') + await client.subscribe('#') + async for message in client.messages: + loop = asyncio.get_event_loop() + loop.create_task(process_mqtt(message, p)) + except aiomqtt.MqttError as e: + logging.warning('MQTT error: %s. Reconnecting in 5 seconds...', e) + await asyncio.sleep(5) async def manage_display(disp):