From f6a5bc9b407d42a8c6d430e9f859b587b3310afb Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Fri, 14 May 2021 20:57:56 +0200 Subject: [PATCH] Removed "retain" from MQTT messages. --- .../usermod_PIR_sensor_switch.h | 2 +- usermods/Temperature/usermod_temperature.h | 4 +- usermods/multi_relay/usermod_multi_relay.h | 4 +- wled00/mqtt.cpp | 41 +++++++++---------- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h b/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h index 19102a22..544b88d9 100644 --- a/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h +++ b/usermods/PIR_sensor_switch/usermod_PIR_sensor_switch.h @@ -139,7 +139,7 @@ private: char subuf[64]; strcpy(subuf, mqttDeviceTopic); strcat_P(subuf, PSTR("/motion")); - mqtt->publish(subuf, 0, true, state); + mqtt->publish(subuf, 0, false, state); } } diff --git a/usermods/Temperature/usermod_temperature.h b/usermods/Temperature/usermod_temperature.h index 298a8177..3a6bbfc4 100644 --- a/usermods/Temperature/usermod_temperature.h +++ b/usermods/Temperature/usermod_temperature.h @@ -162,9 +162,9 @@ class UsermodTemperature : public Usermod { // the DallasTemperature library returns -127C or -196.6F when problem // reading the sensor strcat_P(subuf, PSTR("/temperature")); - mqtt->publish(subuf, 0, true, String(temperature).c_str()); + mqtt->publish(subuf, 0, false, String(temperature).c_str()); strcat_P(subuf, PSTR("_f")); - mqtt->publish(subuf, 0, true, String((float)temperature * 1.8f + 32).c_str()); + mqtt->publish(subuf, 0, false, String((float)temperature * 1.8f + 32).c_str()); } else { // publish something else to indicate status? } diff --git a/usermods/multi_relay/usermod_multi_relay.h b/usermods/multi_relay/usermod_multi_relay.h index 7caff19f..42fe39e4 100644 --- a/usermods/multi_relay/usermod_multi_relay.h +++ b/usermods/multi_relay/usermod_multi_relay.h @@ -56,7 +56,7 @@ class MultiRelay : public Usermod { if (WLED_MQTT_CONNECTED){ char subuf[64]; sprintf_P(subuf, PSTR("%s/relay/%d"), mqttDeviceTopic, relay); - mqtt->publish(subuf, 0, true, state); + mqtt->publish(subuf, 0, false, state); } } @@ -88,7 +88,7 @@ class MultiRelay : public Usermod { DEBUG_PRINTLN("Relays: HTML API"); String janswer; String error = ""; - int params = request->params(); + //int params = request->params(); janswer = F("{\"NoOfRelays\":"); janswer += String(MULTI_RELAY_MAX_RELAYS) + ","; diff --git a/wled00/mqtt.cpp b/wled00/mqtt.cpp index 2754e940..76785665 100644 --- a/wled00/mqtt.cpp +++ b/wled00/mqtt.cpp @@ -52,24 +52,23 @@ void onMqttConnect(bool sessionPresent) } -void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { +void onMqttMessage(char* topic, char* payload0, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { DEBUG_PRINT(F("MQTT msg: ")); DEBUG_PRINTLN(topic); // paranoia check to avoid npe if no payload - if (payload==nullptr) { + if (payload0==nullptr) { DEBUG_PRINTLN(F("no payload -> leave")); return; } - DEBUG_PRINTLN(payload); // payload is not always null terminated - char *payload0 = new char[len+1]; - if (payload0==nullptr) return; // out of memory - strncpy(payload0,payload,len); - payload0[len] = '\0'; - DEBUG_PRINTLN(payload0); + char *payload = new char[len+1]; + if (payload==nullptr) return; // out of memory + strncpy(payload,payload0,len); + payload[len] = '\0'; + DEBUG_PRINTLN(payload); size_t topicPrefixLen = strlen(mqttDeviceTopic); if (strncmp(topic, mqttDeviceTopic, topicPrefixLen) == 0) { @@ -80,8 +79,8 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties topic += topicPrefixLen; } else { // Non-Wled Topic used here. Probably a usermod subscribed to this topic. - usermods.onMqttMessage(topic, payload0); - delete[] payload0; + usermods.onMqttMessage(topic, payload); + delete[] payload; return; } } @@ -89,26 +88,26 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties //Prefix is stripped from the topic at this point if (strcmp_P(topic, PSTR("/col")) == 0) { - colorFromDecOrHexString(col, payload0); + colorFromDecOrHexString(col, payload); colorUpdated(NOTIFIER_CALL_MODE_DIRECT_CHANGE); } else if (strcmp_P(topic, PSTR("/api")) == 0) { - if (payload0[0] == '{') { //JSON API + if (payload[0] == '{') { //JSON API DynamicJsonDocument doc(JSON_BUFFER_SIZE); - deserializeJson(doc, payload0); + deserializeJson(doc, payload); deserializeState(doc.as()); } else { //HTTP API String apireq = "win&"; - apireq += payload0; + apireq += payload; handleSet(nullptr, apireq); } } else if (strlen(topic) != 0) { // non standard topic, check with usermods - usermods.onMqttMessage(topic, payload0); + usermods.onMqttMessage(topic, payload); } else { // topmost topic (just wled/MAC) - parseMQTTBriPayload(payload0); + parseMQTTBriPayload(payload); } - delete[] payload0; + delete[] payload; } @@ -124,22 +123,22 @@ void publishMqtt() sprintf_P(s, PSTR("%u"), bri); strcpy(subuf, mqttDeviceTopic); strcat_P(subuf, PSTR("/g")); - mqtt->publish(subuf, 0, true, s); + mqtt->publish(subuf, 0, false, s); // do not retain message sprintf_P(s, PSTR("#%06X"), (col[3] << 24) | (col[0] << 16) | (col[1] << 8) | (col[2])); strcpy(subuf, mqttDeviceTopic); strcat_P(subuf, PSTR("/c")); - mqtt->publish(subuf, 0, true, s); + mqtt->publish(subuf, 0, false, s); // do not retain message strcpy(subuf, mqttDeviceTopic); strcat_P(subuf, PSTR("/status")); - mqtt->publish(subuf, 0, true, "online"); + mqtt->publish(subuf, 0, false, "online"); // do not retain message char apires[1024]; XML_response(nullptr, apires); strcpy(subuf, mqttDeviceTopic); strcat_P(subuf, PSTR("/v")); - mqtt->publish(subuf, 0, true, apires); + mqtt->publish(subuf, 0, false, apires); // do not retain message }