From 621a0eff116308dd57f3c9e86a7612a2c1f74a6e Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 8 Aug 2021 15:22:44 +0200 Subject: [PATCH] Relax NTP poll if no ntpserver can be resolved by DNS (#12838) --- CHANGELOG.md | 1 + RELEASENOTES.md | 1 + tasmota/support_wifi.ino | 20 ++++++++++++++++---- tasmota/xdrv_02_9_mqtt.ino | 2 ++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7eed2dcfc..e39a12d6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. ### Changed - Make Sonoff L1 MusicSync persistent (#12008) +- Relax NTP poll if no ntpserver can be resolved by DNS ### Fixed - Neopool communication error (#12813) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 23f8384d4..96ddcf5a7 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -129,6 +129,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo - Extended supported sensor driver range to 128 - Disable PSRAM on unsupported hardware - ESP32 remove GPIO initialization to INPUT from not used GPIOs to allow JTAG support +- Relax NTP poll if no ntpserver can be resolved by DNS - Make Sonoff L1 MusicSync persistent [#12008](https://github.com/arendst/Tasmota/issues/12008) - Simplified configuration for ir-full and removal of tasmota-ircustom [#12428](https://github.com/arendst/Tasmota/issues/12428) - Refactor platformio [#12442](https://github.com/arendst/Tasmota/issues/12442) diff --git a/tasmota/support_wifi.ino b/tasmota/support_wifi.ino index 4119b0f0e..0e16338d8 100644 --- a/tasmota/support_wifi.ino +++ b/tasmota/support_wifi.ino @@ -715,6 +715,7 @@ void wifiKeepAlive(void) { void WifiPollNtp() { static uint8_t ntp_sync_minute = 0; + static uint32_t ntp_run_time = 0; if (TasmotaGlobal.global_state.network_down || Rtc.user_time_entry) { return; } @@ -723,13 +724,20 @@ void WifiPollNtp() { ntp_sync_minute = 1; // If sync prepare for a new cycle } // First try ASAP to sync. If fails try once every 60 seconds based on chip id - uint8_t offset = (TasmotaGlobal.uptime < 30) ? RtcTime.second : (((ESP_getChipId() & 0xF) * 3) + 3) ; + uint8_t offset = (TasmotaGlobal.uptime < 30) ? RtcTime.second + ntp_run_time : (((ESP_getChipId() & 0xF) * 3) + 3) ; + if ( (((offset == RtcTime.second) && ( (RtcTime.year < 2016) || // Never synced (ntp_sync_minute == uptime_minute))) || // Re-sync every hour TasmotaGlobal.ntp_force_sync ) ) { // Forced sync TasmotaGlobal.ntp_force_sync = false; + + ntp_run_time = millis(); uint32_t ntp_time = WifiGetNtp(); + ntp_run_time = (millis() - ntp_run_time) / 1000; +// AddLog(LOG_LEVEL_DEBUG, PSTR("NTP: Runtime %d"), ntp_run_time); + if (ntp_run_time < 5) { ntp_run_time = 0; } // DNS timeout is around 10s + if (ntp_time > START_VALID_TIME) { Rtc.utc_time = ntp_time; ntp_sync_minute = 60; // Sync so block further requests @@ -758,10 +766,14 @@ uint32_t WifiGetNtp(void) { ntp_server = fallback_ntp_server; } if (strlen(ntp_server)) { - resolved_ip = (WiFi.hostByName(ntp_server, time_server_ip) == 1); - if (255 == time_server_ip[0]) { resolved_ip = false; } + resolved_ip = (WiFi.hostByName(ntp_server, time_server_ip) == 1); // DNS timeout set to (ESP8266) 10s / (ESP32) 14s + if ((255 == time_server_ip[0]) || // No valid name resolved (255.255.255.255) + ((255 == time_server_ip[1]) && (255 == time_server_ip[2]) && (255 == time_server_ip[3]))) { // No valid name resolved (x.255.255.255) + resolved_ip = false; + } yield(); if (resolved_ip) { break; } +// AddLog(LOG_LEVEL_DEBUG, PSTR("NTP: Unable to resolve '%s'"), ntp_server); } ntp_server_id++; } @@ -770,7 +782,7 @@ uint32_t WifiGetNtp(void) { return 0; } -// AddLog(LOG_LEVEL_DEBUG, PSTR("NTP: Name %s, IP %_I"), ntp_server, (uint32_t)time_server_ip); +// AddLog(LOG_LEVEL_DEBUG, PSTR("NTP: Host %s IP %_I"), ntp_server, (uint32_t)time_server_ip); WiFiUDP udp; diff --git a/tasmota/xdrv_02_9_mqtt.ino b/tasmota/xdrv_02_9_mqtt.ino index bbc37b29e..bb8cdf8b8 100644 --- a/tasmota/xdrv_02_9_mqtt.ino +++ b/tasmota/xdrv_02_9_mqtt.ino @@ -858,6 +858,8 @@ void MqttDisconnected(int state) { } MqttClient.disconnect(); + // Check if this solves intermittent MQTT re-connection failures when broker is restarted + EspClient.stop(); AddLog(LOG_LEVEL_INFO, PSTR(D_LOG_MQTT D_CONNECT_FAILED_TO " %s:%d, rc %d. " D_RETRY_IN " %d " D_UNIT_SECOND), SettingsText(SET_MQTT_HOST), Settings->mqtt_port, state, Mqtt.retry_counter); TasmotaGlobal.rules_flag.mqtt_disconnected = 1;