diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index ab80dfdd..3deaed4f 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -221,7 +221,8 @@ PROGMEM const char* const custom_reset_string[] = { #define MQTT_TOPIC_RSSI "rssi" #define MQTT_TOPIC_APP "app" #define MQTT_TOPIC_INTERVAL "interval" -#define MQTT_TOPIC_HOSTNAME "hostname" +#define MQTT_TOPIC_HOSTNAME "host" +#define MQTT_TOPIC_TIME "time" #define MQTT_TOPIC_ANALOG "analog" // Periodic reports diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index d287869f..e044085c 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -42,7 +42,7 @@ void heartbeat() { unsigned long uptime_seconds = uptime_overflows * (UPTIME_OVERFLOW / 1000) + (last_uptime / 1000); unsigned int free_heap = ESP.getFreeHeap(); - DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) NTP.getTimeDateString().c_str()); + DEBUG_MSG_P(PSTR("[MAIN] Time: %s\n"), (char *) ntpDateTime().c_str()); if (!mqttConnected()) { DEBUG_MSG_P(PSTR("[MAIN] Uptime: %ld seconds\n"), uptime_seconds); DEBUG_MSG_P(PSTR("[MAIN] Free heap: %d bytes\n"), free_heap); @@ -62,7 +62,7 @@ void heartbeat() { mqttSend(MQTT_TOPIC_VERSION, APP_VERSION); #endif #if (MQTT_REPORT_HOSTNAME) - //mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str()); + mqttSend(MQTT_TOPIC_HOSTNAME, getSetting("hostname").c_str()); #endif #if (MQTT_REPORT_IP) mqttSend(MQTT_TOPIC_IP, getIP().c_str()); diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index d23198fe..d7321f29 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -99,8 +99,9 @@ void _mqttFlush() { mqtt_message_t element = _mqtt_queue[i]; root[element.topic] = element.message; } - root["time"] = NTP.getTimeDateString(); - root["hostname"] = getSetting("hostname", HOSTNAME); + if (ntpConnected()) root[MQTT_TOPIC_TIME] = ntpDateTime(); + root[MQTT_TOPIC_HOSTNAME] = getSetting("hostname", HOSTNAME); + root[MQTT_TOPIC_IP] = getIP(); String output; root.printTo(output); diff --git a/code/espurna/ntp.ino b/code/espurna/ntp.ino index 3067ab79..f42469e7 100644 --- a/code/espurna/ntp.ino +++ b/code/espurna/ntp.ino @@ -29,8 +29,21 @@ bool ntpConnected() { return (timeStatus() == timeSet); } -void ntpSetup() { +String ntpDateTime() { + if (!ntpConnected()) return String("Not set"); + String value = NTP.getTimeDateString(); + int hour = value.substring(0, 2).toInt(); + int minute = value.substring(3, 5).toInt(); + int second = value.substring(6, 8).toInt(); + int day = value.substring(9, 11).toInt(); + int month = value.substring(12, 14).toInt(); + int year = value.substring(15, 19).toInt(); + char buffer[20]; + sprintf(buffer, "%04d/%02d/%02dT%02d:%02d:%02d", year, month, day, hour, minute, second); + return String(buffer); +} +void ntpSetup() { NTP.onNTPSyncEvent([](NTPSyncEvent_t error) { if (error) { if (error == noResponse) { @@ -40,11 +53,10 @@ void ntpSetup() { } wsSend("{\"ntpStatus\": false}"); } else { - DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) NTP.getTimeDateString(NTP.getLastNTPSync()).c_str()); + DEBUG_MSG_P(PSTR("[NTP] Time: %s\n"), (char *) ntpDateTime().c_str()); wsSend("{\"ntpStatus\": true}"); } }); - } void ntpLoop() {