2018-09-28 23:53:51 +02:00
|
|
|
/*
|
|
|
|
* MQTT communication protocol for home automation
|
|
|
|
*/
|
|
|
|
|
2019-02-10 23:05:06 +01:00
|
|
|
#define WLED_MQTT_PORT 1883
|
|
|
|
|
2018-09-30 20:24:57 +02:00
|
|
|
void parseMQTTBriPayload(char* payload)
|
|
|
|
{
|
2019-03-01 17:10:42 +01:00
|
|
|
if (strcmp(payload, "ON") == 0 || strcmp(payload, "on") == 0) {bri = briLast; colorUpdated(1);}
|
|
|
|
else if (strcmp(payload, "T" ) == 0 || strcmp(payload, "t" ) == 0) {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;
|
2018-09-28 23:53:51 +02:00
|
|
|
colorUpdated(1);
|
|
|
|
}
|
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];
|
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
publishMqtt();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
DEBUG_PRINT("MQTT callb rec: ");
|
|
|
|
DEBUG_PRINTLN(topic);
|
2019-02-17 19:21:09 +01:00
|
|
|
DEBUG_PRINTLN(payload);
|
2018-09-30 20:24:57 +02:00
|
|
|
|
|
|
|
//no need to check the topic because we only get topics we are subscribed to
|
|
|
|
|
|
|
|
if (strstr(topic, "/col"))
|
2018-09-28 23:53:51 +02:00
|
|
|
{
|
2019-02-05 21:53:39 +01:00
|
|
|
colorFromDecOrHexString(col, (char*)payload);
|
2018-09-28 23:53:51 +02:00
|
|
|
colorUpdated(1);
|
2018-09-30 20:24:57 +02:00
|
|
|
} else if (strstr(topic, "/api"))
|
2018-09-28 23:53:51 +02:00
|
|
|
{
|
2018-10-04 16:50:12 +02:00
|
|
|
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);
|
2019-02-17 19:21:09 +01:00
|
|
|
} else parseMQTTBriPayload(payload);
|
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
|
|
|
{
|
2018-10-04 18:17:01 +02:00
|
|
|
if (mqtt == NULL) return;
|
|
|
|
if (!mqtt->connected()) return;
|
2018-09-30 20:24:57 +02:00
|
|
|
DEBUG_PRINTLN("Publish MQTT");
|
2018-09-28 23:53:51 +02:00
|
|
|
|
2018-10-04 16:50:12 +02:00
|
|
|
char s[10];
|
|
|
|
char subuf[38];
|
|
|
|
|
|
|
|
sprintf(s, "%ld", bri);
|
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
strcat(subuf, "/g");
|
2019-02-17 19:21:09 +01:00
|
|
|
mqtt->publish(subuf, 0, true, s);
|
2018-10-04 16:50:12 +02:00
|
|
|
|
2019-02-05 21:53:39 +01:00
|
|
|
sprintf(s, "#%X", col[3]*16777216 + col[0]*65536 + col[1]*256 + col[2]);
|
2018-10-04 16:50:12 +02:00
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
strcat(subuf, "/c");
|
2019-02-17 19:21:09 +01:00
|
|
|
mqtt->publish(subuf, 0, true, s);
|
2018-10-04 16:50:12 +02:00
|
|
|
|
2019-03-16 02:09:37 +01:00
|
|
|
char apires[1024];
|
|
|
|
XML_response(nullptr, false, apires);
|
2018-10-04 16:50:12 +02:00
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
strcat(subuf, "/v");
|
2019-03-16 02:09:37 +01:00
|
|
|
mqtt->publish(subuf, 0, true, apires);
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (WiFi.status() != WL_CONNECTED) return false;
|
|
|
|
if (mqttServer[0] == 0) return false;
|
|
|
|
|
|
|
|
IPAddress mqttIP;
|
|
|
|
if (mqttIP.fromString(mqttServer)) //see if server is IP or domain
|
|
|
|
{
|
2019-02-10 23:05:06 +01:00
|
|
|
mqtt->setServer(mqttIP, WLED_MQTT_PORT);
|
2018-09-28 23:53:51 +02:00
|
|
|
} else {
|
2019-02-10 23:05:06 +01:00
|
|
|
mqtt->setServer(mqttServer, WLED_MQTT_PORT);
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
2019-02-17 19:21:09 +01:00
|
|
|
mqtt->setClientId(escapedMac.c_str());
|
|
|
|
mqtt->onMessage(onMqttMessage);
|
|
|
|
mqtt->onConnect(onMqttConnect);
|
|
|
|
mqtt->connect();
|
2018-09-30 20:24:57 +02:00
|
|
|
DEBUG_PRINTLN("MQTT ready.");
|
2018-09-28 23:53:51 +02:00
|
|
|
return true;
|
|
|
|
}
|