async-mqtt-client: Client stuck on MQTT reconnect

Hello! Sometimes, my device disconnects from the MQTT broker and never reconnects again while the sketch is running. If I reset the device, it can connect again without any problems.

I have a MWE and extensive logs. The logfile is pretty big, so I separated the part from when it failed: https://pastebin.com/SPdQK3z5

The #include "M5StickPlus.h" and M5.begin(false, true, true); in the MWE are there because I’m using a M5 Stick-C Plus for testing, which has a ESP32-PICO-D4 inside.

I can provide more info if necessary. Thanks in advance!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 34 (12 by maintainers)

Most upvoted comments

I have a similar occurrence when if the broker is down for a while and comes back up (say >5mins when doing a server upgrade) AysncMQTTClient fails to reconnect. I’ll see if I can create some small test code to reproduce it.

@cyber-junkie9 So far, it looks like it - I have three devices working since Thursday - but I’m going to keep monitoring them for a few more days just to be sure.

@bertmelis Thanks!!, one question, the “onDisconnect” callback it´s called each time the client fails to connect? (and obviosly after it had a succesull connection that drops) right?, the point of my question it´s that i guess i could call the “connect” method only when the “onDisconnect” it´s triggered. Currently i´m calling Connect every 5 seconds, independant on the “onDisconnect” callback (i do this with a freertos timer, that only it´s stoped once the “onConnect” callback it´s called)

For reference: only try to reconnect if the previous attempt has failed. The broker disconnects the oldest client when a new one with the same ID tries to connect.

This reconnect loop could be a timing issue.

@GioTB I just realized something. From your comments, I infer that you get your onDisconnect handler called several times; as in, the device attempts to reconnect, it fails, and this loop repeats forever unless you reboot. And the fix you mention can be implemented in the sketch side of things, rather than patching the library. Am I correct?

For me, the onDisconnect handler only gets called once, I call connect again, and that single reconnection is what happens forever unless reboot.

@rousveiga Yes!, precisely that!, so our reconection loop it´s different, another thing that i do it´s to activate a timer wich attempts to reconnect every 5 seconds, so the “connect” it´s called several times, in the device that i have on the field sometimes, it takes up to 14 tries until it reconnects, but this could be for the internet connection, or some other problems, i can´t say it´s cause of the library, and the most important thing it´s that after a while it manages to connect back to the broker. Worth noting that once i changed to the develop branch i haven´t had the issue anymore, i even put a ESP32 disconnecting every 20 seconds so it would attempt the reconnect on it´s own, and it always reconnect with no problem.

@rousveiga I remember reading something (probably on heise online, a German computer magazine, but I can’t find the article) that there was a problem with a recent update to the mosquitto add-on for HA. People had to revert to a previous version.

I’m using HA, but my mosquitto runs on a separate raspberry.

add a counter, so if it goes further than a certain amount of reconnection attempts the node should restart. i had to implement that on my code so i wouldn´t lose the device for ever (my main device it´s about 2 hrs far from me)

@GioTB Might do this myself, my devices are away from me as well.