[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

+6
View File
@@ -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
+8
View File
@@ -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());
+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