Replaced hard coded lwt values by dynymic ones

This commit is contained in:
Thomas Basler 2022-04-21 23:43:44 +02:00
parent 3ca829de1a
commit 0d48d72691
4 changed files with 27 additions and 8 deletions

View File

@ -3,7 +3,7 @@
#include <Arduino.h>
#define CONFIG_FILENAME "/config.bin"
#define CONFIG_VERSION 0x00010600 // 0.1.6 // make sure to clean all after change
#define CONFIG_VERSION 0x00010700 // 0.1.7 // make sure to clean all after change
#define WIFI_MAX_SSID_STRLEN 31
#define WIFI_MAX_PASSWORD_STRLEN 31
@ -17,6 +17,7 @@
#define MQTT_MAX_USERNAME_STRLEN 32
#define MQTT_MAX_PASSWORD_STRLEN 32
#define MQTT_MAX_TOPIC_STRLEN 32
#define MQTT_MAX_LWTVALUE_STRLEN 20
struct CONFIG_T {
uint32_t Cfg_Version;
@ -43,6 +44,9 @@ struct CONFIG_T {
char Mqtt_Password[MQTT_MAX_PASSWORD_STRLEN + 1];
char Mqtt_Topic[MQTT_MAX_TOPIC_STRLEN + 1];
bool Mqtt_Retain;
char Mqtt_LwtTopic[MQTT_MAX_TOPIC_STRLEN + 1];
char Mqtt_LwtValue_Online[MQTT_MAX_LWTVALUE_STRLEN + 1];
char Mqtt_LwtValue_Offline[MQTT_MAX_LWTVALUE_STRLEN + 1];
};
class ConfigurationClass {

View File

@ -27,3 +27,7 @@
#define MQTT_USER ""
#define MQTT_PASSWORD ""
#define MQTT_TOPIC "solar/"
#define MQTT_RETAIN true
#define MQTT_LWT_TOPIC "status"
#define MQTT_LWT_ONLINE "online"
#define MQTT_LWT_OFFLINE "offline"

View File

@ -28,7 +28,10 @@ void ConfigurationClass::init()
strlcpy(config.Mqtt_Username, MQTT_USER, sizeof(config.Mqtt_Username));
strlcpy(config.Mqtt_Password, MQTT_PASSWORD, sizeof(config.Mqtt_Password));
strlcpy(config.Mqtt_Topic, MQTT_TOPIC, sizeof(config.Mqtt_Topic));
config.Mqtt_Retain = true;
config.Mqtt_Retain = MQTT_RETAIN;
strlcpy(config.Mqtt_LwtTopic, MQTT_LWT_TOPIC, sizeof(config.Mqtt_LwtTopic));
strlcpy(config.Mqtt_LwtValue_Online, MQTT_LWT_ONLINE, sizeof(config.Mqtt_LwtValue_Online));
strlcpy(config.Mqtt_LwtValue_Offline, MQTT_LWT_OFFLINE, sizeof(config.Mqtt_LwtValue_Offline));
}
bool ConfigurationClass::write()
@ -78,7 +81,13 @@ void ConfigurationClass::migrate()
}
if (config.Cfg_Version < 0x00010600) {
config.Mqtt_Retain = true;
config.Mqtt_Retain = MQTT_RETAIN;
}
if (config.Cfg_Version < 0x00010700) {
strlcpy(config.Mqtt_LwtTopic, MQTT_LWT_TOPIC, sizeof(config.Mqtt_LwtTopic));
strlcpy(config.Mqtt_LwtValue_Online, MQTT_LWT_ONLINE, sizeof(config.Mqtt_LwtValue_Online));
strlcpy(config.Mqtt_LwtValue_Offline, MQTT_LWT_OFFLINE, sizeof(config.Mqtt_LwtValue_Offline));
}
config.Cfg_Version = CONFIG_VERSION;

View File

@ -27,7 +27,8 @@ void MqttSettingsClass::WiFiEvent(WiFiEvent_t event)
void MqttSettingsClass::onMqttConnect(bool sessionPresent)
{
Serial.println(F("Connected to MQTT."));
mqttClient.publish(willTopic.c_str(), 2, Configuration.get().Mqtt_Retain, "online");
CONFIG_T& config = Configuration.get();
publish(config.Mqtt_LwtTopic, config.Mqtt_LwtValue_Online);
}
void MqttSettingsClass::onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
@ -46,8 +47,8 @@ void MqttSettingsClass::performConnect()
mqttClient.setServer(config.Mqtt_Hostname, config.Mqtt_Port);
mqttClient.setCredentials(config.Mqtt_Username, config.Mqtt_Password);
willTopic = String(config.Mqtt_Topic) + "status";
mqttClient.setWill(willTopic.c_str(), 2, Configuration.get().Mqtt_Retain, "offline");
willTopic = String(config.Mqtt_Topic) + config.Mqtt_LwtTopic;
mqttClient.setWill(willTopic.c_str(), 2, config.Mqtt_Retain, config.Mqtt_LwtValue_Offline);
clientId = WiFiSettings.getApName();
mqttClient.setClientId(clientId.c_str());
@ -58,7 +59,8 @@ void MqttSettingsClass::performConnect()
void MqttSettingsClass::performDisconnect()
{
mqttClient.publish(willTopic.c_str(), 2, Configuration.get().Mqtt_Retain, "offline");
CONFIG_T& config = Configuration.get();
publish(config.Mqtt_LwtTopic, config.Mqtt_LwtValue_Offline);
mqttClient.disconnect();
}