Pubsubclient: Time out in Connect() Function

Created on 25 Feb 2018  路  8Comments  路  Source: knolleary/pubsubclient

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

Most helpful comment

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

All 8 comments

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

@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.

Was this page helpful?
0 / 5 - 0 ratings