knolleary / pubsubclient

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

connected() returns true when internet is no longer available and client should be disconnected #951

Open dsilletti opened 2 years ago

dsilletti commented 2 years ago

After a successful connection to the MQTT broker on the internet, the connected() function returns true even if the internet is no longer available but the WiFi is connected, causing the MQTT publish and loop functions to hang and reset the board due to the watchdog (configured to 30 seconds)

I also have setSocketTimeout to 1 and setKeepAlive to 15 or 5 seconds but it doesn't help.

pubsubclient v2.8 ESP32 Arduino core 2.0.3

WiFiClient espClient;
PubSubClient MQTTclient;
MQTTclient.setClient(espClient);
MQTTclient.setKeepAlive(15);
MQTTclient.setSocketTimeout(1);
dsilletti commented 2 years ago

P.S. I understand that keepalive is responsible for disconnecting in this case, so client.connected() will return true up to ~2*keepalive time, but the publish and mqtt loop functions need a timeout to avoid blocking the entire core for so long (> 30s in my case)

GDev-4664 commented 8 months ago

So what's the solution?

dsilletti commented 8 months ago

@GDev-4664 The workaround I applied is to create another task to periodically check the MQTT connection status and handle reconnection, this way the main task will not be blocked.

ozersenol commented 6 months ago

@dsilletti could you please share your solution?