WLED/wled00/wled17_mqtt.ino

133 lines
3.2 KiB
Arduino
Raw Normal View History

/*
* MQTT communication protocol for home automation
*/
void parseMQTTBriPayload(char* payload)
{
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);}
else {
uint8_t in = strtoul(payload, NULL, 10);
if (in == 0 && bri > 0) briLast = bri;
bri = in;
colorUpdated(1);
}
}
void onMqttConnect(bool sessionPresent)
{
//(re)subscribe to required topics
char subuf[38];
2019-08-17 12:27:06 +02: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-25 00:14:58 +02:00
doPublishMqtt = true;
DEBUG_PRINTLN("MQTT ready");
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
2019-10-20 17:38:25 +02:00
DEBUG_PRINT("MQTT msg: ");
DEBUG_PRINTLN(topic);
DEBUG_PRINTLN(payload);
//no need to check the topic because we only get topics we are subscribed to
if (strstr(topic, "/col"))
{
colorFromDecOrHexString(col, (char*)payload);
colorUpdated(1);
} else if (strstr(topic, "/api"))
{
String apireq = "win&";
2018-10-04 18:17:01 +02:00
apireq += (char*)payload;
2019-02-16 00:21:22 +01:00
handleSet(nullptr, apireq);
} else parseMQTTBriPayload(payload);
}
void publishMqtt()
{
2019-10-20 17:38:25 +02:00
doPublishMqtt = false;
if (mqtt == nullptr || !mqtt->connected()) return;
DEBUG_PRINTLN("Publish MQTT");
char s[10];
char subuf[38];
2019-08-17 12:27:06 +02:00
sprintf(s, "%ld", bri);
strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/g");
mqtt->publish(subuf, 0, true, s);
2019-11-18 12:29:36 +01: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 22:13:07 +01:00
strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/status");
mqtt->publish(subuf, 0, true, "online");
2019-03-16 02:09:37 +01:00
char apires[1024];
XML_response(nullptr, apires);
strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/v");
2019-03-16 02:09:37 +01: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();
2019-10-18 14:06:07 +02:00
if (mqttServer[0] == 0 || !WLED_CONNECTED) return false;
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
DEBUG_PRINTLN("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 12:27:06 +02:00
mqtt->setClientId(mqttClientID);
if (mqttUser[0] && mqttPass[0]) mqtt->setCredentials(mqttUser, mqttPass);
2019-11-10 22:13:07 +01:00
strcpy(mqttStatusTopic, mqttDeviceTopic);
strcat(mqttStatusTopic, "/status");
mqtt->setWill(mqttStatusTopic, 0, true, "offline");
mqtt->connect();
return true;
}