mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
fix Last Will (LWT) not set on MQTT Connect #2247
This commit is contained in:
20
src/mqtt.cpp
20
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user