If I try to start my esp32 when the mqtt server is offline the connection to mqtt will crash with an exception (no connection), the app cannot start at all.
If mqtt is lost later it can recover, that works perfectly, but you know, the situation is that after a power outage, the ESP32 boots up much faster than the router or the MQTT server.
Currently I have a simple workaround, trying to connect in an endless loop, like this (see example 2.3):
async def connect(self):
"""connect to broker"""
while True:
try:
await self.client.connect()
for coroutine in (self.up, self.messages):
asyncio.create_task(coroutine())
return
except Exception as e:
sys.print_exception(e)
syslog.error("cannot connect to mqtt broker")
asyncio.sleep_ms(10)
But I think it is ugly and resilience is already there (somewhere).
If I try to start my esp32 when the mqtt server is offline the connection to mqtt will crash with an exception (no connection), the app cannot start at all.
If mqtt is lost later it can recover, that works perfectly, but you know, the situation is that after a power outage, the ESP32 boots up much faster than the router or the MQTT server.
Currently I have a simple workaround, trying to connect in an endless loop, like this (see example 2.3):
But I think it is ugly and resilience is already there (somewhere).