2018-09-28 23:53:51 +02:00
|
|
|
/*
|
|
|
|
* MQTT communication protocol for home automation
|
|
|
|
*/
|
|
|
|
|
2018-09-30 20:24:57 +02:00
|
|
|
void parseMQTTBriPayload(char* payload)
|
|
|
|
{
|
|
|
|
if (strcmp(payload, "ON") == 0) {bri = briLast; colorUpdated(1);}
|
|
|
|
else if (strcmp(payload, "T" ) == 0) {handleSet("win&T=2");}
|
|
|
|
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
|
|
|
|
2018-09-30 20:24:57 +02:00
|
|
|
void callbackMQTT(char* topic, byte* payload, unsigned int length) {
|
|
|
|
|
|
|
|
DEBUG_PRINT("MQTT callb rec: ");
|
|
|
|
DEBUG_PRINTLN(topic);
|
|
|
|
DEBUG_PRINTLN((char*)payload);
|
|
|
|
|
|
|
|
//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
|
|
|
{
|
|
|
|
colorFromDecOrHexString(col, &white, (char*)payload);
|
|
|
|
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;
|
|
|
|
handleSet(apireq);
|
2018-09-30 20:24:57 +02:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
parseMQTTBriPayload((char*)payload);
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2018-10-04 16:50:12 +02: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");
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->publish(subuf, s);
|
2018-10-04 16:50:12 +02:00
|
|
|
|
|
|
|
sprintf(s, "#%X", white*16777216 + col[0]*65536 + col[1]*256 + col[2]);
|
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
strcat(subuf, "/c");
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->publish(subuf, s);
|
2018-10-04 16:50:12 +02:00
|
|
|
|
|
|
|
//if you want to use this, increase the MQTT buffer in PubSubClient.h to 350+
|
|
|
|
//it will publish the API response to MQTT
|
2018-11-16 19:59:00 +01:00
|
|
|
/*XML_response(false, false);
|
2018-10-04 16:50:12 +02:00
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
|
|
|
strcat(subuf, "/v");
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->publish(subuf, obuf);*/
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2018-09-28 23:53:51 +02:00
|
|
|
bool reconnectMQTT()
|
|
|
|
{
|
2018-10-04 18:17:01 +02:00
|
|
|
if (mqtt->connect(escapedMac.c_str()))
|
2018-09-28 23:53:51 +02:00
|
|
|
{
|
|
|
|
//re-subscribe to required topics
|
2018-09-30 20:24:57 +02:00
|
|
|
char subuf[38];
|
2018-10-04 16:50:12 +02:00
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
2018-09-28 23:53:51 +02:00
|
|
|
|
2018-10-04 16:50:12 +02:00
|
|
|
if (mqttDeviceTopic[0] != 0)
|
2018-09-28 23:53:51 +02:00
|
|
|
{
|
2018-10-04 16:50:12 +02:00
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->subscribe(subuf);
|
2018-09-30 20:24:57 +02:00
|
|
|
strcat(subuf, "/col");
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->subscribe(subuf);
|
2018-10-04 16:50:12 +02:00
|
|
|
strcpy(subuf, mqttDeviceTopic);
|
2018-09-30 20:24:57 +02:00
|
|
|
strcat(subuf, "/api");
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->subscribe(subuf);
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 16:50:12 +02:00
|
|
|
if (mqttGroupTopic[0] != 0)
|
2018-09-28 23:53:51 +02:00
|
|
|
{
|
2018-10-04 16:50:12 +02:00
|
|
|
strcpy(subuf, mqttGroupTopic);
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->subscribe(subuf);
|
2018-09-30 20:24:57 +02:00
|
|
|
strcat(subuf, "/col");
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->subscribe(subuf);
|
2018-10-04 16:50:12 +02:00
|
|
|
strcpy(subuf, mqttGroupTopic);
|
2018-09-30 20:24:57 +02:00
|
|
|
strcat(subuf, "/api");
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->subscribe(subuf);
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
2018-11-01 16:16:38 +01:00
|
|
|
|
|
|
|
publishMQTT();
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
2018-10-04 18:17:01 +02:00
|
|
|
return mqtt->connected();
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2018-09-28 23:53:51 +02:00
|
|
|
bool initMQTT()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->setServer(mqttIP,1883);
|
2018-09-28 23:53:51 +02:00
|
|
|
} else {
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->setServer(mqttServer,1883);
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->setCallback(callbackMQTT);
|
2018-09-30 20:24:57 +02:00
|
|
|
DEBUG_PRINTLN("MQTT ready.");
|
2018-09-28 23:53:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
|
2018-09-28 23:53:51 +02:00
|
|
|
void handleMQTT()
|
|
|
|
{
|
|
|
|
if (WiFi.status() != WL_CONNECTED || !mqttInit) return;
|
2018-10-04 16:50:12 +02:00
|
|
|
|
|
|
|
//every time connection is unsuccessful, the attempt interval is increased, since attempt will block program for 7 sec each time
|
2018-10-04 18:17:01 +02:00
|
|
|
if (!mqtt->connected() && millis() - lastMQTTReconnectAttempt > 5000 + (5000 * mqttFailedConAttempts * mqttFailedConAttempts))
|
2018-09-28 23:53:51 +02:00
|
|
|
{
|
2018-09-30 20:24:57 +02:00
|
|
|
DEBUG_PRINTLN("Attempting to connect MQTT...");
|
2018-09-28 23:53:51 +02:00
|
|
|
lastMQTTReconnectAttempt = millis();
|
2018-10-04 16:50:12 +02:00
|
|
|
if (!reconnectMQTT())
|
|
|
|
{
|
|
|
|
//still attempt reconnect about once daily
|
|
|
|
if (mqttFailedConAttempts < 120) mqttFailedConAttempts++;
|
|
|
|
return;
|
|
|
|
}
|
2018-09-30 20:24:57 +02:00
|
|
|
DEBUG_PRINTLN("MQTT con!");
|
2018-10-04 16:50:12 +02:00
|
|
|
mqttFailedConAttempts = 0;
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|
2018-10-04 18:17:01 +02:00
|
|
|
mqtt->loop();
|
2018-09-28 23:53:51 +02:00
|
|
|
}
|