I have tried the following code:
Code: Select all
# main function - called by asyncio.run
async def main(client):
x = 1
async def connection(x):
try:
await client.connect()
x = 100
return x
except OSError as e:
print('Connection failed.', e)
x += 1
return x
while x < 10:
print(f'Connection tries = {x}')
x = await connection(x)
if x < 11:
globs.run_program = False
Trying to connect for 1 time
Connection failed. -1
Trying to connect for 2 time
Connection failed. -1
Trying to connect for 3 time
Connection failed. Wifi Internal Error
Trying to connect for 4 time
Connection failed. Wifi Internal Error
Trying to connect for 5 time
Connection failed. Wifi Internal Error
Trying to connect for 6 time
Connection failed. Wifi Internal Error
Trying to connect for 7 time
Connection failed. Wifi Internal Error
Trying to connect for 8 time
Connection failed. Wifi Internal Error
Trying to connect for 9 time
Connection failed. Wifi Internal Error
Now I tried to search the mqtt_as.py to find this message but I have not located it. In any case I suspect there is a better way to proceed. Any suggestion on a better way of ensuring the mqtt connection is only made once the mqtt broker is actually up and running would be appreciated.