From d4adc0544d93323b7056f4ea54a0dcb1728d448a Mon Sep 17 00:00:00 2001 From: ilgrank Date: Tue, 14 Mar 2023 19:13:18 +0100 Subject: [PATCH 01/16] Update main.ino --- main/main.ino | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/main/main.ino b/main/main.ino index 56af426041..cca8686770 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1725,12 +1725,34 @@ float intTemperatureRead() { return temp_c; } #endif +# if defined(ESP8266) || defined(ESP32) +void SyncNTP() { + configTime(0, 0, NTP_SERVER); + time_t now = time(nullptr); + uint8_t count = 0; + Log.trace(F("Waiting for NTP time sync" CR)); + while ((now < 8 * 3600 * 2) && count++ < 60) { + delay(500); + now = time(nullptr); + } + if (count >= 60) { + Log.error(F("Unable to update - invalid time" CR)); +# if defined(ZgatewayBT) && defined(ESP32) + startProcessing(); +# endif + return; + } +} +# endif #if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) void stateMeasures() { StaticJsonDocument jsonBuffer; JsonObject SYSdata = jsonBuffer.to(); SYSdata["uptime"] = uptime(); +# if defined(ESP8266) || defined(ESP32) + SYSdata["unixtime"] = time(nullptr); +# endif SYSdata["version"] = OMG_VERSION; # ifdef ZmqttDiscovery SYSdata["discovery"] = disc; @@ -1950,7 +1972,9 @@ void receivingMQTT(char* topicOri, char* datacallback) { # endif #endif SendReceiveIndicatorON(); - +#if defined(ESP8266) || defined(ESP32) + SyncNTP(); +#endif MQTTtoSYS(topicOri, jsondata); } else { // not a json object --> simple decoding #if simpleReceiving @@ -2113,22 +2137,7 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) { client.disconnect(); update_client = *(WiFiClientSecure*)eClient; } else { - configTime(0, 0, NTP_SERVER); - time_t now = time(nullptr); - uint8_t count = 0; - Log.trace(F("Waiting for NTP time sync" CR)); - while ((now < 8 * 3600 * 2) && count++ < 60) { - delay(500); - now = time(nullptr); - } - - if (count >= 60) { - Log.error(F("Unable to update - invalid time" CR)); -# if defined(ZgatewayBT) && defined(ESP32) - startProcessing(); -# endif - return; - } + SyncNTP(); } # ifdef ESP32 From 24ce3c3327da3e74692bd74409a5283d9ca48cb7 Mon Sep 17 00:00:00 2001 From: ilgrank Date: Tue, 14 Mar 2023 19:16:05 +0100 Subject: [PATCH 02/16] Add Unix timestamps to Sensor messages --- main/ZgatewayBT.ino | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index a529c4ca7c..c711e41ba3 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -590,6 +590,9 @@ void procBLETask(void* pvParameters) { String mac_adress = advertisedDevice->getAddress().toString().c_str(); mac_adress.toUpperCase(); BLEdata["id"] = (char*)mac_adress.c_str(); +# if defined(ESP8266) || defined(ESP32) + BLEdata["unixtime"] = time(nullptr); +# endif BLEdata["mac_type"] = advertisedDevice->getAddress().getType(); BLEdata["adv_type"] = advertisedDevice->getAdvType(); Log.notice(F("Device detected: %s" CR), (char*)mac_adress.c_str()); From 5e8ba7580a3b42ccaa7466e297188e4527208a6b Mon Sep 17 00:00:00 2001 From: ilgrank Date: Tue, 14 Mar 2023 19:27:09 +0100 Subject: [PATCH 03/16] Update main.ino --- main/main.ino | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/main/main.ino b/main/main.ino index cca8686770..d153b20b40 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1732,16 +1732,16 @@ void SyncNTP() { uint8_t count = 0; Log.trace(F("Waiting for NTP time sync" CR)); while ((now < 8 * 3600 * 2) && count++ < 60) { - delay(500); - now = time(nullptr); + delay(500); + now = time(nullptr); } if (count >= 60) { - Log.error(F("Unable to update - invalid time" CR)); + Log.error(F("Unable to update - invalid time" CR)); # if defined(ZgatewayBT) && defined(ESP32) - startProcessing(); + startProcessing(); # endif - return; + return; } } # endif @@ -1972,9 +1972,9 @@ void receivingMQTT(char* topicOri, char* datacallback) { # endif #endif SendReceiveIndicatorON(); -#if defined(ESP8266) || defined(ESP32) +# if defined(ESP8266) || defined(ESP32) SyncNTP(); -#endif +# endif MQTTtoSYS(topicOri, jsondata); } else { // not a json object --> simple decoding #if simpleReceiving From 8c109840c85111e360ce67027f9f53c6e9fa8fe1 Mon Sep 17 00:00:00 2001 From: ilgrank Date: Tue, 14 Mar 2023 19:33:51 +0100 Subject: [PATCH 04/16] Lint --- main/main.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main/main.ino b/main/main.ino index d153b20b40..ced1d5bfaa 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1725,7 +1725,7 @@ float intTemperatureRead() { return temp_c; } #endif -# if defined(ESP8266) || defined(ESP32) +#if defined(ESP8266) || defined(ESP32) void SyncNTP() { configTime(0, 0, NTP_SERVER); time_t now = time(nullptr); @@ -1744,7 +1744,7 @@ void SyncNTP() { return; } } -# endif +#endif #if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) void stateMeasures() { StaticJsonDocument jsonBuffer; @@ -1972,9 +1972,9 @@ void receivingMQTT(char* topicOri, char* datacallback) { # endif #endif SendReceiveIndicatorON(); -# if defined(ESP8266) || defined(ESP32) +#if defined(ESP8266) || defined(ESP32) SyncNTP(); -# endif +#endif MQTTtoSYS(topicOri, jsondata); } else { // not a json object --> simple decoding #if simpleReceiving From 3a6d9dce133423821ad7c973eb4bad0ba56d935c Mon Sep 17 00:00:00 2001 From: ilgrank Date: Tue, 14 Mar 2023 19:35:28 +0100 Subject: [PATCH 05/16] Lint --- main/main.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/main.ino b/main/main.ino index ced1d5bfaa..d2564a62dc 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1726,7 +1726,7 @@ float intTemperatureRead() { } #endif #if defined(ESP8266) || defined(ESP32) -void SyncNTP() { +void SyncNTP() { configTime(0, 0, NTP_SERVER); time_t now = time(nullptr); uint8_t count = 0; @@ -1743,7 +1743,7 @@ void SyncNTP() { # endif return; } -} +} #endif #if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) void stateMeasures() { From 9c0f0c394948f3285fc8a60554f94ce1659e937f Mon Sep 17 00:00:00 2001 From: ilgrank Date: Wed, 15 Mar 2023 02:11:25 +0100 Subject: [PATCH 06/16] minor optimization --- main/main.ino | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main/main.ino b/main/main.ino index d2564a62dc..4ddd936053 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1751,6 +1751,7 @@ void stateMeasures() { JsonObject SYSdata = jsonBuffer.to(); SYSdata["uptime"] = uptime(); # if defined(ESP8266) || defined(ESP32) + SyncNTP(); SYSdata["unixtime"] = time(nullptr); # endif SYSdata["version"] = OMG_VERSION; @@ -1972,9 +1973,7 @@ void receivingMQTT(char* topicOri, char* datacallback) { # endif #endif SendReceiveIndicatorON(); -#if defined(ESP8266) || defined(ESP32) - SyncNTP(); -#endif + MQTTtoSYS(topicOri, jsondata); } else { // not a json object --> simple decoding #if simpleReceiving From c5a68143e9eb7641738a041001062b536f62181f Mon Sep 17 00:00:00 2001 From: ilgrank Date: Fri, 17 Mar 2023 14:26:20 +0100 Subject: [PATCH 07/16] Update User_config.h --- main/User_config.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main/User_config.h b/main/User_config.h index 59c7fdc819..9806883c13 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -386,6 +386,12 @@ int lowpowermode = DEFAULT_LOW_POWER_MODE; #ifndef simpleReceiving # define simpleReceiving true //define false if you don't want to use old way reception analysis #endif +#ifndef message_Time +# define message_Time false //define true if you want messages to be timestamped in ISO8601 UTC format (e.g.: "time"="2023-12-26T19:10:20Z") +#endif +#ifndef message_UnixTime +# define message_UnixTime false //define true if you want messages to have an unix timestamp (e.g.: "unixtime"=1679015107) +#endif /*-------------DEFINE YOUR OTA PARAMETERS BELOW----------------*/ #ifndef ota_hostname From adcf40fbea7d3c84b0ff83b7f98f7b0543d155f8 Mon Sep 17 00:00:00 2001 From: ilgrank Date: Fri, 17 Mar 2023 14:26:48 +0100 Subject: [PATCH 08/16] Update ZgatewayBT.ino --- main/ZgatewayBT.ino | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index a5fd989c60..5877ad04be 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -593,7 +593,12 @@ void procBLETask(void* pvParameters) { mac_adress.toUpperCase(); BLEdata["id"] = (char*)mac_adress.c_str(); # if defined(ESP8266) || defined(ESP32) - BLEdata["unixtime"] = time(nullptr); +# if message_Time == true + BLEdata["time"] = timestamp(); +# endif +# if message_UnixTime == true + BLEdata["unixtime"] = unixtimestamp(); +# endif # endif BLEdata["mac_type"] = advertisedDevice->getAddress().getType(); BLEdata["adv_type"] = advertisedDevice->getAdvType(); From 5c95036f91af10fa605c343a95d9c25b3b481b34 Mon Sep 17 00:00:00 2001 From: ilgrank Date: Fri, 17 Mar 2023 14:29:31 +0100 Subject: [PATCH 09/16] optional timestamps to SYStoMQTT, Better time sync Add optional timestamps to the SYStoMQTT topic Also, the sync with the NTP time server now occurs every hour instead of every 2 minutes (can be defined with TimeBetweenCheckingSYS in UserConfig.h) --- main/main.ino | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/main/main.ino b/main/main.ino index 8fe7b91135..dedd12c769 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1613,6 +1613,9 @@ void loop() { if (now > (timer_sys_checks + (TimeBetweenCheckingSYS * 1000)) || !timer_sys_checks) { # if defined(ESP32) && defined(MQTT_HTTPS_FW_UPDATE) checkForUpdates(); +# endif +# if defined(ESP8266) || defined(ESP32) + syncNTP(); # endif timer_sys_checks = millis(); } @@ -1781,8 +1784,9 @@ float intTemperatureRead() { return temp_c; } #endif + #if defined(ESP8266) || defined(ESP32) -void SyncNTP() { +void syncNTP() { configTime(0, 0, NTP_SERVER); time_t now = time(nullptr); uint8_t count = 0; @@ -1800,16 +1804,35 @@ void SyncNTP() { return; } } + +int unixtimestamp() { + return time(nullptr); +} + +String timestamp() { + time_t now; + time(&now); + char buffer[sizeof "yyyy-MM-ddThh:mm:ssZ"]; + strftime(buffer, sizeof buffer, "%FT%TZ", gmtime(&now)); + return buffer; +} + #endif + #if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) void stateMeasures() { StaticJsonDocument jsonBuffer; JsonObject SYSdata = jsonBuffer.to(); SYSdata["uptime"] = uptime(); # if defined(ESP8266) || defined(ESP32) - SyncNTP(); - SYSdata["unixtime"] = time(nullptr); +# if message_Time == true + SYSdata["time"] = timestamp(); +# endif +# if message_UnixTime == true + SYSdata["unixtime"] = unixtimestamp(); +# endif # endif + SYSdata["version"] = OMG_VERSION; # ifdef ZmqttDiscovery SYSdata["discovery"] = disc; @@ -2198,7 +2221,7 @@ void MQTTHttpsFWUpdate(char* topicOri, JsonObject& HttpsFwUpdateData) { client.disconnect(); update_client = *(WiFiClientSecure*)eClient; } else { - SyncNTP(); + syncNTP(); } # ifdef ESP32 From ce71a236fcffeb27e5b5388e2aab9f586d4ac8c6 Mon Sep 17 00:00:00 2001 From: ilgrank Date: Fri, 17 Mar 2023 15:16:21 +0100 Subject: [PATCH 10/16] time->UTCtime --- main/User_config.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main/User_config.h b/main/User_config.h index 9806883c13..4aa87fc9d6 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -386,11 +386,11 @@ int lowpowermode = DEFAULT_LOW_POWER_MODE; #ifndef simpleReceiving # define simpleReceiving true //define false if you don't want to use old way reception analysis #endif -#ifndef message_Time -# define message_Time false //define true if you want messages to be timestamped in ISO8601 UTC format (e.g.: "time"="2023-12-26T19:10:20Z") +#ifndef message_UTCtime +# define message_UTCTime false //define true if you want messages to be timestamped in ISO8601 UTC format (e.g.: "UTCtime"="2023-12-26T19:10:20Z") #endif -#ifndef message_UnixTime -# define message_UnixTime false //define true if you want messages to have an unix timestamp (e.g.: "unixtime"=1679015107) +#ifndef message_unixtime +# define message_unixtime false //define true if you want messages to have an unix timestamp (e.g.: "unixtime"=1679015107) #endif /*-------------DEFINE YOUR OTA PARAMETERS BELOW----------------*/ From 51bb5f2ee6565e17954b95003b8c4df30161946c Mon Sep 17 00:00:00 2001 From: ilgrank Date: Fri, 17 Mar 2023 15:17:38 +0100 Subject: [PATCH 11/16] Update main.ino --- main/main.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main/main.ino b/main/main.ino index dedd12c769..1aa16c5ee1 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1809,7 +1809,7 @@ int unixtimestamp() { return time(nullptr); } -String timestamp() { +String UTCtimestamp() { time_t now; time(&now); char buffer[sizeof "yyyy-MM-ddThh:mm:ssZ"]; @@ -1825,10 +1825,10 @@ void stateMeasures() { JsonObject SYSdata = jsonBuffer.to(); SYSdata["uptime"] = uptime(); # if defined(ESP8266) || defined(ESP32) -# if message_Time == true - SYSdata["time"] = timestamp(); +# if message_UTCtime == true + SYSdata["UTCtime"] = UTCtimestamp(); # endif -# if message_UnixTime == true +# if message_unixtime == true SYSdata["unixtime"] = unixtimestamp(); # endif # endif From 2206a4dccfc5031da740fc0e54129ef3956175cf Mon Sep 17 00:00:00 2001 From: ilgrank Date: Fri, 17 Mar 2023 15:18:37 +0100 Subject: [PATCH 12/16] Update ZgatewayBT.ino --- main/ZgatewayBT.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 5877ad04be..a5b7abd2bd 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -593,10 +593,10 @@ void procBLETask(void* pvParameters) { mac_adress.toUpperCase(); BLEdata["id"] = (char*)mac_adress.c_str(); # if defined(ESP8266) || defined(ESP32) -# if message_Time == true - BLEdata["time"] = timestamp(); +# if message_UTCtime == true + BLEdata["UTCtime"] = UTCtimestamp(); # endif -# if message_UnixTime == true +# if message_unixtime == true BLEdata["unixtime"] = unixtimestamp(); # endif # endif From da5ea41b426864a2e85e491f21785f58f321bd1b Mon Sep 17 00:00:00 2001 From: ilgrank Date: Fri, 17 Mar 2023 15:20:42 +0100 Subject: [PATCH 13/16] typo --- main/User_config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/User_config.h b/main/User_config.h index 4aa87fc9d6..387d8732e2 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -387,7 +387,7 @@ int lowpowermode = DEFAULT_LOW_POWER_MODE; # define simpleReceiving true //define false if you don't want to use old way reception analysis #endif #ifndef message_UTCtime -# define message_UTCTime false //define true if you want messages to be timestamped in ISO8601 UTC format (e.g.: "UTCtime"="2023-12-26T19:10:20Z") +# define message_UTCtime false //define true if you want messages to be timestamped in ISO8601 UTC format (e.g.: "UTCtime"="2023-12-26T19:10:20Z") #endif #ifndef message_unixtime # define message_unixtime false //define true if you want messages to have an unix timestamp (e.g.: "unixtime"=1679015107) From 05581028a76d64e02e38d158130119d0a4cd9e84 Mon Sep 17 00:00:00 2001 From: ilgrank Date: Fri, 17 Mar 2023 15:22:07 +0100 Subject: [PATCH 14/16] Update User_config.h --- main/User_config.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main/User_config.h b/main/User_config.h index 387d8732e2..a696e9dfde 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -386,11 +386,11 @@ int lowpowermode = DEFAULT_LOW_POWER_MODE; #ifndef simpleReceiving # define simpleReceiving true //define false if you don't want to use old way reception analysis #endif -#ifndef message_UTCtime -# define message_UTCtime false //define true if you want messages to be timestamped in ISO8601 UTC format (e.g.: "UTCtime"="2023-12-26T19:10:20Z") +#ifndef message_UTCtimestamp +# define message_UTCtimestamp false //define true if you want messages to be timestamped in ISO8601 UTC format (e.g.: "UTCtime"="2023-12-26T19:10:20Z") #endif -#ifndef message_unixtime -# define message_unixtime false //define true if you want messages to have an unix timestamp (e.g.: "unixtime"=1679015107) +#ifndef message_unixtimestamp +# define message_unixtimestamp false //define true if you want messages to have an unix timestamp (e.g.: "unixtime"=1679015107) #endif /*-------------DEFINE YOUR OTA PARAMETERS BELOW----------------*/ From a7b2524aa8bdfb579bb9f5eec284e11e97eb66a5 Mon Sep 17 00:00:00 2001 From: ilgrank Date: Fri, 17 Mar 2023 15:22:57 +0100 Subject: [PATCH 15/16] Update ZgatewayBT.ino --- main/ZgatewayBT.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index a5b7abd2bd..f0b9ff5fa0 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -593,10 +593,10 @@ void procBLETask(void* pvParameters) { mac_adress.toUpperCase(); BLEdata["id"] = (char*)mac_adress.c_str(); # if defined(ESP8266) || defined(ESP32) -# if message_UTCtime == true +# if message_UTCtimestamp == true BLEdata["UTCtime"] = UTCtimestamp(); # endif -# if message_unixtime == true +# if message_unixtimestamp == true BLEdata["unixtime"] = unixtimestamp(); # endif # endif From 572876e1e357508a158e5ecd86bc9726a608465d Mon Sep 17 00:00:00 2001 From: ilgrank Date: Fri, 17 Mar 2023 15:23:38 +0100 Subject: [PATCH 16/16] Update main.ino --- main/main.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/main.ino b/main/main.ino index 1aa16c5ee1..333d5ca1fc 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1825,10 +1825,10 @@ void stateMeasures() { JsonObject SYSdata = jsonBuffer.to(); SYSdata["uptime"] = uptime(); # if defined(ESP8266) || defined(ESP32) -# if message_UTCtime == true +# if message_UTCtimestamp == true SYSdata["UTCtime"] = UTCtimestamp(); # endif -# if message_unixtime == true +# if message_unixtimestamp == true SYSdata["unixtime"] = unixtimestamp(); # endif # endif