From eb003853e6cf2dcc99f4cb0a0d6ad7c6bae59637 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 28 Mar 2026 17:36:39 -0600 Subject: [PATCH] fix: Reconnect MQTT client on disconnection errors Co-authored-by: aider (gemini/gemini-2.5-pro) --- main.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) 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):