ESP8266 : how to add a timeout on MQTT writing

Hi all,

I'm using several WiFi probes' based on ESP-8266 and sending MQTT messages.

Randomly, connecting to the MQTT broker become VERY slow, leading to dramatic battery consumption (I'm suspecting something wrong in the WiFi repeater I added on my network, nothing I can track unfortunately).

My connection code looks like :

void Connexion_MQTT(){
	LED(LOW);
#ifdef SERIAL_ENABLED
	Serial.println("Connexion MQTT");
#endif

	Duree dMQTT;
	for( int i=0; i< 240; i++ ){
		if(clientMQTT.connect(MQTT_CLIENT, false)){	// false prevent commands to be cleared
			LED(HIGH);
			dMQTT.Fini();

#ifdef SERIAL_ENABLED
			Serial.print("Duree connexion MQTT :");
			Serial.println( *dMQTT );
#endif
			publish( MQTT_MQTT, *dMQTT, false );
			clientMQTT.subscribe(MQTT_Command.c_str(), 1);
			return;
		} else {
#ifdef SERIAL_ENABLED
			Serial.print("Echec, rc:");
			Serial.println(clientMQTT.state());
#endif
			delay(500);	// Test dans 0,5 seconde
		}
	}
	LED(HIGH);
#ifdef SERIAL_ENABLED
	Serial.println("Impossible de se connecter au MQTT");
#endif
	ESP.deepSleep( Sommeil.getConsigne() * 1e6 );	// On essaiera plus tard
}

"Duree" is a class that measure the duration from its creation to Fini() method ... so I can measure that when stuffs are going wrong, clientMQTT.connect() itself "hangs" to 30 seconds or more !!!

Is it a way to implement a watchdog that will reboot the ESP in such situation ?

Thanks

Laurent

ps: as none of the standards hardware or software watchdog are triggered, I presume they are feed by Wifi or MQTT code ... So I have to find another way.

Is it a way to implement a watchdog that will reboot the ESP in such situation ?

If the client.connect() function really does block for 30 seconds, without ever calling yield(), then the watchdog will already growl and reset the ESP.

Any call to yield() pets the watchdog, and keeps it from growling.

PaulS:
If the client.connect() function really does block for 30 seconds, without ever calling yield(), then the watchdog will already growl and reset the ESP.

So I think (I'm sure :slight_smile: ), there are some yielding into WiFi code as it doesn't reset.
I'll try to test with disabling soft timer ...