5

I want to reconnect an MQTT client to a server on failure.

I am using uqmtt.robust for the reconnection: it is working for some failures but not handling internet disconnections. I am thinking about doing like:

while True:
 if c.isConnected():# how to check this?
  c.wait_msg()
 else:
  c=MQTTClient(params)
  c.connect() #reconnect trial

I tried to solve by reading PINGRESP but pings are in interval so I couldn't find whether the connection is live or not.

dda
  • 6,030
  • 2
  • 25
  • 34
Yugandhar Chaudhari
  • 3,831
  • 3
  • 24
  • 40

1 Answers1

4

You can send PING at a constant interval and set a flag to False as soon as you sent the PING.If you receive a PINGRESP set the flag back to true.Check the flag when you are about to send the next PING.If the flag is still False means you don't have received a PINGRESP and you can assume the connection is lost.Actually I have modified the umqtt simple library to support this here is the git https://github.com/AntonisKekempanos/SonoffMicropythonMQTT.