From c8510f2605bbeac10a980633939364f83e5c73ff Mon Sep 17 00:00:00 2001 From: ilgrank Date: Fri, 17 Mar 2023 16:49:27 +0100 Subject: [PATCH] [SYS] Add utc & unix timestamp options to SYStoMQTT and Sensor messages (#1533) Add optional timestamps to the SYStoMQTT and modules messages Also, the sync with the NTP time server now occurs every hour (can be defined with TimeBetweenCheckingSYS in UserConfig.h) --- main/User_config.h | 6 +++++ main/ZgatewayBT.ino | 8 ++++++ main/main.ino | 63 +++++++++++++++++++++++++++++++++------------ 3 files changed, 61 insertions(+), 16 deletions(-) diff --git a/main/User_config.h b/main/User_config.h index 59c7fdc..a696e9d 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_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_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----------------*/ #ifndef ota_hostname diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index c4fe3e1..f0b9ff5 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -592,6 +592,14 @@ 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) +# if message_UTCtimestamp == true + BLEdata["UTCtime"] = UTCtimestamp(); +# endif +# if message_unixtimestamp == true + BLEdata["unixtime"] = unixtimestamp(); +# endif +# endif BLEdata["mac_type"] = advertisedDevice->getAddress().getType(); BLEdata["adv_type"] = advertisedDevice->getAdvType(); Log.notice(F("Device detected: %s" CR), (char*)mac_adress.c_str()); diff --git a/main/main.ino b/main/main.ino index bbe3501..333d5ca 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(); } @@ -1782,11 +1785,54 @@ float intTemperatureRead() { } #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; + } +} + +int unixtimestamp() { + return time(nullptr); +} + +String UTCtimestamp() { + 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) +# if message_UTCtimestamp == true + SYSdata["UTCtime"] = UTCtimestamp(); +# endif +# if message_unixtimestamp == true + SYSdata["unixtime"] = unixtimestamp(); +# endif +# endif + SYSdata["version"] = OMG_VERSION; # ifdef ZmqttDiscovery SYSdata["discovery"] = disc; @@ -2175,22 +2221,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