|
@@ -17,11 +17,11 @@
|
|
|
|
|
|
|
|
static const char *TAG = "SNTP";
|
|
static const char *TAG = "SNTP";
|
|
|
|
|
|
|
|
-bool setTimeAlwaysOnReboot = true;
|
|
|
|
|
time_t bootTime;
|
|
time_t bootTime;
|
|
|
|
|
|
|
|
static void obtain_time(void);
|
|
static void obtain_time(void);
|
|
|
static void initialize_sntp(void);
|
|
static void initialize_sntp(void);
|
|
|
|
|
+static void logNtpStatus(sntp_sync_status_t status);
|
|
|
|
|
|
|
|
void time_sync_notification_cb(struct timeval *tv)
|
|
void time_sync_notification_cb(struct timeval *tv)
|
|
|
{
|
|
{
|
|
@@ -58,27 +58,27 @@ void setup_time()
|
|
|
struct tm timeinfo;
|
|
struct tm timeinfo;
|
|
|
time(&now);
|
|
time(&now);
|
|
|
localtime_r(&now, &timeinfo);
|
|
localtime_r(&now, &timeinfo);
|
|
|
|
|
+ char strftime_buf[64];
|
|
|
|
|
+
|
|
|
// Is time set? If not, tm_year will be (1970 - 1900).
|
|
// 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();
|
|
initialize_sntp();
|
|
|
obtain_time();
|
|
obtain_time();
|
|
|
// update 'now' variable with current time
|
|
// update 'now' variable with current time
|
|
|
time(&now);
|
|
time(&now);
|
|
|
- }
|
|
|
|
|
- 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);
|
|
|
|
|
|
|
+ setTimeZone("CET-1CEST,M3.5.0,M10.5.0/3");
|
|
|
|
|
|
|
|
- std::string zw = gettimestring("%Y%m%d-%H%M%S");
|
|
|
|
|
- ESP_LOGD(TAG, "timeist %s", zw.c_str());
|
|
|
|
|
|
|
+ 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);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void setTimeZone(std::string _tzstring)
|
|
void setTimeZone(std::string _tzstring)
|
|
@@ -97,8 +97,23 @@ static void obtain_time(void)
|
|
|
const int retry_count = 10;
|
|
const int retry_count = 10;
|
|
|
time(&now);
|
|
time(&now);
|
|
|
localtime_r(&now, &timeinfo);
|
|
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);
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -106,6 +121,20 @@ static void obtain_time(void)
|
|
|
localtime_r(&now, &timeinfo);
|
|
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)
|
|
void reset_servername(std::string _servername)
|
|
|
{
|
|
{
|
|
|
ESP_LOGD(TAG, "Set SNTP-Server: %s", _servername.c_str());
|
|
ESP_LOGD(TAG, "Set SNTP-Server: %s", _servername.c_str());
|
|
@@ -138,4 +167,29 @@ time_t getUpTime()
|
|
|
time(&now);
|
|
time(&now);
|
|
|
|
|
|
|
|
return now - bootTime;
|
|
return now - bootTime;
|
|
|
-}
|
|
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+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();
|
|
|
|
|
+}
|