Change to set LWT only once

This commit is contained in:
Christian W. Zuckschwerdt 2025-05-22 09:36:41 +02:00
parent 25223c446f
commit 817157bbc1

View File

@ -27,7 +27,7 @@ static void parseMQTTBriPayload(char* payload)
static void onMqttConnect(bool sessionPresent) static void onMqttConnect(bool sessionPresent)
{ {
//(re)subscribe to required topics //(re)subscribe to required topics
char subuf[MQTT_MAX_TOPIC_LEN + 6]; char subuf[MQTT_MAX_TOPIC_LEN + 9];
if (mqttDeviceTopic[0] != 0) { if (mqttDeviceTopic[0] != 0) {
strlcpy(subuf, mqttDeviceTopic, MQTT_MAX_TOPIC_LEN + 1); strlcpy(subuf, mqttDeviceTopic, MQTT_MAX_TOPIC_LEN + 1);
@ -52,6 +52,13 @@ static void onMqttConnect(bool sessionPresent)
UsermodManager::onMqttConnect(sessionPresent); UsermodManager::onMqttConnect(sessionPresent);
DEBUG_PRINTLN(F("MQTT ready")); DEBUG_PRINTLN(F("MQTT ready"));
#ifndef USERMOD_SMARTNEST
strlcpy(subuf, mqttDeviceTopic, MQTT_MAX_TOPIC_LEN + 1);
strcat_P(subuf, PSTR("/status"));
mqtt->publish(subuf, 0, true, "online"); // retain message for a LWT
#endif
publishMqtt(); publishMqtt();
} }
@ -174,10 +181,6 @@ void publishMqtt()
strcat_P(subuf, PSTR("/c")); strcat_P(subuf, PSTR("/c"));
mqtt->publish(subuf, 0, retainMqttMsg, s); // optionally retain message (#2263) mqtt->publish(subuf, 0, retainMqttMsg, s); // optionally retain message (#2263)
strlcpy(subuf, mqttDeviceTopic, MQTT_MAX_TOPIC_LEN + 1);
strcat_P(subuf, PSTR("/status"));
mqtt->publish(subuf, 0, true, "online"); // retain message for a LWT
// TODO: use a DynamicBufferList. Requires a list-read-capable MQTT client API. // TODO: use a DynamicBufferList. Requires a list-read-capable MQTT client API.
DynamicBuffer buf(1024); DynamicBuffer buf(1024);
bufferPrint pbuf(buf.data(), buf.size()); bufferPrint pbuf(buf.data(), buf.size());