From 00e707ee2191621e5758cfdcf56601eae677ff4c Mon Sep 17 00:00:00 2001 From: proddy Date: Sat, 19 Oct 2019 09:35:37 +0200 Subject: [PATCH] added option to overide MQTT retain (e.g. in shower time and heartbeat messages) --- src/MyESP.cpp | 14 ++++++++++---- src/MyESP.h | 3 ++- src/ems-esp.cpp | 3 ++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/MyESP.cpp b/src/MyESP.cpp index 9d4e2cbb0..6f802640d 100644 --- a/src/MyESP.cpp +++ b/src/MyESP.cpp @@ -397,12 +397,18 @@ void MyESP::mqttUnsubscribe(const char * topic) { } } +// Publish using the user's custom retain flag +bool MyESP::mqttPublish(const char * topic, const char * payload) { + // use the custom MQTT retain flag + return mqttPublish(topic, payload, _mqtt_retain); +} + // MQTT Publish // returns true if all good -bool MyESP::mqttPublish(const char * topic, const char * payload) { +bool MyESP::mqttPublish(const char * topic, const char * payload, bool retain) { if (mqttClient.connected() && (strlen(topic) > 0)) { //myDebug_P(PSTR("[MQTT] Sending publish to %s with payload %s"), _mqttTopic(topic), payload); // for debugging - uint16_t packet_id = mqttClient.publish(_mqttTopic(topic), _mqtt_qos, _mqtt_retain, payload); + uint16_t packet_id = mqttClient.publish(_mqttTopic(topic), _mqtt_qos, retain, payload); if (packet_id) { _addMQTTLog(topic, payload, 1); // add to the log, using type of 1 for Publish @@ -423,7 +429,7 @@ void MyESP::_mqttOnConnect() { _mqtt_last_connection = millis(); // say we're alive to the Last Will topic - mqttClient.publish(_mqttTopic(_mqtt_will_topic), 1, true, _mqtt_will_online_payload); // qos=1, retain=true + mqttPublish(_mqtt_will_topic, _mqtt_will_online_payload, true); // force retain on // subscribe to general subs mqttSubscribe(MQTT_TOPIC_RESTART); @@ -1365,7 +1371,7 @@ void MyESP::_heartbeatCheck(bool force = false) { strlcat(payload, itoa(mem_available, s, 10), sizeof(payload)); // free mem as a % strlcat(payload, "%", sizeof(payload)); - mqttPublish(MQTT_TOPIC_HEARTBEAT, payload); // send to MQTT + mqttPublish(MQTT_TOPIC_HEARTBEAT, payload, false); // send to MQTT with retain off } } diff --git a/src/MyESP.h b/src/MyESP.h index 398483950..a9abeaffa 100644 --- a/src/MyESP.h +++ b/src/MyESP.h @@ -9,7 +9,7 @@ #ifndef MyESP_h #define MyESP_h -#define MYESP_VERSION "1.2.11" +#define MYESP_VERSION "1.2.12" #include #include @@ -275,6 +275,7 @@ class MyESP { bool mqttSubscribe(const char * topic); void mqttUnsubscribe(const char * topic); bool mqttPublish(const char * topic, const char * payload); + bool mqttPublish(const char * topic, const char * payload, bool retain); void setMQTT(mqtt_callback_f callback); // OTA diff --git a/src/ems-esp.cpp b/src/ems-esp.cpp index 7060788e6..838de20f0 100644 --- a/src/ems-esp.cpp +++ b/src/ems-esp.cpp @@ -1076,7 +1076,8 @@ bool do_publishShowerData() { myDebugLog("Publishing shower data via MQTT"); - return (myESP.mqttPublish(TOPIC_SHOWER_DATA, data)); + // Publish MQTT forcing retain to be off + return (myESP.mqttPublish(TOPIC_SHOWER_DATA, data, false)); } // callback for custom settings when showing Stored Settings with the 'set' command