with a function that is called only on MQTT disconnect by the Async-MQTT-Client KeepAlive functionality.
Create a timer and register an onDisconnect callback function that manages the (re)connection of the MQTT client.
It will be called by the Asyc-MQTT-Client KeepAlive function if a connection loss is detected.
The timer is then started and will start a function to reconnect MQTT after 2 seconds
Improvement of MQTT Re-Connect
Replace the polling task
that is periodically calling the function
with a function that is called only on MQTT disconnect by the Async-MQTT-Client KeepAlive functionality.
Create a timer and register an onDisconnect callback function that manages the (re)connection of the MQTT client. It will be called by the Asyc-MQTT-Client KeepAlive function if a connection loss is detected. The timer is then started and will start a function to reconnect MQTT after 2 seconds
Async-MQTT-Client will call the callback
If WiFi is not available the timer will be restarted to try again after 2 seconds.
The functionality was successfully tested with
In both cases the MQTT connection was established again.
Tests with mqttGuard have yet to be done.