[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)
This commit is contained in:
ilgrank authored and GitHub committed 2023-03-17 10:49:27 -05:00
1 parent 3b4af96990
commit c8510f2605
3 files changed
+61 -16

No files matched your search

+47 -16
View File
@@ -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<JSON_MSG_BUFFER> jsonBuffer;
JsonObject SYSdata = jsonBuffer.to<JsonObject>();
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