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:
parent
c2b3b6d0fa
commit
38d8f3f813
5 changed files with 7 additions and 1 deletions
|
@ -492,6 +492,7 @@ void setup() {
|
||||||
amqttClient.setServer(mqtt_host, atoi(mqtt_port));
|
amqttClient.setServer(mqtt_host, atoi(mqtt_port));
|
||||||
if (mqtt_user != "" or mqtt_pass != "") amqttClient.setCredentials(mqtt_user, mqtt_pass);
|
if (mqtt_user != "" or mqtt_pass != "") amqttClient.setCredentials(mqtt_user, mqtt_pass);
|
||||||
amqttClient.setClientId(mqtt_clientid);
|
amqttClient.setClientId(mqtt_clientid);
|
||||||
|
amqttClient.setWill(mqtt_will_topic.c_str(), 2, true, mqtt_will_payload, 0);
|
||||||
|
|
||||||
connectToMqtt();
|
connectToMqtt();
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,6 +62,9 @@ uint32_t autoParams[][4] = { // color, speed, mode, duration (milliseconds)
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined(ENABLE_MQTT) or defined(ENABLE_AMQTT)
|
#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
|
#ifdef ENABLE_MQTT
|
||||||
#define MQTT_MAX_PACKET_SIZE 512
|
#define MQTT_MAX_PACKET_SIZE 512
|
||||||
#define MQTT_MAX_RECONNECT_TRIES 4
|
#define MQTT_MAX_RECONNECT_TRIES 4
|
||||||
|
|
|
@ -996,7 +996,7 @@ void checkForRequests() {
|
||||||
mqtt_reconnect_retries++;
|
mqtt_reconnect_retries++;
|
||||||
DBG_OUTPUT_PORT.printf("Attempting MQTT connection %d / %d ...\n", mqtt_reconnect_retries, MQTT_MAX_RECONNECT_TRIES);
|
DBG_OUTPUT_PORT.printf("Attempting MQTT connection %d / %d ...\n", mqtt_reconnect_retries, MQTT_MAX_RECONNECT_TRIES);
|
||||||
// Attempt to connect
|
// 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!");
|
DBG_OUTPUT_PORT.println("MQTT connected!");
|
||||||
// Once connected, publish an announcement...
|
// Once connected, publish an announcement...
|
||||||
char * message = new char[18 + strlen(HOSTNAME) + 1];
|
char * message = new char[18 + strlen(HOSTNAME) + 1];
|
||||||
|
|
|
@ -73,4 +73,5 @@
|
||||||
* - Added alternative way to send large messages using PubSubClient
|
* - Added alternative way to send large messages using PubSubClient
|
||||||
* - Bump PIO core to 2.0.1
|
* - Bump PIO core to 2.0.1
|
||||||
* - Send HA state on MQTT connect, address https://github.com/toblum/McLighting/issues/349
|
* - 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
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -45,6 +45,7 @@ lib_deps =
|
||||||
WebSockets
|
WebSockets
|
||||||
ESPAsyncE131
|
ESPAsyncE131
|
||||||
ESPAsyncUDP
|
ESPAsyncUDP
|
||||||
|
PubSubClient
|
||||||
|
|
||||||
[env:esp01_1m]
|
[env:esp01_1m]
|
||||||
board = esp01_1m
|
board = esp01_1m
|
||||||
|
|
Loading…
Reference in a new issue