From 17c94a2b62f8fb9e2e80efa936acd2f70d6cefcb Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 17 Nov 2020 16:44:00 +0100 Subject: [PATCH] Changed MQTT Wifi connection timeout Changed MQTT Wifi connection timeout from 5000 to 200 mSec (#9886) --- CHANGELOG.md | 1 + RELEASENOTES.md | 1 + tasmota/xdrv_02_mqtt.ino | 15 +++++++++++---- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3adaae337..5e98a91d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. ### Changed - Shelly Dimmer fw upgrade using WebGUI Firmware Upgrade and file from folder `tools/fw_shd_stm32/` +- MQTT Wifi connection timeout from 5000 to 200 mSec (#9886) ### Fixed - KNX ESP32 UDP mulicastpackage (#9811) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index b0c9a7a09..c11f7a39a 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -77,6 +77,7 @@ The attached binaries can also be downloaded from http://ota.tasmota.com/tasmota - Core library from v2.7.4.5 to v2.7.4.7 - Platformio compiler option `no target align` enabled (#9749) - Sonoff L1 color up scaling and color margin detection (#9545) +- MQTT Wifi connection timeout from 5000 to 200 mSec (#9886) ### Fixed - NTP fallback server functionality (#9739) diff --git a/tasmota/xdrv_02_mqtt.ino b/tasmota/xdrv_02_mqtt.ino index 0d0221559..0ab518473 100644 --- a/tasmota/xdrv_02_mqtt.ino +++ b/tasmota/xdrv_02_mqtt.ino @@ -54,6 +54,7 @@ void (* const MqttCommand[])(void) PROGMEM = { struct MQTT { uint16_t connect_count = 0; // MQTT re-connect count uint16_t retry_counter = 1; // MQTT connection retry counter + uint16_t retry_counter_delay = 0; // MQTT retry counter multiplier uint8_t initial_connection_state = 2; // MQTT connection messages state bool connected = false; // MQTT virtual connection status bool allowed = false; // MQTT enabled and parameters valid @@ -480,7 +481,11 @@ uint16_t MqttConnectCount(void) void MqttDisconnected(int state) { Mqtt.connected = false; - Mqtt.retry_counter = Settings.mqtt_retry; + + if ((Settings.mqtt_retry * Mqtt.retry_counter_delay) < 120) { + Mqtt.retry_counter_delay++; + } + Mqtt.retry_counter = Settings.mqtt_retry * Mqtt.retry_counter_delay; MqttClient.disconnect(); @@ -496,6 +501,7 @@ void MqttConnected(void) AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT D_CONNECTED)); Mqtt.connected = true; Mqtt.retry_counter = 0; + Mqtt.retry_counter_delay = 0; Mqtt.connect_count++; GetTopic_P(stopic, TELE, TasmotaGlobal.mqtt_topic, S_LWT); @@ -602,7 +608,7 @@ void MqttReconnect(void) #endif // USE_EMULATION Mqtt.connected = false; - Mqtt.retry_counter = Settings.mqtt_retry; + Mqtt.retry_counter = Settings.mqtt_retry * Mqtt.retry_counter_delay; TasmotaGlobal.global_state.mqtt_down = 1; #ifdef FIRMWARE_MINIMAL @@ -629,15 +635,16 @@ void MqttReconnect(void) Response_P(S_LWT_OFFLINE); if (MqttClient.connected()) { MqttClient.disconnect(); } + EspClient.setTimeout(200); #ifdef USE_MQTT_TLS if (Mqtt.mqtt_tls) { tlsClient->stop(); } else { - EspClient = WiFiClient(); // Wifi Client reconnect issue 4497 (https://github.com/esp8266/Arduino/issues/4497) +// EspClient = WiFiClient(); // Wifi Client reconnect issue 4497 (https://github.com/esp8266/Arduino/issues/4497) MqttClient.setClient(EspClient); } #else - EspClient = WiFiClient(); // Wifi Client reconnect issue 4497 (https://github.com/esp8266/Arduino/issues/4497) +// EspClient = WiFiClient(); // Wifi Client reconnect issue 4497 (https://github.com/esp8266/Arduino/issues/4497) MqttClient.setClient(EspClient); #endif