Removed "retain" from MQTT messages.

This commit is contained in:
Blaz Kristan 2021-05-14 20:57:56 +02:00
parent 9a5917a331
commit f6a5bc9b40
4 changed files with 25 additions and 26 deletions

View File

@ -139,7 +139,7 @@ private:
char subuf[64]; char subuf[64];
strcpy(subuf, mqttDeviceTopic); strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/motion")); strcat_P(subuf, PSTR("/motion"));
mqtt->publish(subuf, 0, true, state); mqtt->publish(subuf, 0, false, state);
} }
} }

View File

@ -162,9 +162,9 @@ class UsermodTemperature : public Usermod {
// the DallasTemperature library returns -127C or -196.6F when problem // the DallasTemperature library returns -127C or -196.6F when problem
// reading the sensor // reading the sensor
strcat_P(subuf, PSTR("/temperature")); 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")); 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 { } else {
// publish something else to indicate status? // publish something else to indicate status?
} }

View File

@ -56,7 +56,7 @@ class MultiRelay : public Usermod {
if (WLED_MQTT_CONNECTED){ if (WLED_MQTT_CONNECTED){
char subuf[64]; char subuf[64];
sprintf_P(subuf, PSTR("%s/relay/%d"), mqttDeviceTopic, relay); 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"); DEBUG_PRINTLN("Relays: HTML API");
String janswer; String janswer;
String error = ""; String error = "";
int params = request->params(); //int params = request->params();
janswer = F("{\"NoOfRelays\":"); janswer = F("{\"NoOfRelays\":");
janswer += String(MULTI_RELAY_MAX_RELAYS) + ","; janswer += String(MULTI_RELAY_MAX_RELAYS) + ",";

View File

@ -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_PRINT(F("MQTT msg: "));
DEBUG_PRINTLN(topic); DEBUG_PRINTLN(topic);
// paranoia check to avoid npe if no payload // paranoia check to avoid npe if no payload
if (payload==nullptr) { if (payload0==nullptr) {
DEBUG_PRINTLN(F("no payload -> leave")); DEBUG_PRINTLN(F("no payload -> leave"));
return; return;
} }
DEBUG_PRINTLN(payload);
// payload is not always null terminated // payload is not always null terminated
char *payload0 = new char[len+1]; char *payload = new char[len+1];
if (payload0==nullptr) return; // out of memory if (payload==nullptr) return; // out of memory
strncpy(payload0,payload,len); strncpy(payload,payload0,len);
payload0[len] = '\0'; payload[len] = '\0';
DEBUG_PRINTLN(payload0); DEBUG_PRINTLN(payload);
size_t topicPrefixLen = strlen(mqttDeviceTopic); size_t topicPrefixLen = strlen(mqttDeviceTopic);
if (strncmp(topic, mqttDeviceTopic, topicPrefixLen) == 0) { if (strncmp(topic, mqttDeviceTopic, topicPrefixLen) == 0) {
@ -80,8 +79,8 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
topic += topicPrefixLen; topic += topicPrefixLen;
} else { } else {
// Non-Wled Topic used here. Probably a usermod subscribed to this topic. // Non-Wled Topic used here. Probably a usermod subscribed to this topic.
usermods.onMqttMessage(topic, payload0); usermods.onMqttMessage(topic, payload);
delete[] payload0; delete[] payload;
return; return;
} }
} }
@ -89,26 +88,26 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties
//Prefix is stripped from the topic at this point //Prefix is stripped from the topic at this point
if (strcmp_P(topic, PSTR("/col")) == 0) { if (strcmp_P(topic, PSTR("/col")) == 0) {
colorFromDecOrHexString(col, payload0); colorFromDecOrHexString(col, payload);
colorUpdated(NOTIFIER_CALL_MODE_DIRECT_CHANGE); colorUpdated(NOTIFIER_CALL_MODE_DIRECT_CHANGE);
} else if (strcmp_P(topic, PSTR("/api")) == 0) { } else if (strcmp_P(topic, PSTR("/api")) == 0) {
if (payload0[0] == '{') { //JSON API if (payload[0] == '{') { //JSON API
DynamicJsonDocument doc(JSON_BUFFER_SIZE); DynamicJsonDocument doc(JSON_BUFFER_SIZE);
deserializeJson(doc, payload0); deserializeJson(doc, payload);
deserializeState(doc.as<JsonObject>()); deserializeState(doc.as<JsonObject>());
} else { //HTTP API } else { //HTTP API
String apireq = "win&"; String apireq = "win&";
apireq += payload0; apireq += payload;
handleSet(nullptr, apireq); handleSet(nullptr, apireq);
} }
} else if (strlen(topic) != 0) { } else if (strlen(topic) != 0) {
// non standard topic, check with usermods // non standard topic, check with usermods
usermods.onMqttMessage(topic, payload0); usermods.onMqttMessage(topic, payload);
} else { } else {
// topmost topic (just wled/MAC) // 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); sprintf_P(s, PSTR("%u"), bri);
strcpy(subuf, mqttDeviceTopic); strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/g")); 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])); sprintf_P(s, PSTR("#%06X"), (col[3] << 24) | (col[0] << 16) | (col[1] << 8) | (col[2]));
strcpy(subuf, mqttDeviceTopic); strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/c")); strcat_P(subuf, PSTR("/c"));
mqtt->publish(subuf, 0, true, s); mqtt->publish(subuf, 0, false, s); // do not retain message
strcpy(subuf, mqttDeviceTopic); strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/status")); strcat_P(subuf, PSTR("/status"));
mqtt->publish(subuf, 0, true, "online"); mqtt->publish(subuf, 0, false, "online"); // do not retain message
char apires[1024]; char apires[1024];
XML_response(nullptr, apires); XML_response(nullptr, apires);
strcpy(subuf, mqttDeviceTopic); strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/v")); strcat_P(subuf, PSTR("/v"));
mqtt->publish(subuf, 0, true, apires); mqtt->publish(subuf, 0, false, apires); // do not retain message
} }