diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 661bf72..c3bc235 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -492,6 +492,7 @@ void setup() { amqttClient.setServer(mqtt_host, atoi(mqtt_port)); if (mqtt_user != "" or mqtt_pass != "") amqttClient.setCredentials(mqtt_user, mqtt_pass); amqttClient.setClientId(mqtt_clientid); + amqttClient.setWill(mqtt_will_topic.c_str(), 2, true, mqtt_will_payload, 0); connectToMqtt(); } diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 52b8542..2ebb77f 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -62,6 +62,9 @@ uint32_t autoParams[][4] = { // color, speed, mode, duration (milliseconds) }; #if defined(ENABLE_MQTT) or defined(ENABLE_AMQTT) + const String mqtt_will_topic = String(HOSTNAME) + "/status"; + const char mqtt_will_payload[] = "ONLINE"; + #ifdef ENABLE_MQTT #define MQTT_MAX_PACKET_SIZE 512 #define MQTT_MAX_RECONNECT_TRIES 4 diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 4e3da66..d105d6e 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -996,7 +996,7 @@ void checkForRequests() { mqtt_reconnect_retries++; DBG_OUTPUT_PORT.printf("Attempting MQTT connection %d / %d ...\n", mqtt_reconnect_retries, MQTT_MAX_RECONNECT_TRIES); // Attempt to connect - if (mqtt_client.connect(mqtt_clientid, mqtt_user, mqtt_pass)) { + if (mqtt_client.connect(mqtt_clientid, mqtt_user, mqtt_pass, mqtt_will_topic.c_str(), 2, true, mqtt_will_payload, true)) { DBG_OUTPUT_PORT.println("MQTT connected!"); // Once connected, publish an announcement... char * message = new char[18 + strlen(HOSTNAME) + 1]; diff --git a/Arduino/McLighting/version_info.ino b/Arduino/McLighting/version_info.ino index 1e545f9..61fbb9b 100644 --- a/Arduino/McLighting/version_info.ino +++ b/Arduino/McLighting/version_info.ino @@ -73,4 +73,5 @@ * - Added alternative way to send large messages using PubSubClient * - Bump PIO core to 2.0.1 * - Send HA state on MQTT connect, address https://github.com/toblum/McLighting/issues/349 + * - Add LWT for MQTT and AMQTT, address https://github.com/toblum/McLighting/issues/340 */ diff --git a/platformio.ini b/platformio.ini index 3ae5eca..cd6307f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -45,6 +45,7 @@ lib_deps = WebSockets ESPAsyncE131 ESPAsyncUDP + PubSubClient [env:esp01_1m] board = esp01_1m