diff --git a/code/espurna/button.ino b/code/espurna/button.ino index 657cc197..cd94082d 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -27,7 +27,7 @@ void buttonMQTT(unsigned char id, uint8_t event) { if (id >= _buttons.size()) return; char payload[2]; itoa(event, payload, 10); - mqttSend(MQTT_TOPIC_BUTTON, id, payload); + mqttSend(MQTT_TOPIC_BUTTON, id, payload, false, false); // 1st bool = force, 2nd = retain } #endif diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index b7814b3d..ea53f7af 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -484,18 +484,24 @@ String mqttTopic(const char * magnitude, unsigned int index, bool is_set) { // ----------------------------------------------------------------------------- -void mqttSendRaw(const char * topic, const char * message) { +void mqttSendRaw(const char * topic, const char * message, bool retain) { + if (_mqtt.connected()) { #if MQTT_USE_ASYNC - unsigned int packetId = _mqtt.publish(topic, _mqtt_qos, _mqtt_retain, message); + unsigned int packetId = _mqtt.publish(topic, _mqtt_qos, retain, message); DEBUG_MSG_P(PSTR("[MQTT] Sending %s => %s (PID %d)\n"), topic, message, packetId); #else - _mqtt.publish(topic, message, _mqtt_retain); + _mqtt.publish(topic, message, retain); DEBUG_MSG_P(PSTR("[MQTT] Sending %s => %s\n"), topic, message); #endif } } + +void mqttSendRaw(const char * topic, const char * message) { + mqttSendRaw (topic, message, _mqtt_retain); +} + void mqttFlush() { if (!_mqtt.connected()) return; @@ -567,7 +573,7 @@ void mqttEnqueue(const char * topic, const char * message) { } -void mqttSend(const char * topic, const char * message, bool force) { +void mqttSend(const char * topic, const char * message, bool force, bool retain) { bool useJson = force ? false : _mqtt_use_json; @@ -585,20 +591,28 @@ void mqttSend(const char * topic, const char * message, bool force) { // Send it right away } else { - mqttSendRaw(mqttTopic(topic, false).c_str(), message); + mqttSendRaw(mqttTopic(topic, false).c_str(), message, retain); } } +void mqttSend(const char * topic, const char * message, bool force) { + mqttSend(topic, message, force, _mqtt_retain); +} + void mqttSend(const char * topic, const char * message) { mqttSend(topic, message, false); } -void mqttSend(const char * topic, unsigned int index, const char * message, bool force) { +void mqttSend(const char * topic, unsigned int index, const char * message, bool force, bool retain) { char buffer[strlen(topic)+5]; snprintf_P(buffer, sizeof(buffer), PSTR("%s/%d"), topic, index); - mqttSend(buffer, message, force); + mqttSend(buffer, message, force, retain); +} + +void mqttSend(const char * topic, unsigned int index, const char * message, bool force) { + mqttSend(topic, index, message, force, _mqtt_retain); } void mqttSend(const char * topic, unsigned int index, const char * message) {