pubsubclient
pubsubclient copied to clipboard
Time out in Connect() Function
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
hello, I have exactly the same problem.. I will tell if I find something
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 ..
Hello, I have exactly the same problem.
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 that won't work in the Arduino IDE. It moves all includes to the top before compiling.
It would be nice if this was a constructor default, PR are welcome?
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.
Hello,
i am still facing the same issue with latest version of the library. Anybody found any luck with this question.
any improvement?
@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!
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:
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.
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.
Therefore, as can be seen from screenshot, -1 is passed implicitly, which does all the 'magic'.
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):
Now, in Serial it shows different disconnect reason:
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.