2018-09-28 21:53:51 +00:00
|
|
|
/*
|
|
|
|
* MQTT communication protocol for home automation
|
|
|
|
*/
|
|
|
|
|
2019-12-10 23:59:15 +00:00
|
|
|
#ifdef WLED_ENABLE_MQTT
|
|
|
|
|
2018-09-30 18:24:57 +00:00
|
|
|
void parseMQTTBriPayload(char* payload)
|
|
|
|
{
|
2019-04-14 17:31:25 +00:00
|
|
|
if (strstr(payload, "ON") || strstr(payload, "on") || strstr(payload, "true")) {bri = briLast; colorUpdated(1);}
|
2019-03-27 20:31:59 +00:00
|
|
|
else if (strstr(payload, "T" ) || strstr(payload, "t" )) {toggleOnOff(); colorUpdated(1);}
|
2018-09-30 18:24:57 +00:00
|
|
|
else {
|
|
|
|
uint8_t in = strtoul(payload, NULL, 10);
|
|
|
|
if (in == 0 && bri > 0) briLast = bri;
|
|
|
|
bri = in;
|
2018-09-28 21:53:51 +00:00
|
|
|
colorUpdated(1);
|
|
|
|
}
|
2018-09-30 18:24:57 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 16:00:36 +00:00
|
|
|
|
2019-02-17 18:21:09 +00:00
|
|
|
void onMqttConnect(bool sessionPresent)
|
|
|
|
{
|
|
|
|
//(re)subscribe to required topics
|
|
|
|
char subuf[38];
|
2019-08-17 10:27:06 +00:00
|
|
|
|
2019-02-17 18:21:09 +00:00
|
|
|
if (mqttDeviceTopic[0] != 0)
|
|
|
|
{
|
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
mqtt->subscribe(subuf, 0);
|
|
|
|
strcat(subuf, "/col");
|
|
|
|
mqtt->subscribe(subuf, 0);
|
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
strcat(subuf, "/api");
|
|
|
|
mqtt->subscribe(subuf, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mqttGroupTopic[0] != 0)
|
|
|
|
{
|
|
|
|
strcpy(subuf, mqttGroupTopic);
|
|
|
|
mqtt->subscribe(subuf, 0);
|
|
|
|
strcat(subuf, "/col");
|
|
|
|
mqtt->subscribe(subuf, 0);
|
|
|
|
strcpy(subuf, mqttGroupTopic);
|
|
|
|
strcat(subuf, "/api");
|
|
|
|
mqtt->subscribe(subuf, 0);
|
|
|
|
}
|
2019-03-18 16:23:39 +00:00
|
|
|
|
2019-10-24 22:14:58 +00:00
|
|
|
doPublishMqtt = true;
|
2019-10-18 21:47:11 +00:00
|
|
|
DEBUG_PRINTLN("MQTT ready");
|
2019-02-17 18:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
2018-09-30 18:24:57 +00:00
|
|
|
|
2019-10-20 15:38:25 +00:00
|
|
|
DEBUG_PRINT("MQTT msg: ");
|
2018-09-30 18:24:57 +00:00
|
|
|
DEBUG_PRINTLN(topic);
|
2019-02-17 18:21:09 +00:00
|
|
|
DEBUG_PRINTLN(payload);
|
2018-09-30 18:24:57 +00:00
|
|
|
|
|
|
|
//no need to check the topic because we only get topics we are subscribed to
|
|
|
|
|
|
|
|
if (strstr(topic, "/col"))
|
2018-09-28 21:53:51 +00:00
|
|
|
{
|
2019-02-05 20:53:39 +00:00
|
|
|
colorFromDecOrHexString(col, (char*)payload);
|
2018-09-28 21:53:51 +00:00
|
|
|
colorUpdated(1);
|
2018-09-30 18:24:57 +00:00
|
|
|
} else if (strstr(topic, "/api"))
|
2018-09-28 21:53:51 +00:00
|
|
|
{
|
2018-10-04 14:50:12 +00:00
|
|
|
String apireq = "win&";
|
2018-10-04 16:17:01 +00:00
|
|
|
apireq += (char*)payload;
|
2019-02-15 23:21:22 +00:00
|
|
|
handleSet(nullptr, apireq);
|
2019-02-17 18:21:09 +00:00
|
|
|
} else parseMQTTBriPayload(payload);
|
2018-09-28 21:53:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 16:00:36 +00:00
|
|
|
|
2019-02-17 18:21:09 +00:00
|
|
|
void publishMqtt()
|
2018-09-28 21:53:51 +00:00
|
|
|
{
|
2019-10-20 15:38:25 +00:00
|
|
|
doPublishMqtt = false;
|
2019-10-18 21:47:11 +00:00
|
|
|
if (mqtt == nullptr || !mqtt->connected()) return;
|
2018-09-30 18:24:57 +00:00
|
|
|
DEBUG_PRINTLN("Publish MQTT");
|
2018-09-28 21:53:51 +00:00
|
|
|
|
2018-10-04 14:50:12 +00:00
|
|
|
char s[10];
|
|
|
|
char subuf[38];
|
2019-08-17 10:27:06 +00:00
|
|
|
|
2018-10-04 14:50:12 +00:00
|
|
|
sprintf(s, "%ld", bri);
|
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
strcat(subuf, "/g");
|
2019-02-17 18:21:09 +00:00
|
|
|
mqtt->publish(subuf, 0, true, s);
|
2018-10-04 14:50:12 +00:00
|
|
|
|
2019-11-18 11:29:36 +00:00
|
|
|
sprintf(s, "#%06X", (col[3] << 24) | (col[0] << 16) | (col[1] << 8) | (col[2]));
|
2018-10-04 14:50:12 +00:00
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
strcat(subuf, "/c");
|
2019-02-17 18:21:09 +00:00
|
|
|
mqtt->publish(subuf, 0, true, s);
|
2018-10-04 14:50:12 +00:00
|
|
|
|
2019-11-10 21:13:07 +00:00
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
strcat(subuf, "/status");
|
|
|
|
mqtt->publish(subuf, 0, true, "online");
|
|
|
|
|
2019-03-16 01:09:37 +00:00
|
|
|
char apires[1024];
|
2019-12-04 01:01:47 +00:00
|
|
|
XML_response(nullptr, apires);
|
2018-10-04 14:50:12 +00:00
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
strcat(subuf, "/v");
|
2019-03-16 01:09:37 +00:00
|
|
|
mqtt->publish(subuf, 0, true, apires);
|
2018-09-28 21:53:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 23:49:26 +00:00
|
|
|
|
2019-12-04 01:01:47 +00:00
|
|
|
//HA autodiscovery was removed in favor of the native integration in HA v0.102.0
|
2018-11-09 16:00:36 +00:00
|
|
|
|
2019-02-17 18:21:09 +00:00
|
|
|
bool initMqtt()
|
2018-09-28 21:53:51 +00:00
|
|
|
{
|
2019-10-18 21:47:11 +00:00
|
|
|
lastMqttReconnectAttempt = millis();
|
2019-10-18 12:06:07 +00:00
|
|
|
if (mqttServer[0] == 0 || !WLED_CONNECTED) return false;
|
|
|
|
|
2019-10-20 10:48:29 +00:00
|
|
|
if (mqtt == nullptr) {
|
|
|
|
mqtt = new AsyncMqttClient();
|
|
|
|
mqtt->onMessage(onMqttMessage);
|
|
|
|
mqtt->onConnect(onMqttConnect);
|
|
|
|
}
|
2019-05-21 16:50:56 +00:00
|
|
|
if (mqtt->connected()) return true;
|
2019-08-17 10:27:06 +00:00
|
|
|
|
2019-10-18 21:47:11 +00:00
|
|
|
DEBUG_PRINTLN("Reconnecting MQTT");
|
2018-09-28 21:53:51 +00:00
|
|
|
IPAddress mqttIP;
|
|
|
|
if (mqttIP.fromString(mqttServer)) //see if server is IP or domain
|
|
|
|
{
|
2019-08-18 16:14:17 +00:00
|
|
|
mqtt->setServer(mqttIP, mqttPort);
|
2018-09-28 21:53:51 +00:00
|
|
|
} else {
|
2019-08-18 16:14:17 +00:00
|
|
|
mqtt->setServer(mqttServer, mqttPort);
|
2018-09-28 21:53:51 +00:00
|
|
|
}
|
2019-08-17 10:27:06 +00:00
|
|
|
mqtt->setClientId(mqttClientID);
|
2019-10-18 21:47:11 +00:00
|
|
|
if (mqttUser[0] && mqttPass[0]) mqtt->setCredentials(mqttUser, mqttPass);
|
2019-11-10 21:13:07 +00:00
|
|
|
|
|
|
|
strcpy(mqttStatusTopic, mqttDeviceTopic);
|
|
|
|
strcat(mqttStatusTopic, "/status");
|
|
|
|
mqtt->setWill(mqttStatusTopic, 0, true, "offline");
|
2019-02-17 18:21:09 +00:00
|
|
|
mqtt->connect();
|
2018-09-28 21:53:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-10 23:59:15 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
bool initMqtt(){return false;}
|
|
|
|
void publishMqtt(){}
|
|
|
|
#endif
|