fix Last Will (LWT) not set on MQTT Connect #2247

This commit is contained in:
proddy
2024-11-25 00:56:15 +01:00
parent a7a3207cca
commit acebc03d69
5 changed files with 23 additions and 15 deletions

View File

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