From acebc03d691b99e60e0873660566d3840fc68426 Mon Sep 17 00:00:00 2001 From: proddy Date: Mon, 25 Nov 2024 00:56:15 +0100 Subject: [PATCH] fix Last Will (LWT) not set on MQTT Connect #2247 --- CHANGELOG_LATEST.md | 1 + lib/framework/ESP8266React.h | 4 ---- lib/framework/MqttSettingsService.cpp | 11 +++++++++++ src/mqtt.cpp | 20 ++++++++++---------- src/version.h | 2 +- 5 files changed, 23 insertions(+), 15 deletions(-) diff --git a/CHANGELOG_LATEST.md b/CHANGELOG_LATEST.md index 899fb95c1..69fa7f9ee 100644 --- a/CHANGELOG_LATEST.md +++ b/CHANGELOG_LATEST.md @@ -27,6 +27,7 @@ For more details go to [docs.emsesp.org](https://docs.emsesp.org/). - analog dac output and inputs on dac pins [#2201](https://github.com/emsesp/EMS-ESP32/discussions/2201) - api memory leak [#2216](https://github.com/emsesp/EMS-ESP32/issues/2216) - modbus multiple mixers [#2229](https://github.com/emsesp/EMS-ESP32/issues/2229) +- Last Will (LWT) not set on MQTT Connect [#2247](https://github.com/emsesp/EMS-ESP32/issues/2247) ## Changed diff --git a/lib/framework/ESP8266React.h b/lib/framework/ESP8266React.h index fd61a389d..7cecdda17 100644 --- a/lib/framework/ESP8266React.h +++ b/lib/framework/ESP8266React.h @@ -57,10 +57,6 @@ class ESP8266React { // special functions needed outside scope // - void setWill(const char * will_topic) { - _mqttSettingsService.setWill(will_topic); - } - // true if AP is active bool apStatus() { return _apSettingsService.getAPNetworkStatus() == APNetworkStatus::ACTIVE; diff --git a/lib/framework/MqttSettingsService.cpp b/lib/framework/MqttSettingsService.cpp index 9274ae49b..e97c15691 100644 --- a/lib/framework/MqttSettingsService.cpp +++ b/lib/framework/MqttSettingsService.cpp @@ -207,6 +207,17 @@ bool MqttSettingsService::configureMqtt() { static_cast(_mqttClient)->setClientId(_state.clientId.c_str()); static_cast(_mqttClient)->setKeepAlive(_state.keepAlive); static_cast(_mqttClient)->setCleanSession(_state.cleanSession); + + // create last will topic with the base prefixed. It has to be static because the client destroys the reference + static char will_topic[FACTORY_MQTT_MAX_TOPIC_LENGTH]; + if (_state.base.isEmpty()) { + snprintf(will_topic, sizeof(will_topic), "status"); + } else { + snprintf(will_topic, sizeof(will_topic), "%s/status", _state.base.c_str()); + } + setWill(will_topic); + + return _mqttClient->connect(); } diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 5fef8e371..3e18c7d9e 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -386,16 +386,6 @@ void Mqtt::start() { // add the 'publish' command ('call system publish' in console or via API) Command::add(EMSdevice::DeviceType::SYSTEM, F_(publish), System::command_publish, FL_(publish_cmd)); - // create last will topic with the base prefixed. It has to be static because the client destroys the reference - static char will_topic[MQTT_TOPIC_MAX_SIZE]; - if (!Mqtt::base().empty()) { - snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "%s/status", Mqtt::base().c_str()); - } else { - snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "status"); - } - - EMSESP::esp8266React.setWill(will_topic); // with qos 1, retain true - #if defined(EMSESP_STANDALONE) Mqtt::on_connect(); // simulate an MQTT connection #endif @@ -524,6 +514,16 @@ void Mqtt::on_connect() { // re-subscribe to all custom registered MQTT topics resubscribe(); + // create last will topic with the base prefixed. It has to be static because the client destroys the reference + static char will_topic[MQTT_TOPIC_MAX_SIZE]; + if (!Mqtt::base().empty()) { + snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "%s/status", Mqtt::base().c_str()); + } else { + snprintf(will_topic, MQTT_TOPIC_MAX_SIZE, "status"); + } + // EMSESP::esp8266React.setWill(will_topic); // with qos 1, retain true + + // publish to the last will topic (see Mqtt::start() function) to say we're alive queue_publish_retain("status", "online", true); // retain: https://github.com/emsesp/EMS-ESP32/discussions/2086 } diff --git a/src/version.h b/src/version.h index b5fcbc1a4..881539f8f 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define EMSESP_APP_VERSION "3.7.1-dev.11" \ No newline at end of file +#define EMSESP_APP_VERSION "3.7.1-dev.12" \ No newline at end of file