Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYS] Add utc & unix timestamp options to SYStoMQTT and Sensor messages #1533

Merged
merged 17 commits into from
Mar 17, 2023
6 changes: 6 additions & 0 deletions main/User_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
63 changes: 47 additions & 16 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down