Add LWT for MQTT and AMQTT

- Add LWT for MQTT and AMQTT, address https://github.com/toblum/McLighting/issues/340
This commit is contained in:
Debashish Sahu 2019-03-04 13:48:26 -05:00
parent c2b3b6d0fa
commit 38d8f3f813
5 changed files with 7 additions and 1 deletions

View file

@ -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();
}

View file

@ -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

View file

@ -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];

View file

@ -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
*/

View file

@ -45,6 +45,7 @@ lib_deps =
WebSockets
ESPAsyncE131
ESPAsyncUDP
PubSubClient
[env:esp01_1m]
board = esp01_1m