use flash strings for MQTT topics

This commit is contained in:
proddy
2020-09-13 11:05:18 +02:00
parent 5b772985bb
commit 802b466160
9 changed files with 35 additions and 16 deletions

View File

@@ -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_);
}
}
}

View File

@@ -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);
}
}

View File

@@ -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<JsonObject>(); // 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);
}
}

View File

@@ -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<const Telegram> telegram) {
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> 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<const Telegram> telegram) {
}
doc["value"] = value;
}
Mqtt::publish("response", doc);
Mqtt::publish(F("response"), doc);
}
// search for recognized device_ids : Me, All, otherwise print hex value

View File

@@ -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) {

View File

@@ -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);

View File

@@ -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

View File

@@ -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

View File

@@ -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