mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
added option to overide MQTT retain (e.g. in shower time and heartbeat messages)
This commit is contained in:
@@ -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
|
// MQTT Publish
|
||||||
// returns true if all good
|
// 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)) {
|
if (mqttClient.connected() && (strlen(topic) > 0)) {
|
||||||
//myDebug_P(PSTR("[MQTT] Sending publish to %s with payload %s"), _mqttTopic(topic), payload); // for debugging
|
//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) {
|
if (packet_id) {
|
||||||
_addMQTTLog(topic, payload, 1); // add to the log, using type of 1 for Publish
|
_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();
|
_mqtt_last_connection = millis();
|
||||||
|
|
||||||
// say we're alive to the Last Will topic
|
// 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
|
// subscribe to general subs
|
||||||
mqttSubscribe(MQTT_TOPIC_RESTART);
|
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, itoa(mem_available, s, 10), sizeof(payload)); // free mem as a %
|
||||||
strlcat(payload, "%", sizeof(payload));
|
strlcat(payload, "%", sizeof(payload));
|
||||||
|
|
||||||
mqttPublish(MQTT_TOPIC_HEARTBEAT, payload); // send to MQTT
|
mqttPublish(MQTT_TOPIC_HEARTBEAT, payload, false); // send to MQTT with retain off
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
#ifndef MyESP_h
|
#ifndef MyESP_h
|
||||||
#define MyESP_h
|
#define MyESP_h
|
||||||
|
|
||||||
#define MYESP_VERSION "1.2.11"
|
#define MYESP_VERSION "1.2.12"
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
@@ -275,6 +275,7 @@ class MyESP {
|
|||||||
bool mqttSubscribe(const char * topic);
|
bool mqttSubscribe(const char * topic);
|
||||||
void mqttUnsubscribe(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 mqttPublish(const char * topic, const char * payload, bool retain);
|
||||||
void setMQTT(mqtt_callback_f callback);
|
void setMQTT(mqtt_callback_f callback);
|
||||||
|
|
||||||
// OTA
|
// OTA
|
||||||
|
|||||||
@@ -1076,7 +1076,8 @@ bool do_publishShowerData() {
|
|||||||
|
|
||||||
myDebugLog("Publishing shower data via MQTT");
|
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
|
// callback for custom settings when showing Stored Settings with the 'set' command
|
||||||
|
|||||||
Reference in New Issue
Block a user