Check mqtt connection state ESP32


#1

Hi,

I’m implementing a reconnection method in my application for Wi-FI and mqtt. So far the reconnection method for Wi-Fi works, but how can I check if the thing is still connected to the mqtt broker, if not, re-establish the connection?

something like:

if thing.mqtt.is_connected() == false:
thing.mqtt.connect()
thing.on_shadow_request(shadow_callback)
thing.mqtt.loop()

BR


#2

Hello @Marcel,

the mqtt library automatically handles mqtt reconnection which is detected when the reception channel is somehow interrupted and no incoming activity has been registered lately (no response to ping requests which are sent periodically).

Due to current automatic reconnection method I do not recommend to also handle it manually :slight_smile:


#3

Ok great, thank you. My reconnection procedure now works (without touching mqtt reconnect).