Skip to content

Commit

Permalink
fix Last Will (LWT) not set on MQTT Connect #2247
Browse files Browse the repository at this point in the history
  • Loading branch information
proddy committed Nov 24, 2024
1 parent a7a3207 commit acebc03
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_LATEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 0 additions & 4 deletions lib/framework/ESP8266React.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions lib/framework/MqttSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ bool MqttSettingsService::configureMqtt() {
static_cast<espMqttClient *>(_mqttClient)->setClientId(_state.clientId.c_str());
static_cast<espMqttClient *>(_mqttClient)->setKeepAlive(_state.keepAlive);
static_cast<espMqttClient *>(_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();
}

Expand Down
20 changes: 10 additions & 10 deletions src/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define EMSESP_APP_VERSION "3.7.1-dev.11"
#define EMSESP_APP_VERSION "3.7.1-dev.12"

0 comments on commit acebc03

Please sign in to comment.