knolleary / pubsubclient

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

connect timeout is always 5 sec #1024

Open wavoigt opened 10 months ago

wavoigt commented 10 months ago

Hi, i'm using a battery powered ESP8266 and PubSubClient (latest version) with Home Assistant on Raspi3 and Mosquitto Broker. Normally, all is working fine. Now, if in case of a blackout the mosquitto Broker is unreachable, the connect function of PubSubClient has a timeout of 5 seconds. this is too long for my battery powered equipment. I tried to use setSocketTimeout(1) before using connect() but nothing is changing. Setting MQTT_SOCKET_TIMEOUT to 1 in PubSubClient.h also does not change anything.

wavoigt commented 10 months ago

Now, i found the solution for myself:

for Windows in: C:\Users\xxxx\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\3.1.2\libraries\ESP8266WiFi\src\WiFiClient.cpp line 80 and 87 _timeout = 1000; (original 5000)

`WiFiClient::WiFiClient() : _client(0), _owned(0) { _timeout = 1000; // was 5000 WiFiClient::_add(this); }

WiFiClient::WiFiClient(ClientContext* client) : _client(client), _owned(0) { _timeout = 1000; // was 5000 _client->ref(); WiFiClient::_add(this); setSync(defaultSync); setNoDelay(defaultNoDelay); } ` I'm wondering why it is hardcoded, so one normally can't change. I know, it is not the best way to make changes in libraries, but i had no better idea. For me, a timeout of 1000ms is sufficient, maybe in other applications it could be too low.

warnerthuis commented 10 months ago

I use the lib on a thermostat with an ESP8266 with another library OpenTherm and the heater requires as I understand that at least EVERY 1SECOND a call is done to the routine. The effect is in my case, I think,that if my thermostat loses contact with the server the heater is turned on. So I want this routine to be non-blocking with the connect command OR return within say 800ms,