knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.84k stars 1.47k forks source link

ESP32 connection failed (rc = -2) after 20 minutes aprox and can´t reconnect. #947

Open IgnacioCipo opened 2 years ago

IgnacioCipo commented 2 years ago

I'm using ESP32 with Platformio in Arduino code. I am sending information with a SIM800 through MQTT using Pubsubclient and TinyGSM to a .net broker using FreeRTOS. ESP32 starts and all works fine, I have a function to check if the MQTT connection is still ON, and if it disconnects, reconnects it. This is working, but after some time the MQTT connection is lost and it is not able to reconnect anymore. The only way to connect the ESP32 back to MQTT is rebooting it. I´ve checked if the problem comes from the server side where the MQTT broker is, but it is not. When ESP32 is trying to reconnect, I´ve reset the server and the problem persist. I´ve checked SIM800 connectivity and it algo is good. This is my task that checks for MQTT connection each half minute. image

This are the function calling to check MQTT: image

image

I´ve been dealing with this problem too much time. I would appreciate any tips. Thanks!

LaZorraTech commented 2 years ago

Have you tried setting your KeepAlive to somethjng longer? Perhaps 45?

Also, I was having issues with PubSub and the current version of espressif32. In the platformio.ini file, I set my platform to:

platform = espressif32@^3.5.0

and my connection issues resolved. Good luck!

IgnacioCipo commented 2 years ago

Have you tried setting your KeepAlive to somethjng longer? Perhaps 45?

Also, I was having issues with PubSub and the current version of espressif32. In the platformio.ini file, I set my platform to:

platform = espressif32@^3.5.0

and my connection issues resolved. Good luck!

Hi! I´ve tried setting KeepAlive to bigger values, but not changes. I will try changing my platformio.ini file, thank you for the advice!

tablatronix commented 2 years ago

Same, will not reconnect..

Oct 3 19:45:35 ESP_env 192.168.20.58 ESP:[INFO]- [MQTT] try client re-connect Oct 3 19:45:38 ESP_env 192.168.20.58 ESP:[ERROR]- [ERROR] [MQTT] failed, rc=-2

simogaspa84 commented 1 year ago

did you get any news on this @IgnacioCipo ?

tablatronix commented 1 year ago

Still no idea what the root cause is, really starting to piss me off

Screen Shot 2022-12-02 at 11 21 01 PM

ZanzyTHEbar commented 1 year ago

Same issue, MQTTBox and my other servers can connect to the broker just fine, esp can not.

tablatronix commented 1 year ago

I have tried to debug this but it seems to be deep in the wificlient library and since these are all abstracted classes, I could not really follow it to where it was failing. I basically gave up as I spent too much time on it. Someone with knowledge of these classes might have better luck tracing it, or have actual debuggers setup.

I have a suspicion its a socket issue in the esp lib

ZanzyTHEbar commented 1 year ago

I ended up moving away from PubSubClient in favour of this library https://github.com/paclema/MQTTClient it works very well, and is even async on the ESP32. So far, i am really enjoying working with it, especially since it uses the espressif native mqtt api's.

tablatronix commented 1 year ago

@ZanzyTHEbar checking it out now, thanks!