From 9321b53e40e087b25371617e4f45c084b50eae7f Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 12 Mar 2020 14:35:08 +0100 Subject: [PATCH] start topic show datetime #347 #348 --- src/MyESP.cpp | 49 ++++++++++++++++++++++++++++++++++++------------- src/MyESP.h | 3 ++- src/ems-esp.cpp | 12 ++++++------ src/ems.cpp | 1 + src/version.h | 2 +- 5 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/MyESP.cpp b/src/MyESP.cpp index db5428c69..3a4958758 100644 --- a/src/MyESP.cpp +++ b/src/MyESP.cpp @@ -590,6 +590,36 @@ void MyESP::_mqttPublishQueue() { _mqttRemoveLastPublish(); } +// send online appended with the version information as JSON +void MyESP::_sendStartTopic() { + StaticJsonDocument doc; + JsonObject payload = doc.to(); + payload["version"] = _app_version; + payload["IP"] = WiFi.localIP().toString(); + // add time if we know it + if ((_ntp_enabled) && (NTP.tcr->abbrev != nullptr)) { + uint32_t real_time = getSystemTime(); + // exclude millis() just in case + if (real_time > 10000L) { + char s[25]; + // ISO 8601 describes an internationally accepted way to represent dates and times using numbers + snprintf_P(s, + 25, + PSTR("%04u-%02u-%02uT%02u:%02u:%02u"), + to_year(real_time), + to_month(real_time), + to_day(real_time), + to_hour(real_time), + to_minute(real_time), + to_second(real_time) + + ); + payload["boottime"] = s; + } + } + mqttPublish(MQTT_TOPIC_START, doc, true); // send with retain on +} + // MQTT onConnect - when a connect is established void MyESP::_mqttOnConnect() { myDebug_P(PSTR("[MQTT] MQTT connected")); @@ -607,19 +637,10 @@ void MyESP::_mqttOnConnect() { // forcing retain to off since we only want to send this once mqttSubscribe(MQTT_TOPIC_START); - // send online appended with the version information as JSON - StaticJsonDocument doc; - JsonObject payload = doc.to(); - payload["version"] = _app_version; - payload["IP"] = WiFi.localIP().toString(); - // add time if we know it - if ((_ntp_enabled) && (NTP.tcr->abbrev != nullptr)) { - uint32_t real_time = getSystemTime(); - char s[30]; - snprintf_P(s, 30, PSTR("%02d:%02d:%02d %s"), to_hour(real_time), to_minute(real_time), to_second(real_time), NTP.tcr->abbrev); - payload["boottime"] = s; + // send start topic now unless NTP is enabled, otherwise wait for the time + if (!_ntp_enabled) { + _sendStartTopic(); } - mqttPublish(MQTT_TOPIC_START, doc, false); // send heartbeat if enabled heartbeatCheck(true); @@ -2868,9 +2889,11 @@ void MyESP::_bootupSequence() { // check if its booted if (boot_status == MYESP_BOOTSTATUS_BOOTED) { - if ((_ntp_enabled) && (now() > 10000) && !_have_ntp_time) { + if ((_ntp_enabled) && (now() > 10000L) && !_have_ntp_time) { _have_ntp_time = true; writeLogEvent(MYESP_SYSLOG_INFO, "System booted"); + // send start topic + _sendStartTopic(); } return; } diff --git a/src/MyESP.h b/src/MyESP.h index 51729e17c..1568860d4 100644 --- a/src/MyESP.h +++ b/src/MyESP.h @@ -9,7 +9,7 @@ #ifndef MyESP_h #define MyESP_h -#define MYESP_VERSION "1.2.35" +#define MYESP_VERSION "1.2.36" #include #include @@ -347,6 +347,7 @@ class MyESP { void _printMQTTQueue(); void _mqttPublishQueue(); void _mqttRemoveLastPublish(); + void _sendStartTopic(); AsyncMqttClient mqttClient; // the MQTT class uint32_t _mqtt_reconnect_delay; mqtt_callback_f _mqtt_callback_f; diff --git a/src/ems-esp.cpp b/src/ems-esp.cpp index f0c36ff9a..44d113c04 100644 --- a/src/ems-esp.cpp +++ b/src/ems-esp.cpp @@ -798,8 +798,8 @@ bool publishEMSValues_boiler() { // last_boilerActive stores heating in bit 1 and tap water in bit 2 static uint8_t last_boilerActive = 0xFF; // for remembering last setting of the tap water or heating on/off if (last_boilerActive != ((EMS_Boiler.tapwaterActive << 1) + EMS_Boiler.heatingActive)) { - myESP.mqttPublish(TOPIC_BOILER_TAPWATER_ACTIVE, EMS_Boiler.tapwaterActive == 1 ? "1" : "0"); - myESP.mqttPublish(TOPIC_BOILER_HEATING_ACTIVE, EMS_Boiler.heatingActive == 1 ? "1" : "0"); + myESP.mqttPublish(TOPIC_BOILER_TAPWATER_ACTIVE, EMS_Boiler.tapwaterActive ? "1" : "0"); + myESP.mqttPublish(TOPIC_BOILER_HEATING_ACTIVE, EMS_Boiler.heatingActive ? "1" : "0"); last_boilerActive = ((EMS_Boiler.tapwaterActive << 1) + EMS_Boiler.heatingActive); // remember last state } @@ -1908,9 +1908,9 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) { // wwOneTime if (strcmp(topic, TOPIC_BOILER_CMD_WWONETIME) == 0) { - if (message[0] == '1' || strcmp(message, "on") == 0) { + if (message[0] == MYESP_MQTT_PAYLOAD_ON || strcmp(message, "on") == 0) { ems_setWarmWaterOnetime(true); - } else if (message[0] == '0' || strcmp(message, "off") == 0) { + } else if (message[0] == MYESP_MQTT_PAYLOAD_OFF || strcmp(message, "off") == 0) { ems_setWarmWaterOnetime(false); } return; @@ -1918,9 +1918,9 @@ void MQTTCallback(unsigned int type, const char * topic, const char * message) { // wwCirculation if (strcmp(topic, TOPIC_BOILER_CMD_WWCIRCULATION) == 0) { - if (message[0] == '1' || strcmp(message, "on") == 0) { + if (message[0] == MYESP_MQTT_PAYLOAD_ON || strcmp(message, "on") == 0) { ems_setWarmWaterCirculation(true); - } else if (message[0] == '0' || strcmp(message, "off") == 0) { + } else if (message[0] == MYESP_MQTT_PAYLOAD_OFF || strcmp(message, "off") == 0) { ems_setWarmWaterCirculation(false); } return; diff --git a/src/ems.cpp b/src/ems.cpp index ef973ad06..72cf98114 100644 --- a/src/ems.cpp +++ b/src/ems.cpp @@ -953,6 +953,7 @@ void _checkActive() { if (EMS_Boiler.selFlowTemp != EMS_VALUE_INT_NOTSET && EMS_Boiler.burnGas != EMS_VALUE_INT_NOTSET) { EMS_Boiler.heatingActive = ((EMS_Boiler.selFlowTemp >= EMS_BOILER_SELFLOWTEMP_HEATING) && (EMS_Boiler.burnGas == EMS_VALUE_BOOL_ON)); } + } /** diff --git a/src/version.h b/src/version.h index c8d009f73..bf444aad1 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define APP_VERSION "1.9.5b54" +#define APP_VERSION "1.9.5b55"