2020-03-25 09:00:55 +01:00
|
|
|
#include "wled.h"
|
2020-03-31 02:38:08 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MQTT communication protocol for home automation
|
|
|
|
*/
|
2018-09-28 23:53:51 +02:00
|
|
|
|
2019-12-11 00:59:15 +01:00
|
|
|
#ifdef WLED_ENABLE_MQTT
|
2020-02-22 17:20:34 +01:00
|
|
|
#define MQTT_KEEP_ALIVE_TIME 60 // contact the MQTT broker every 60 seconds
|
2019-12-11 00:59:15 +01:00
|
|
|
|
2018-09-30 20:24:57 +02:00
|
|
|
void parseMQTTBriPayload(char* payload)
|
|
|
|
{
|
2019-04-14 19:31:25 +02:00
|
|
|
if (strstr(payload, "ON") || strstr(payload, "on") || strstr(payload, "true")) {bri = briLast; colorUpdated(1);}
|
2019-03-27 21:31:59 +01:00
|
|
|
else if (strstr(payload, "T" ) || strstr(payload, "t" )) {toggleOnOff(); colorUpdated(1);}
|
2018-09-30 20:24:57 +02:00
|
|
|
else {
|
|
|
|
uint8_t in = strtoul(payload, NULL, 10);
|
|
|
|
if (in == 0 && bri > 0) briLast = bri;
|
|
|
|
bri = in;
|
2021-07-09 18:54:28 +02:00
|
|
|
colorUpdated(CALL_MODE_DIRECT_CHANGE);
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
2018-09-30 20:24:57 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2019-02-17 19:21:09 +01:00
|
|
|
void onMqttConnect(bool sessionPresent)
|
|
|
|
{
|
|
|
|
//(re)subscribe to required topics
|
|
|
|
char subuf[38];
|
2019-08-17 12:27:06 +02:00
|
|
|
|
2021-05-06 22:58:03 +02:00
|
|
|
if (mqttDeviceTopic[0] != 0) {
|
2021-09-11 01:17:42 +02:00
|
|
|
strlcpy(subuf, mqttDeviceTopic, 33);
|
2019-02-17 19:21:09 +01:00
|
|
|
mqtt->subscribe(subuf, 0);
|
2021-05-06 22:58:03 +02:00
|
|
|
strcat_P(subuf, PSTR("/col"));
|
2019-02-17 19:21:09 +01:00
|
|
|
mqtt->subscribe(subuf, 0);
|
2021-09-11 01:17:42 +02:00
|
|
|
strlcpy(subuf, mqttDeviceTopic, 33);
|
2021-05-06 22:58:03 +02:00
|
|
|
strcat_P(subuf, PSTR("/api"));
|
2019-02-17 19:21:09 +01:00
|
|
|
mqtt->subscribe(subuf, 0);
|
|
|
|
}
|
|
|
|
|
2021-05-06 22:58:03 +02:00
|
|
|
if (mqttGroupTopic[0] != 0) {
|
2021-09-11 01:17:42 +02:00
|
|
|
strlcpy(subuf, mqttGroupTopic, 33);
|
2019-02-17 19:21:09 +01:00
|
|
|
mqtt->subscribe(subuf, 0);
|
2021-05-06 22:58:03 +02:00
|
|
|
strcat_P(subuf, PSTR("/col"));
|
2019-02-17 19:21:09 +01:00
|
|
|
mqtt->subscribe(subuf, 0);
|
2021-09-11 01:17:42 +02:00
|
|
|
strlcpy(subuf, mqttGroupTopic, 33);
|
2021-05-06 22:58:03 +02:00
|
|
|
strcat_P(subuf, PSTR("/api"));
|
2019-02-17 19:21:09 +01:00
|
|
|
mqtt->subscribe(subuf, 0);
|
|
|
|
}
|
2019-03-18 17:23:39 +01:00
|
|
|
|
2021-05-06 22:58:03 +02:00
|
|
|
usermods.onMqttConnect(sessionPresent);
|
|
|
|
|
2019-10-25 00:14:58 +02:00
|
|
|
doPublishMqtt = true;
|
2020-09-20 01:18:31 +02:00
|
|
|
DEBUG_PRINTLN(F("MQTT ready"));
|
2019-02-17 19:21:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
2018-09-30 20:24:57 +02:00
|
|
|
|
2020-09-20 01:18:31 +02:00
|
|
|
DEBUG_PRINT(F("MQTT msg: "));
|
2018-09-30 20:24:57 +02:00
|
|
|
DEBUG_PRINTLN(topic);
|
2020-08-20 00:13:06 +02:00
|
|
|
|
|
|
|
// paranoia check to avoid npe if no payload
|
|
|
|
if (payload==nullptr) {
|
2020-09-20 01:18:31 +02:00
|
|
|
DEBUG_PRINTLN(F("no payload -> leave"));
|
2020-08-20 00:13:06 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-05-17 10:38:07 +02:00
|
|
|
//make a copy of the payload to 0-terminate it
|
|
|
|
char* payloadStr = new char[len+1];
|
2021-05-18 15:45:34 +02:00
|
|
|
if (payloadStr == nullptr) return; //no mem
|
2021-05-17 10:38:07 +02:00
|
|
|
strncpy(payloadStr, payload, len);
|
|
|
|
payloadStr[len] = '\0';
|
2021-05-13 01:04:33 +02:00
|
|
|
DEBUG_PRINTLN(payloadStr);
|
2021-05-11 16:20:43 +02:00
|
|
|
|
2020-11-01 02:23:37 +01:00
|
|
|
size_t topicPrefixLen = strlen(mqttDeviceTopic);
|
|
|
|
if (strncmp(topic, mqttDeviceTopic, topicPrefixLen) == 0) {
|
2021-05-06 22:58:03 +02:00
|
|
|
topic += topicPrefixLen;
|
2020-11-01 02:23:37 +01:00
|
|
|
} else {
|
2021-05-06 22:58:03 +02:00
|
|
|
topicPrefixLen = strlen(mqttGroupTopic);
|
|
|
|
if (strncmp(topic, mqttGroupTopic, topicPrefixLen) == 0) {
|
|
|
|
topic += topicPrefixLen;
|
|
|
|
} else {
|
|
|
|
// Non-Wled Topic used here. Probably a usermod subscribed to this topic.
|
2021-05-13 01:04:33 +02:00
|
|
|
usermods.onMqttMessage(topic, payloadStr);
|
2021-05-17 10:38:07 +02:00
|
|
|
delete[] payloadStr;
|
2021-05-06 22:58:03 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-11-01 02:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Prefix is stripped from the topic at this point
|
2018-09-30 20:24:57 +02:00
|
|
|
|
2021-05-06 22:58:03 +02:00
|
|
|
if (strcmp_P(topic, PSTR("/col")) == 0) {
|
2021-05-13 01:04:33 +02:00
|
|
|
colorFromDecOrHexString(col, (char*)payloadStr);
|
2021-07-09 18:54:28 +02:00
|
|
|
colorUpdated(CALL_MODE_DIRECT_CHANGE);
|
2021-05-06 22:58:03 +02:00
|
|
|
} else if (strcmp_P(topic, PSTR("/api")) == 0) {
|
2021-05-14 20:57:56 +02:00
|
|
|
if (payload[0] == '{') { //JSON API
|
2021-11-03 14:52:22 +01:00
|
|
|
//DynamicJsonDocument doc(JSON_BUFFER_SIZE);
|
|
|
|
while (jsonBufferLock) delay(1);
|
|
|
|
jsonBufferLock = true;
|
|
|
|
doc.clear();
|
2021-05-13 01:04:33 +02:00
|
|
|
deserializeJson(doc, payloadStr);
|
2021-07-09 18:42:52 +02:00
|
|
|
fileDoc = &doc;
|
2020-11-12 09:13:08 +01:00
|
|
|
deserializeState(doc.as<JsonObject>());
|
2021-07-09 18:42:52 +02:00
|
|
|
fileDoc = nullptr;
|
2021-11-03 14:52:22 +01:00
|
|
|
jsonBufferLock = false;
|
2020-11-12 09:13:08 +01:00
|
|
|
} else { //HTTP API
|
|
|
|
String apireq = "win&";
|
2021-05-13 01:04:33 +02:00
|
|
|
apireq += (char*)payloadStr;
|
2020-11-12 09:13:08 +01:00
|
|
|
handleSet(nullptr, apireq);
|
|
|
|
}
|
2021-05-06 22:58:03 +02:00
|
|
|
} else if (strlen(topic) != 0) {
|
|
|
|
// non standard topic, check with usermods
|
2021-05-13 01:04:33 +02:00
|
|
|
usermods.onMqttMessage(topic, payloadStr);
|
2021-05-06 22:58:03 +02:00
|
|
|
} else {
|
|
|
|
// topmost topic (just wled/MAC)
|
2021-05-13 01:04:33 +02:00
|
|
|
parseMQTTBriPayload(payloadStr);
|
2020-11-01 02:23:37 +01:00
|
|
|
}
|
2021-05-17 10:38:07 +02:00
|
|
|
delete[] payloadStr;
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2019-02-17 19:21:09 +01:00
|
|
|
void publishMqtt()
|
2018-09-28 23:53:51 +02:00
|
|
|
{
|
2019-10-20 17:38:25 +02:00
|
|
|
doPublishMqtt = false;
|
2020-05-28 02:20:02 +02:00
|
|
|
if (!WLED_MQTT_CONNECTED) return;
|
2020-09-20 01:18:31 +02:00
|
|
|
DEBUG_PRINTLN(F("Publish MQTT"));
|
2018-09-28 23:53:51 +02:00
|
|
|
|
2018-10-04 16:50:12 +02:00
|
|
|
char s[10];
|
|
|
|
char subuf[38];
|
2019-08-17 12:27:06 +02:00
|
|
|
|
2021-05-06 22:58:03 +02:00
|
|
|
sprintf_P(s, PSTR("%u"), bri);
|
2021-09-11 01:17:42 +02:00
|
|
|
strlcpy(subuf, mqttDeviceTopic, 33);
|
2021-05-06 22:58:03 +02:00
|
|
|
strcat_P(subuf, PSTR("/g"));
|
2021-05-19 08:22:41 +02:00
|
|
|
mqtt->publish(subuf, 0, true, s); // retain message
|
2018-10-04 16:50:12 +02:00
|
|
|
|
2021-05-06 22:58:03 +02:00
|
|
|
sprintf_P(s, PSTR("#%06X"), (col[3] << 24) | (col[0] << 16) | (col[1] << 8) | (col[2]));
|
2021-09-11 01:17:42 +02:00
|
|
|
strlcpy(subuf, mqttDeviceTopic, 33);
|
2021-05-06 22:58:03 +02:00
|
|
|
strcat_P(subuf, PSTR("/c"));
|
2021-05-19 08:22:41 +02:00
|
|
|
mqtt->publish(subuf, 0, true, s); // retain message
|
2018-10-04 16:50:12 +02:00
|
|
|
|
2021-09-11 01:17:42 +02:00
|
|
|
strlcpy(subuf, mqttDeviceTopic, 33);
|
2021-05-06 22:58:03 +02:00
|
|
|
strcat_P(subuf, PSTR("/status"));
|
2021-05-19 08:22:41 +02:00
|
|
|
mqtt->publish(subuf, 0, true, "online"); // retain message for a LWT
|
2019-11-10 22:13:07 +01:00
|
|
|
|
2021-05-19 08:22:41 +02:00
|
|
|
char apires[1024]; // allocating 1024 bytes from stack can be risky
|
2019-12-04 02:01:47 +01:00
|
|
|
XML_response(nullptr, apires);
|
2021-09-11 01:17:42 +02:00
|
|
|
strlcpy(subuf, mqttDeviceTopic, 33);
|
2021-05-06 22:58:03 +02:00
|
|
|
strcat_P(subuf, PSTR("/v"));
|
2021-05-19 08:22:41 +02:00
|
|
|
mqtt->publish(subuf, 0, false, apires); // do not retain message
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
|
|
|
|
2019-03-24 00:49:26 +01:00
|
|
|
|
2019-12-04 02:01:47 +01:00
|
|
|
//HA autodiscovery was removed in favor of the native integration in HA v0.102.0
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2019-02-17 19:21:09 +01:00
|
|
|
bool initMqtt()
|
2018-09-28 23:53:51 +02:00
|
|
|
{
|
2019-12-13 01:23:07 +01:00
|
|
|
if (!mqttEnabled || mqttServer[0] == 0 || !WLED_CONNECTED) return false;
|
2019-10-18 14:06:07 +02:00
|
|
|
|
2019-10-20 12:48:29 +02:00
|
|
|
if (mqtt == nullptr) {
|
|
|
|
mqtt = new AsyncMqttClient();
|
|
|
|
mqtt->onMessage(onMqttMessage);
|
|
|
|
mqtt->onConnect(onMqttConnect);
|
|
|
|
}
|
2019-05-21 18:50:56 +02:00
|
|
|
if (mqtt->connected()) return true;
|
2019-08-17 12:27:06 +02:00
|
|
|
|
2020-09-20 01:18:31 +02:00
|
|
|
DEBUG_PRINTLN(F("Reconnecting MQTT"));
|
2018-09-28 23:53:51 +02:00
|
|
|
IPAddress mqttIP;
|
|
|
|
if (mqttIP.fromString(mqttServer)) //see if server is IP or domain
|
|
|
|
{
|
2019-08-18 18:14:17 +02:00
|
|
|
mqtt->setServer(mqttIP, mqttPort);
|
2018-09-28 23:53:51 +02:00
|
|
|
} else {
|
2019-08-18 18:14:17 +02:00
|
|
|
mqtt->setServer(mqttServer, mqttPort);
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
2019-08-17 12:27:06 +02:00
|
|
|
mqtt->setClientId(mqttClientID);
|
2019-10-18 23:47:11 +02:00
|
|
|
if (mqttUser[0] && mqttPass[0]) mqtt->setCredentials(mqttUser, mqttPass);
|
2019-11-10 22:13:07 +01:00
|
|
|
|
2021-09-11 01:17:42 +02:00
|
|
|
strlcpy(mqttStatusTopic, mqttDeviceTopic, 33);
|
2021-05-06 22:58:03 +02:00
|
|
|
strcat_P(mqttStatusTopic, PSTR("/status"));
|
2021-05-19 08:22:41 +02:00
|
|
|
mqtt->setWill(mqttStatusTopic, 0, true, "offline"); // LWT message
|
2020-02-22 17:20:34 +01:00
|
|
|
mqtt->setKeepAlive(MQTT_KEEP_ALIVE_TIME);
|
2019-02-17 19:21:09 +01:00
|
|
|
mqtt->connect();
|
2018-09-28 23:53:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-11 00:59:15 +01:00
|
|
|
|
|
|
|
#else
|
|
|
|
bool initMqtt(){return false;}
|
|
|
|
void publishMqtt(){}
|
|
|
|
#endif
|