mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
use flash strings for MQTT topics
This commit is contained in:
@@ -324,7 +324,7 @@ void Boiler::publish_values() {
|
|||||||
|
|
||||||
// if we have data, publish it
|
// if we have data, publish it
|
||||||
if (!doc.isNull()) {
|
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_;
|
uint8_t latest_boilerState = (tap_water_active_ << 1) + heating_active_;
|
||||||
if (latest_boilerState != last_boilerState) {
|
if (latest_boilerState != last_boilerState) {
|
||||||
last_boilerState = latest_boilerState;
|
last_boilerState = latest_boilerState;
|
||||||
Mqtt::publish("tapwater_active", tap_water_active_);
|
Mqtt::publish(F("tapwater_active"), tap_water_active_);
|
||||||
Mqtt::publish("heating_active", heating_active_);
|
Mqtt::publish(F("heating_active"), heating_active_);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ void Solar::publish_values() {
|
|||||||
|
|
||||||
// if we have data, publish it
|
// if we have data, publish it
|
||||||
if (!doc.isNull()) {
|
if (!doc.isNull()) {
|
||||||
Mqtt::publish("sm_data", doc);
|
Mqtt::publish(F("sm_data"), doc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -304,7 +304,7 @@ void Thermostat::publish_values() {
|
|||||||
|
|
||||||
// send this specific data using the thermostat_data topic
|
// send this specific data using the thermostat_data topic
|
||||||
if (mqtt_format_ != MQTT_format::NESTED) {
|
if (mqtt_format_ != MQTT_format::NESTED) {
|
||||||
Mqtt::publish("thermostat_data", doc);
|
Mqtt::publish(F("thermostat_data"), doc);
|
||||||
rootThermostat = doc.to<JsonObject>(); // clear object
|
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 we're using nested json, send all in one go under one topic called thermostat_data
|
||||||
if (mqtt_format_ == MQTT_format::NESTED) {
|
if (mqtt_format_ == MQTT_format::NESTED) {
|
||||||
Mqtt::publish("thermostat_data", doc);
|
Mqtt::publish(F("thermostat_data"), doc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -316,8 +316,10 @@ 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) {
|
void EMSESP::publish_response(std::shared_ptr<const Telegram> telegram) {
|
||||||
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> doc;
|
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_SMALL> doc;
|
||||||
|
|
||||||
char buffer[100];
|
char buffer[100];
|
||||||
doc["src"] = Helpers::hextoa(buffer, telegram->src);
|
doc["src"] = Helpers::hextoa(buffer, telegram->src);
|
||||||
doc["dest"] = Helpers::hextoa(buffer, telegram->dest);
|
doc["dest"] = Helpers::hextoa(buffer, telegram->dest);
|
||||||
@@ -325,6 +327,7 @@ void EMSESP::publish_response(std::shared_ptr<const Telegram> telegram) {
|
|||||||
doc["offset"] = Helpers::hextoa(buffer, telegram->offset);
|
doc["offset"] = Helpers::hextoa(buffer, telegram->offset);
|
||||||
strcpy(buffer, Helpers::data_to_hex(telegram->message_data, telegram->message_length).c_str());
|
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) {
|
if (telegram->message_length <= 4) {
|
||||||
uint32_t value = 0;
|
uint32_t value = 0;
|
||||||
for (uint8_t i = 0; i < telegram->message_length; i++) {
|
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;
|
doc["value"] = value;
|
||||||
}
|
}
|
||||||
Mqtt::publish("response", doc);
|
|
||||||
|
Mqtt::publish(F("response"), doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
// search for recognized device_ids : Me, All, otherwise print hex value
|
// search for recognized device_ids : Me, All, otherwise print hex value
|
||||||
|
|||||||
16
src/mqtt.cpp
16
src/mqtt.cpp
@@ -479,9 +479,9 @@ void Mqtt::on_connect() {
|
|||||||
#ifndef EMSESP_STANDALONE
|
#ifndef EMSESP_STANDALONE
|
||||||
doc["ip"] = WiFi.localIP().toString();
|
doc["ip"] = WiFi.localIP().toString();
|
||||||
#endif
|
#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
|
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);
|
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) {
|
void Mqtt::publish(const std::string & topic, const JsonDocument & payload, bool retain) {
|
||||||
std::string payload_text;
|
std::string payload_text;
|
||||||
serializeJson(payload, payload_text); // convert json to string
|
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) {
|
void Mqtt::publish(const std::string & topic, const bool value) {
|
||||||
queue_publish_message(topic, value ? "1" : "0", false);
|
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
|
// no payload
|
||||||
void Mqtt::publish(const std::string & topic) {
|
void Mqtt::publish(const std::string & topic) {
|
||||||
|
|||||||
@@ -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 std::string & payload, bool retain = false);
|
||||||
static void publish(const std::string & topic, const JsonDocument & 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 std::string & topic, const bool value);
|
||||||
|
static void publish(const __FlashStringHelper * topi, const bool value);
|
||||||
static void publish(const std::string & topic);
|
static void publish(const std::string & topic);
|
||||||
|
|
||||||
static void show_topic_handlers(uuid::console::Shell & shell, const uint8_t device_type);
|
static void show_topic_handlers(uuid::console::Shell & shell, const uint8_t device_type);
|
||||||
|
|||||||
@@ -347,9 +347,9 @@ void Sensors::publish_values() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((mqtt_format_ == MQTT_format::NESTED) || (mqtt_format_ == MQTT_format::CUSTOM)) {
|
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) {
|
} 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
|
} // namespace emsesp
|
||||||
@@ -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
|
// 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) {
|
if (!shower_on_ && (time_now - timer_start_) > SHOWER_MIN_DURATION) {
|
||||||
shower_on_ = true;
|
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"));
|
LOG_DEBUG(F("[Shower] hot water still running, starting shower timer"));
|
||||||
}
|
}
|
||||||
// check if the shower has been on too long
|
// check if the shower has been on too long
|
||||||
@@ -74,7 +74,7 @@ void Shower::loop() {
|
|||||||
if ((timer_pause_ - timer_start_) > SHOWER_OFFSET_TIME) {
|
if ((timer_pause_ - timer_start_) > SHOWER_OFFSET_TIME) {
|
||||||
duration_ = (timer_pause_ - timer_start_ - SHOWER_OFFSET_TIME);
|
duration_ = (timer_pause_ - timer_start_ - SHOWER_OFFSET_TIME);
|
||||||
if (duration_ > SHOWER_MIN_DURATION) {
|
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_);
|
LOG_DEBUG(F("[Shower] finished with duration %d"), duration_);
|
||||||
publish_values();
|
publish_values();
|
||||||
}
|
}
|
||||||
@@ -129,7 +129,7 @@ void Shower::publish_values() {
|
|||||||
doc["duration"] = s;
|
doc["duration"] = s;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mqtt::publish("shower_data", doc);
|
Mqtt::publish(F("shower_data"), doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace emsesp
|
} // namespace emsesp
|
||||||
|
|||||||
@@ -237,7 +237,7 @@ void System::send_heartbeat() {
|
|||||||
doc["rxfails"] = EMSESP::rxservice_.telegram_error_count();
|
doc["rxfails"] = EMSESP::rxservice_.telegram_error_count();
|
||||||
doc["adc"] = analog_; //analogRead(A0);
|
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
|
// measure and moving average adc
|
||||||
|
|||||||
Reference in New Issue
Block a user