Parcourir la source

Add support for MQTT LWT messages

Christopher Mullins il y a 6 ans
Parent
commit
5edf424b85
5 fichiers modifiés avec 39 ajouts et 3 suppressions
  1. 2 2
      dist/index.html.gz.h
  2. 19 1
      lib/MQTT/MqttClient.cpp
  3. 4 0
      lib/Settings/Settings.cpp
  4. 2 0
      lib/Settings/Settings.h
  5. 12 0
      web/src/js/script.js

Fichier diff supprimé car celui-ci est trop grand
+ 2 - 2
dist/index.html.gz.h


+ 19 - 1
lib/MQTT/MqttClient.cpp

@@ -51,12 +51,30 @@ bool MqttClient::connect() {
     Serial.println(F("MqttClient - connecting"));
 #endif
 
-  if (settings.mqttUsername.length() > 0) {
+  if (settings.mqttUsername.length() > 0 && settings.mqttLwtTopic.length() > 0) {
+    return mqttClient->connect(
+      nameBuffer,
+      settings.mqttUsername.c_str(),
+      settings.mqttPassword.c_str(),
+      settings.mqttLwtTopic.c_str(),
+      2,
+      true,
+      settings.mqttLwtMessage.c_str()
+    );
+  } else if (settings.mqttUsername.length() > 0) {
     return mqttClient->connect(
       nameBuffer,
       settings.mqttUsername.c_str(),
       settings.mqttPassword.c_str()
     );
+  } else if (settings.mqttLwtTopic.length() > 0) {
+    return mqttClient->connect(
+      nameBuffer,
+      settings.mqttLwtTopic.c_str(),
+      2,
+      true,
+      settings.mqttLwtMessage.c_str()
+    );
   } else {
     return mqttClient->connect(nameBuffer);
   }

+ 4 - 0
lib/Settings/Settings.cpp

@@ -97,6 +97,8 @@ void Settings::patch(JsonObject& parsedSettings) {
     this->setIfPresent(parsedSettings, "mqtt_topic_pattern", mqttTopicPattern);
     this->setIfPresent(parsedSettings, "mqtt_update_topic_pattern", mqttUpdateTopicPattern);
     this->setIfPresent(parsedSettings, "mqtt_state_topic_pattern", mqttStateTopicPattern);
+    this->setIfPresent(parsedSettings, "mqtt_lwt_topic", mqttLwtTopic);
+    this->setIfPresent(parsedSettings, "mqtt_lwt_message", mqttLwtMessage);
     this->setIfPresent(parsedSettings, "discovery_port", discoveryPort);
     this->setIfPresent(parsedSettings, "listen_repeats", listenRepeats);
     this->setIfPresent(parsedSettings, "state_flush_interval", stateFlushInterval);
@@ -193,6 +195,8 @@ void Settings::serialize(Stream& stream, const bool prettyPrint) {
   root["mqtt_topic_pattern"] = this->mqttTopicPattern;
   root["mqtt_update_topic_pattern"] = this->mqttUpdateTopicPattern;
   root["mqtt_state_topic_pattern"] = this->mqttStateTopicPattern;
+  root["mqtt_lwt_topic"] = this->mqttLwtTopic;
+  root["mqtt_lwt_message"] = this->mqttLwtMessage;
   root["discovery_port"] = this->discoveryPort;
   root["listen_repeats"] = this->listenRepeats;
   root["state_flush_interval"] = this->stateFlushInterval;

+ 2 - 0
lib/Settings/Settings.h

@@ -153,6 +153,8 @@ public:
   String mqttTopicPattern;
   String mqttUpdateTopicPattern;
   String mqttStateTopicPattern;
+  String mqttLwtTopic;
+  String mqttLwtMessage;
   GroupStateField *groupStateFields;
   size_t numGroupStateFields;
   uint16_t discoveryPort;

+ 12 - 0
web/src/js/script.js

@@ -127,6 +127,18 @@ var UI_FIELDS = [ {
     type: "string",
     tab: "tab-mqtt"
   }, {
+    tag:   "mqtt_lwt_topic", 
+    friendly: "MQTT LWT Topic",
+    help: "Topic to use for LWT message (leave blank to disable LWT)",
+    type: "string",
+    tab: "tab-mqtt"
+  }, {
+    tag:   "mqtt_lwt_message", 
+    friendly: "MQTT LWT Message",
+    help: "LWT Message - sent when client disconnects uncleanly",
+    type: "string",
+    tab: "tab-mqtt"
+  }, {
     tag:   "radio_interface_type", 
     friendly: "Radio interface type",
     help: "2.4 GHz radio model. Only change this if you know you're not using an NRF24L01!",