diff --git a/src/devices/boiler.cpp b/src/devices/boiler.cpp index bed02c8c0..d092c1803 100644 --- a/src/devices/boiler.cpp +++ b/src/devices/boiler.cpp @@ -324,7 +324,7 @@ void Boiler::publish_values() { // if we have data, publish it if (!doc.isNull()) { - Mqtt::publish("boiler_data", doc); + Mqtt::publish(F("boiler_data"), doc); } } @@ -476,8 +476,8 @@ void Boiler::check_active() { uint8_t latest_boilerState = (tap_water_active_ << 1) + heating_active_; if (latest_boilerState != last_boilerState) { last_boilerState = latest_boilerState; - Mqtt::publish("tapwater_active", tap_water_active_); - Mqtt::publish("heating_active", heating_active_); + Mqtt::publish(F("tapwater_active"), tap_water_active_); + Mqtt::publish(F("heating_active"), heating_active_); } } } diff --git a/src/devices/solar.cpp b/src/devices/solar.cpp index cee2d1849..f563b02c3 100644 --- a/src/devices/solar.cpp +++ b/src/devices/solar.cpp @@ -170,7 +170,7 @@ void Solar::publish_values() { // if we have data, publish it if (!doc.isNull()) { - Mqtt::publish("sm_data", doc); + Mqtt::publish(F("sm_data"), doc); } } diff --git a/src/devices/thermostat.cpp b/src/devices/thermostat.cpp index fb90f8d3b..d135f7b4a 100644 --- a/src/devices/thermostat.cpp +++ b/src/devices/thermostat.cpp @@ -304,7 +304,7 @@ void Thermostat::publish_values() { // send this specific data using the thermostat_data topic if (mqtt_format_ != MQTT_format::NESTED) { - Mqtt::publish("thermostat_data", doc); + Mqtt::publish(F("thermostat_data"), doc); rootThermostat = doc.to(); // clear object } } @@ -446,7 +446,7 @@ void Thermostat::publish_values() { // if we're using nested json, send all in one go under one topic called thermostat_data if (mqtt_format_ == MQTT_format::NESTED) { - Mqtt::publish("thermostat_data", doc); + Mqtt::publish(F("thermostat_data"), doc); } } diff --git a/src/emsesp.cpp b/src/emsesp.cpp index ac4ed0dac..a422dbbaf 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -316,15 +316,18 @@ void EMSESP::publish_sensor_values(const bool force) { } } +// MQTT publish a telegram as raw data void EMSESP::publish_response(std::shared_ptr telegram) { StaticJsonDocument doc; + char buffer[100]; doc["src"] = Helpers::hextoa(buffer, telegram->src); doc["dest"] = Helpers::hextoa(buffer, telegram->dest); doc["type"] = Helpers::hextoa(buffer, telegram->type_id); doc["offset"] = Helpers::hextoa(buffer, telegram->offset); strcpy(buffer, Helpers::data_to_hex(telegram->message_data, telegram->message_length).c_str()); - doc["data"] = buffer; + doc["data"] = buffer; + if (telegram->message_length <= 4) { uint32_t value = 0; for (uint8_t i = 0; i < telegram->message_length; i++) { @@ -332,7 +335,8 @@ void EMSESP::publish_response(std::shared_ptr telegram) { } doc["value"] = value; } - Mqtt::publish("response", doc); + + Mqtt::publish(F("response"), doc); } // search for recognized device_ids : Me, All, otherwise print hex value diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 69dd533c2..b33b5b980 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -479,9 +479,9 @@ void Mqtt::on_connect() { #ifndef EMSESP_STANDALONE doc["ip"] = WiFi.localIP().toString(); #endif - publish("info", doc, false); // send with retain off + publish(F("info"), doc, false); // send with retain off - publish("status", "online", true); // say we're alive to the Last Will topic, with retain on + publish(F("status"), "online", true); // say we're alive to the Last Will topic, with retain on reset_publish_fails(); // reset fail count to 0 @@ -538,6 +538,15 @@ void Mqtt::publish(const std::string & topic, const std::string & payload, bool queue_publish_message(topic, payload, retain); } +// MQTT Publish, using a specific retain flag, topic is a flash string +void Mqtt::publish(const __FlashStringHelper * topic, const std::string & payload, bool retain) { + queue_publish_message(uuid::read_flash_string(topic), payload, retain); +} + +void Mqtt::publish(const __FlashStringHelper * topic, const JsonDocument & payload, bool retain) { + publish(uuid::read_flash_string(topic), payload, retain); +} + void Mqtt::publish(const std::string & topic, const JsonDocument & payload, bool retain) { std::string payload_text; serializeJson(payload, payload_text); // convert json to string @@ -548,6 +557,9 @@ void Mqtt::publish(const std::string & topic, const JsonDocument & payload, bool void Mqtt::publish(const std::string & topic, const bool value) { queue_publish_message(topic, value ? "1" : "0", false); } +void Mqtt::publish(const __FlashStringHelper * topic, const bool value) { + queue_publish_message(uuid::read_flash_string(topic), value ? "1" : "0", false); +} // no payload void Mqtt::publish(const std::string & topic) { diff --git a/src/mqtt.h b/src/mqtt.h index f7a53b33a..e37b6502a 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -88,7 +88,10 @@ class Mqtt { static void publish(const std::string & topic, const std::string & payload, bool retain = false); static void publish(const std::string & topic, const JsonDocument & payload, bool retain = false); + static void publish(const __FlashStringHelper * topic, const JsonDocument & payload, bool retain = false); + static void publish(const __FlashStringHelper * topic, const std::string & payload, bool retain = false); static void publish(const std::string & topic, const bool value); + static void publish(const __FlashStringHelper * topi, const bool value); static void publish(const std::string & topic); static void show_topic_handlers(uuid::console::Shell & shell, const uint8_t device_type); diff --git a/src/sensors.cpp b/src/sensors.cpp index bdd08fa75..79ae8de9c 100644 --- a/src/sensors.cpp +++ b/src/sensors.cpp @@ -347,9 +347,9 @@ void Sensors::publish_values() { } if ((mqtt_format_ == MQTT_format::NESTED) || (mqtt_format_ == MQTT_format::CUSTOM)) { - Mqtt::publish("sensors", doc); + Mqtt::publish(F("sensors"), doc); } else if (mqtt_format_ == MQTT_format::HA) { - Mqtt::publish("homeassistant/sensor/ems-esp/state", doc); + Mqtt::publish(F("homeassistant/sensor/ems-esp/state"), doc); } } } // namespace emsesp \ No newline at end of file diff --git a/src/shower.cpp b/src/shower.cpp index 2d8e33ef2..4451e0a71 100644 --- a/src/shower.cpp +++ b/src/shower.cpp @@ -53,7 +53,7 @@ void Shower::loop() { // first check to see if hot water has been on long enough to be recognized as a Shower/Bath if (!shower_on_ && (time_now - timer_start_) > SHOWER_MIN_DURATION) { shower_on_ = true; - Mqtt::publish("shower_active", (bool)true); + Mqtt::publish(F("shower_active"), (bool)true); LOG_DEBUG(F("[Shower] hot water still running, starting shower timer")); } // check if the shower has been on too long @@ -74,7 +74,7 @@ void Shower::loop() { if ((timer_pause_ - timer_start_) > SHOWER_OFFSET_TIME) { duration_ = (timer_pause_ - timer_start_ - SHOWER_OFFSET_TIME); if (duration_ > SHOWER_MIN_DURATION) { - Mqtt::publish("shower_active", (bool)false); + Mqtt::publish(F("shower_active"), (bool)false); LOG_DEBUG(F("[Shower] finished with duration %d"), duration_); publish_values(); } @@ -129,7 +129,7 @@ void Shower::publish_values() { doc["duration"] = s; } - Mqtt::publish("shower_data", doc); + Mqtt::publish(F("shower_data"), doc); } } // namespace emsesp diff --git a/src/system.cpp b/src/system.cpp index bab8ce199..ba5709e33 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -237,7 +237,7 @@ void System::send_heartbeat() { doc["rxfails"] = EMSESP::rxservice_.telegram_error_count(); doc["adc"] = analog_; //analogRead(A0); - Mqtt::publish("heartbeat", doc, false); // send to MQTT with retain off. This will add to MQTT queue. + Mqtt::publish(F("heartbeat"), doc, false); // send to MQTT with retain off. This will add to MQTT queue. } // measure and moving average adc