diff --git a/code/components/jomjol_influxdb/interface_influxdb.cpp b/code/components/jomjol_influxdb/interface_influxdb.cpp index 6fdc51db7..d5e4b2b2f 100644 --- a/code/components/jomjol_influxdb/interface_influxdb.cpp +++ b/code/components/jomjol_influxdb/interface_influxdb.cpp @@ -46,10 +46,8 @@ void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _ti LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB_V2_Publish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp); - // Format: #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S%z" - - char nowTimestamp[21]; std::string payload; + char nowTimestamp[21]; if (_timestamp.length() > 0) { @@ -57,20 +55,22 @@ void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _ti strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm); time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime) - struct tm * ptm; - ptm = gmtime ( &t ); - time_t utc = mktime(ptm); - utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone) +// struct tm * ptm; +// ptm = gmtime ( &t ); +// time_t utc = mktime(ptm); + + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Use handover timestamp: " + _timestamp + " converted GMT timestamp: " + std::to_string(t)); - sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC +// utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone) +// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc after: " + std::to_string(utc)); + + sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC payload = _influxDB_V2_Measurement + " " + _key + "=" + _content + " " + nowTimestamp; -// payload = _influxDB_V2_Measurement + " " + _key + "=774 " + nowTimestamp; } else { payload = _influxDB_V2_Measurement + " " + _key + "=" + _content; -// payload = _influxDB_V2_Measurement + " " + _key + "=774"; } payload.shrink_to_fit(); @@ -157,10 +157,10 @@ void InfluxDBPublish(std::string _key, std::string _content, std::string _timest http_config.auth_type = HTTP_AUTH_TYPE_BASIC; } - LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp); - - char nowTimestamp[21]; std::string payload; + char nowTimestamp[21]; + + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp); if (_timestamp.length() > 0) { @@ -168,19 +168,22 @@ void InfluxDBPublish(std::string _key, std::string _content, std::string _timest strptime(_timestamp.c_str(), PREVALUE_TIME_FORMAT_OUTPUT, &tm); time_t t = mktime(&tm); // Time in Localtime (looks like timezone is not used by strptime) - struct tm * ptm; - ptm = gmtime ( &t ); - time_t utc = mktime(ptm); - utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone) +// struct tm * ptm; +// ptm = gmtime ( &t ); +// time_t utc = mktime(ptm); - sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Use handover timestamp: " + _timestamp + " converted GMT timestamp: " + std::to_string(t)); + +// utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone) +// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc after: " + std::to_string(utc)); + + sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp; -// payload = _influxDBMeasurement + " " + _key + "=774 " + nowTimestamp; } else { - payload = _influxDBMeasurement + " " + _key + "=" + _content; + payload = _influxDB_V2_Measurement + " " + _key + "=" + _content; } payload.shrink_to_fit(); diff --git a/code/components/jomjol_time_sntp/time_sntp.cpp b/code/components/jomjol_time_sntp/time_sntp.cpp index 78df9d5b4..7af1c6d6d 100644 --- a/code/components/jomjol_time_sntp/time_sntp.cpp +++ b/code/components/jomjol_time_sntp/time_sntp.cpp @@ -72,6 +72,24 @@ void time_sync_notification_cb(struct timeval *tv) } +bool time_manual_reset_sync(void) +{ + sntp_restart(); +// sntp_init(); + int retry = 0; + const int retry_count = 10; + while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET && ++retry < retry_count) { + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Waiting for system time to be set... " + std::to_string(retry) + "/" + std::to_string(retry_count)); + vTaskDelay(2000 / portTICK_PERIOD_MS); + } + if (retry >= retry_count) + return false; + + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Waiting for system time successfull with " + std::to_string(retry) + "/" + std::to_string(retry_count)); + return true; +} + + void setTimeZone(std::string _tzstring) { setenv("TZ", _tzstring.c_str(), 1); @@ -220,11 +238,16 @@ bool setupTime() { LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Configuring NTP Client..."); sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_setservername(0, timeServer.c_str()); - sntp_init(); - sntp_set_time_sync_notification_cb(time_sync_notification_cb); - setTimeZone(timeZone); + + sntp_init(); +/* + if (!wait_for_timesync()) + { + LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Timesync at startup failed."); + } +*/ } @@ -250,3 +273,5 @@ bool setupTime() { return true; } + + diff --git a/code/components/jomjol_time_sntp/time_sntp.h b/code/components/jomjol_time_sntp/time_sntp.h index 2faf324fc..c3dab062d 100644 --- a/code/components/jomjol_time_sntp/time_sntp.h +++ b/code/components/jomjol_time_sntp/time_sntp.h @@ -27,4 +27,7 @@ bool getTimeWasNotSetAtBoot(void); bool getUseNtp(void); bool setupTime(); +bool time_manual_reset_sync(void); + + #endif //TIMESNTP_H \ No newline at end of file diff --git a/code/main/main.cpp b/code/main/main.cpp index f461e7293..2889e34d5 100644 --- a/code/main/main.cpp +++ b/code/main/main.cpp @@ -337,6 +337,16 @@ extern "C" void app_main(void) ESP_LOGD(TAG, "main: sleep for: %ldms", (long) xDelay * CONFIG_FREERTOS_HZ/portTICK_PERIOD_MS); vTaskDelay( xDelay ); + + // manual reset the time + // ******************************************** + if (!time_manual_reset_sync()) + { + LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Manual Time Sync failed during startup" ); + } + + + // Set log level for wifi component to WARN level (default: INFO; only relevant for serial console) // ******************************************** esp_log_level_set("wifi", ESP_LOG_WARN);