pubsubclient icon indicating copy to clipboard operation
pubsubclient copied to clipboard

Time out in Connect() Function

Open himanshu-hearthacker opened this issue 6 years ago • 12 comments

The connect() Function is a blocking Function, i want it as short as possible. For that i have set MQTT_SOCKET_TIMEOUT = 5 SEC, But in my case, timeout in connection usually takes around 30 sec in a low network area. I am unable to find out any other timeout in the source code. Please help me how can i set this time out to exactly 5 seconds. No issues if there is any delay due to network but i want to get out of this function in exactly 5 seconds. Setting MQTT_SOCKET_TIMEOUT = 5 SEC doesn't seems to solve my purpose

himanshu-hearthacker avatar Feb 25 '18 07:02 himanshu-hearthacker

hello, I have exactly the same problem.. I will tell if I find something

LeSuedois avatar Mar 29 '18 07:03 LeSuedois

I have the same problem .. on poor network connectivity client->write might hang a long time and after it gets back I get a bunch of subscribed messages at once and then a hangup .. ~32seconds in my case.

I lowered both timeouts to 5seconds as well and also tried to add:

_client->setTimeout(500);

in the connection function .. no change.

To test low connectivity I slightly touch the esp8266 .. then the _client->write function just fails with D: prepare client->write | D: client->write rc = 22 len = 42 indicating it has written 22 of 42 bytes .. the problem might be more with the underlying wificlient ..

cimba007 avatar Mar 29 '18 19:03 cimba007

Hello, I have exactly the same problem.

davekrum avatar Mar 29 '18 19:03 davekrum

Before including PubSubClient.h, define MQTT_SOCKET_TIMEOUT in seconds.

So to set a 1 second timeout: #define MQTT_SOCKET_TIMEOUT 1 #include <PubSubClient.h>

fusionstream avatar Oct 23 '18 05:10 fusionstream

@fusionstream that won't work in the Arduino IDE. It moves all includes to the top before compiling.

knolleary avatar Oct 23 '18 07:10 knolleary

It would be nice if this was a constructor default, PR are welcome?

dhilst avatar Aug 19 '19 22:08 dhilst

If you are using AVR and Wiznet libraries, use setRetransmissionCount() and set it to 4 (default is 8) on init. I tested this on W5500. I got that problem when connection was corrupt - wiznet was hanging in a retransmittion loop and it caused my watchdog to run out and restart the AVR. It took something around 20-30 seconds. Now it fits the 8s watchdog and works fine.

davekrum avatar Aug 20 '19 19:08 davekrum

Hello,

i am still facing the same issue with latest version of the library. Anybody found any luck with this question.

prashanthabib avatar Dec 27 '20 11:12 prashanthabib

any improvement?

bogus105 avatar Feb 21 '21 04:02 bogus105

@knolleary: Would be greatful for any hint here. Even with latest lirbary (2.8.0) no luck with changing the timeouts. Tried both the ways of #define MQTT_SOCKET_TIMEOUT 5 and setSocketTimeout(5) no change with both. Is this somehow hardcoded ?

Thanks in advance!

prashanthabib avatar Aug 12 '21 09:08 prashanthabib

Not sure, whether this is exact solution to your problem, but I also faced such problem by working with this library for the first time. My esp32 connects to my mobile hotspot with mobile data turned on; messages are sent successfully. Then, I turned off mobile data to test the behavior of my program, but esp32 is still connected to hotspot. By looking at Serial output in Arduino IDE, I noticed that a call to "connect(mqtt_station, mqtt_username, mqtt_password)" took 20 seconds. Here are the screenshots: image_2023-02-16_15-14-58 I found that problem is in select function from socket programming: https://man7.org/linux/man-pages/man2/select.2.html Since I pass WifiClient object to PubSubClient, it calls its own connect method where select() does its work. image It passes only two argument, since third argument 'timeout' is not allowed - PubSubClient stores pointer to abstract class Client as its attribute, which only has declarations of 'connect()' function with two arguments. image Therefore, as can be seen from screenshot, -1 is passed implicitly, which does all the 'magic'. image_2023-02-16_15-27-39 And according to the link I provided to Linux man-page:

If timeout is specified as NULL, select() blocks indefinitely waiting for a file descriptor to become ready.

So, you need to just pass your timeout parameter in milliseconds, as I did (2000ms): image Now, in Serial it shows different disconnect reason: image_2023-02-16_15-10-56

Gen1usBruh avatar Feb 16 '23 10:02 Gen1usBruh

Hello everyone! I use WiFiClientSecure.h. If the connection is lost (namely, the Internet connection, not wifi), then the esp32 board freezes for a long time. And if the board starts in this mode, with inaccessible internet, then nothing hangs. I reduced the timeouts in the library to 1 second and call "client connect" every 10 seconds. It's clumsy, but at least it's something. Help me please.

dancheeek92 avatar May 23 '23 20:05 dancheeek92