knolleary / pubsubclient

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

Internet disconnected, client.publish() becomes false but client.state() is MQTT_CONNECTED #712

Open MuhammadOsaid opened 4 years ago

MuhammadOsaid commented 4 years ago

ESP8266 is connected to WiFi and connection to server is successfully established. I am using #include <ESP8266WiFi.h> #include <PubSubClient.h>

For testing purpose the Internet is deliberately switched off from the WiFi router (WiFi is still connected). client.publish() rightfully returns false but when the client state is checked using client.state() it returns 0 i.e. MQTT_CONNECTED ref:https://pubsubclient.knolleary.net/api.html#state

This keeps on happening four times. The fifth time client.state() returns MQTT_CONNECTION_TIMEOUT

Kindly look into this.

daniel3514 commented 4 years ago

ESP8266 is connected to WiFi and connection to server is successfully established. I am using #include <ESP8266WiFi.h> #include <PubSubClient.h>

For testing purpose the Internet is deliberately switched off from the WiFi router (WiFi is still connected). client.publish() rightfully returns false but when the client state is checked using client.state() it returns 0 i.e. MQTT_CONNECTED ref:https://pubsubclient.knolleary.net/api.html#state

This keeps on happening four times. The fifth time client.state() returns MQTT_CONNECTION_TIMEOUT

Kindly look into this.

I have a similar environment, when connected, no problem, if I physically disconnect the MQTT server, state () and connected () continue to mark connected, it does not mark time out

cl779 commented 4 years ago

Hello, I also discovered this kind of problem:

-ESP32 establish a MQTT connection to the MQTT broker -the ESP32 send a MQTT Message every 3 seconds -Now I disconnect the MQTT broker from the network -Client.state() return 0 (MQTT_CONNECTED) and the serial monitor says a message was send -This continue for 25 minutes -After 25 minutes Client.state() return -2 -Now the ESP32 try to reconnect to the MQTT broker

That is way to long until the client detects the connection loss, anyone has an idea?

zcattacz commented 2 years ago

If you have calls to client.loop() (which inherently calls client.connected()) or client.connected (which updates client._state ) just before client.state() checks, it will most likely yield correct state.