jeelabs / el-client

Arduino client for esp-link's REST, MQTT, and command interface
BSD 2-Clause "Simplified" License
83 stars 46 forks source link

MQTT: reconnect after broken connection? #52

Open flipsa opened 5 years ago

flipsa commented 5 years ago

Hey TVE,

Thanks for esp-link and el-client, your work is much appreciated!

I've been using esp-link (v3.2.47-g9c6530d) succesfully for an Arduino (Mega) project I'm working on and just added MQTT support through el-client (v3.0.alpha0) to publish status and sensor data, which then gets picked up by an mqtt broker running in a Openhab (home automation) instance to display nice graphs from the sensor data.

The problem I'm facing is identical or similar to #8: sometimes the MQTT connection between the el-client and the Openhab instance goes down, usually only for seconds or sometimes in the range of a few minutes. In the serial log I can see that the mqttDisconnect callback is being called a few times (usually twice or three times within a short time like a minute or so) but then the callback is not being called again afterwards, or at least I don't see any "MQTT disconnected" messages on the serial monitor.

If the connection to Openhab is re-established before that, then it resumes publishing status data. However, if the issue persists a bit longer (a few minutes), the mqtt connection is never re-established from the Arduino's side of things, BUT, the esp-link MQTT status info (rssi and heap) will reconnect just fine and I can receive the status data in Openhab without any issues.

How can I make sure that the Arduino also notices that the connection is working again and then start publishing the mqtt data again? I tried manually calling the esp.Sync(); function in the MQTT disconnect callback, but that reliably crashes the Arduino.

Any help is appreciated...

Edit: Added version information for esp-link and el-client.