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