add lwt topic

This commit is contained in:
technyon
2023-03-27 18:46:07 +02:00
parent 44ad2dd320
commit ddf70f5d8a
10 changed files with 51 additions and 5 deletions

View File

@@ -1,6 +1,5 @@
#include "Network.h"
#include "PreferencesKeys.h"
#include "MqttTopics.h"
#include "networkDevices/W5500Device.h"
#include "networkDevices/WifiDevice.h"
#include "Logger.h"
@@ -369,20 +368,21 @@ bool Network::reconnect()
Log->println(F("Attempting MQTT connection"));
_connectReplyReceived = false;
if(strlen(_mqttUser) == 0)
{
Log->println(F("MQTT: Connecting without credentials"));
_device->mqttSetServer(_mqttBrokerAddr, port);
_device->mqttConnect();
}
else
{
Log->print(F("MQTT: Connecting with user: ")); Log->println(_mqttUser);
_device->mqttSetCredentials(_mqttUser, _mqttPass);
_device->mqttSetServer(_mqttBrokerAddr, port);
_device->mqttConnect();
}
_device->setWill(_mqttConnectionStateTopic, 1, true, _lastWillPayload);
_device->mqttSetServer(_mqttBrokerAddr, port);
_device->mqttConnect();
unsigned long timeout = millis() + 60000;
while(!_connectReplyReceived && millis() < timeout)
@@ -416,6 +416,9 @@ bool Network::reconnect()
_device->mqttPublish(it.first.c_str(), MQTT_QOS_LEVEL, true, it.second.c_str());
}
}
publishString(_maintenancePathPrefix, _mqttConnectionStateTopic, "online");
_mqttConnectionState = 2;
for(const auto& callback : _reconnectedCallbacks)
{