fix: Reconnect MQTT client on disconnection errors

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2026-03-28 17:36:39 -06:00
parent 7567214289
commit eb003853e6

22
main.py
View File

@@ -66,14 +66,20 @@ async def process_mqtt(message, p):
async def fetch_mqtt(p): async def fetch_mqtt(p):
await asyncio.sleep(3) await asyncio.sleep(3)
async with Client( while True:
hostname='172.17.17.181', try:
port=1883, async with Client(
) as client: hostname='172.17.17.181',
await client.subscribe('#') port=1883,
async for message in client.messages: ) as client:
loop = asyncio.get_event_loop() logging.info('MQTT client connected')
loop.create_task(process_mqtt(message, p)) 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): async def manage_display(disp):