WLED/wled00/mqtt.cpp

172 wiersze
4.3 KiB
C++
Czysty Zwykły widok Historia

#include "wled.h"
/*
* MQTT communication protocol for home automation
*/
#ifdef WLED_ENABLE_MQTT
#define MQTT_KEEP_ALIVE_TIME 60 // contact the MQTT broker every 60 seconds
void parseMQTTBriPayload(char* payload)
{
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);}
else {
uint8_t in = strtoul(payload, NULL, 10);
if (in == 0 && bri > 0) briLast = bri;
bri = in;
2020-02-22 15:17:32 +00:00
colorUpdated(NOTIFIER_CALL_MODE_DIRECT_CHANGE);
}
}
void onMqttConnect(bool sessionPresent)
{
//(re)subscribe to required topics
char subuf[38];
2019-08-17 10:27:06 +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-10-24 22:14:58 +00:00
doPublishMqtt = true;
2020-09-19 23:18:31 +00:00
DEBUG_PRINTLN(F("MQTT ready"));
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
2020-09-19 23:18:31 +00:00
DEBUG_PRINT(F("MQTT msg: "));
DEBUG_PRINTLN(topic);
2020-08-19 22:13:06 +00:00
// paranoia check to avoid npe if no payload
if (payload==nullptr) {
2020-09-19 23:18:31 +00:00
DEBUG_PRINTLN(F("no payload -> leave"));
2020-08-19 22:13:06 +00:00
return;
}
DEBUG_PRINTLN(payload);
size_t topicPrefixLen = strlen(mqttDeviceTopic);
if (strncmp(topic, mqttDeviceTopic, topicPrefixLen) == 0) {
topic += topicPrefixLen;
} else {
topicPrefixLen = strlen(mqttGroupTopic);
if (strncmp(topic, mqttGroupTopic, topicPrefixLen) == 0) {
topic += topicPrefixLen;
} else {
// Topic not used here. Probably a usermod subscribed to this topic.
return;
}
}
//Prefix is stripped from the topic at this point
if (strcmp(topic, "/col") == 0)
{
colorFromDecOrHexString(col, (char*)payload);
2020-02-22 15:17:32 +00:00
colorUpdated(NOTIFIER_CALL_MODE_DIRECT_CHANGE);
} else if (strcmp(topic, "/api") == 0)
{
2020-11-12 08:13:08 +00:00
if (payload[0] == '{') { //JSON API
DynamicJsonDocument doc(JSON_BUFFER_SIZE);
deserializeJson(doc, payload);
deserializeState(doc.as<JsonObject>());
} else { //HTTP API
String apireq = "win&";
apireq += (char*)payload;
handleSet(nullptr, apireq);
}
} else if (strcmp(topic, "") == 0)
{
2020-11-15 11:41:51 +00:00
parseMQTTBriPayload(payload);
}
}
void publishMqtt()
{
2019-10-20 15:38:25 +00:00
doPublishMqtt = false;
2020-05-28 00:20:02 +00:00
if (!WLED_MQTT_CONNECTED) return;
2020-09-19 23:18:31 +00:00
DEBUG_PRINTLN(F("Publish MQTT"));
char s[10];
char subuf[38];
2019-08-17 10:27:06 +00:00
sprintf(s, "%ld", bri);
strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/g");
mqtt->publish(subuf, 0, true, s);
2019-11-18 11:29:36 +00:00
sprintf(s, "#%06X", (col[3] << 24) | (col[0] << 16) | (col[1] << 8) | (col[2]));
strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/c");
mqtt->publish(subuf, 0, true, s);
2019-11-10 21:13:07 +00:00
strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/status");
mqtt->publish(subuf, 0, true, "online");
2019-11-10 21:13:07 +00:00
2019-03-16 01:09:37 +00:00
char apires[1024];
XML_response(nullptr, apires);
strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/v");
2019-03-16 01:09:37 +00:00
mqtt->publish(subuf, 0, true, apires);
}
//HA autodiscovery was removed in favor of the native integration in HA v0.102.0
bool initMqtt()
{
lastMqttReconnectAttempt = millis();
if (!mqttEnabled || mqttServer[0] == 0 || !WLED_CONNECTED) return false;
2019-10-18 12:06:07 +00:00
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
2020-09-19 23:18:31 +00:00
DEBUG_PRINTLN(F("Reconnecting MQTT"));
IPAddress mqttIP;
if (mqttIP.fromString(mqttServer)) //see if server is IP or domain
{
mqtt->setServer(mqttIP, mqttPort);
} else {
mqtt->setServer(mqttServer, mqttPort);
}
2019-08-17 10:27:06 +00:00
mqtt->setClientId(mqttClientID);
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");
mqtt->setKeepAlive(MQTT_KEEP_ALIVE_TIME);
mqtt->connect();
return true;
}
#else
bool initMqtt(){return false;}
void publishMqtt(){}
#endif