From ecc43edbba11d77bb52a9a5f333b868d70ed862c Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Thu, 17 Nov 2022 19:01:24 +0100 Subject: [PATCH] Improve NTP (#1364) * checking NTP status on every round and restart NTP client if we still are in 1970 * . * . * . Co-authored-by: CaCO3 --- .../jomjol_flowcontroll/ClassFlowControll.cpp | 6 ++ .../components/jomjol_time_sntp/time_sntp.cpp | 92 +++++++++++++++---- code/components/jomjol_time_sntp/time_sntp.h | 2 + 3 files changed, 81 insertions(+), 19 deletions(-) diff --git a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp b/code/components/jomjol_flowcontroll/ClassFlowControll.cpp index 2090ed07..6235a058 100644 --- a/code/components/jomjol_flowcontroll/ClassFlowControll.cpp +++ b/code/components/jomjol_flowcontroll/ClassFlowControll.cpp @@ -286,6 +286,12 @@ bool ClassFlowControll::doFlow(string time) LogFile.WriteHeapInfo("ClassFlowControll::doFlow - Start"); #endif + /* Check if we have a valid date/time and if not restart the NTP client */ + if (! getTimeIsSet()) { + LogFile.WriteToFile(ESP_LOG_WARN, TAG, "Time not set, restarting NTP Client!"); + restartNtpClient(); + } + for (int i = 0; i < FlowControll.size(); ++i) { zw_time = gettimestring("%H:%M:%S"); diff --git a/code/components/jomjol_time_sntp/time_sntp.cpp b/code/components/jomjol_time_sntp/time_sntp.cpp index a64531bb..e049d621 100644 --- a/code/components/jomjol_time_sntp/time_sntp.cpp +++ b/code/components/jomjol_time_sntp/time_sntp.cpp @@ -17,11 +17,11 @@ static const char *TAG = "SNTP"; -bool setTimeAlwaysOnReboot = true; time_t bootTime; static void obtain_time(void); static void initialize_sntp(void); +static void logNtpStatus(sntp_sync_status_t status); void time_sync_notification_cb(struct timeval *tv) { @@ -58,27 +58,27 @@ void setup_time() struct tm timeinfo; time(&now); localtime_r(&now, &timeinfo); + char strftime_buf[64]; + // Is time set? If not, tm_year will be (1970 - 1900). - if ((timeinfo.tm_year < (2016 - 1900)) || setTimeAlwaysOnReboot) { - ESP_LOGI(TAG, "Time is not set yet. Connecting to WiFi and getting time over NTP."); + if (!getTimeIsSet()) { + ESP_LOGI(TAG, "Time is not set yet. Getting time over NTP."); initialize_sntp(); obtain_time(); // update 'now' variable with current time time(&now); + + setTimeZone("CET-1CEST,M3.5.0,M10.5.0/3"); + + localtime_r(&now, &timeinfo); + strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H:%M:%S", &timeinfo); + ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf); + } + else { + localtime_r(&now, &timeinfo); + strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H:%M:%S", &timeinfo); + ESP_LOGI(TAG, "Time is already set (%s)", strftime_buf); } - char strftime_buf[64]; - - setTimeZone("CET-1CEST,M3.5.0,M10.5.0/3"); - - localtime_r(&now, &timeinfo); - strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); - ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf); - - strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H:%M", &timeinfo); - ESP_LOGI(TAG, "The current date/time in Berlin is: %s", strftime_buf); - - std::string zw = gettimestring("%Y%m%d-%H%M%S"); - ESP_LOGD(TAG, "timeist %s", zw.c_str()); } void setTimeZone(std::string _tzstring) @@ -97,8 +97,23 @@ static void obtain_time(void) const int retry_count = 10; time(&now); localtime_r(&now, &timeinfo); - while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) { - ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, retry_count); + + ESP_LOGI(TAG, "Waiting until we get a time from the NTP server..."); + while (true) { + retry++; + + if (retry == retry_count) { + ESP_LOGW(TAG, "NTP time fetching seems to take longer, will check again on next round!"); // The NTP client will automatically retry periodically! + break; + } + + sntp_sync_status_t status = sntp_get_sync_status(); + logNtpStatus(status); + if (status == SNTP_SYNC_STATUS_COMPLETED) { + ESP_LOGI(TAG, "Time is synced with NTP Server"); + break; + } + vTaskDelay(2000 / portTICK_PERIOD_MS); } @@ -106,6 +121,20 @@ static void obtain_time(void) localtime_r(&now, &timeinfo); } + +void logNtpStatus(sntp_sync_status_t status) { + if (status == SNTP_SYNC_STATUS_COMPLETED) { + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "NTP Status OK"); + } + else if (status == SNTP_SYNC_STATUS_IN_PROGRESS) { + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "NTP Status: In Progress"); + } + else { // SNTP_SYNC_STATUS_RESET + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "NTP Status: Reset"); + } +} + + void reset_servername(std::string _servername) { ESP_LOGD(TAG, "Set SNTP-Server: %s", _servername.c_str()); @@ -138,4 +167,29 @@ time_t getUpTime() time(&now); return now - bootTime; -} \ No newline at end of file +} + +bool getTimeIsSet(void) { + time_t now; + struct tm timeinfo; + time(&now); + localtime_r(&now, &timeinfo); + char strftime_buf[64]; + + localtime_r(&now, &timeinfo); + strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d_%H:%M:%S", &timeinfo); + ESP_LOGD(TAG, "The current date/time in Berlin is: %s", strftime_buf); + + // Is time set? If not, tm_year will be (1970 - 1900). + if ((timeinfo.tm_year < (2022 - 1900))) { + return false; + } + else { + return true; + } +} + +void restartNtpClient(void) { + sntp_restart(); + obtain_time(); +} diff --git a/code/components/jomjol_time_sntp/time_sntp.h b/code/components/jomjol_time_sntp/time_sntp.h index c56e6c91..ec94e741 100644 --- a/code/components/jomjol_time_sntp/time_sntp.h +++ b/code/components/jomjol_time_sntp/time_sntp.h @@ -22,3 +22,5 @@ void reset_servername(std::string _servername); void setBootTime(); time_t getUpTime(); +bool getTimeIsSet(void); +void restartNtpClient(void); \ No newline at end of file