From 42b9adf39409aaf5ffb45de246dad01cbb3a7ec2 Mon Sep 17 00:00:00 2001 From: Nis Wechselberg Date: Thu, 30 Mar 2017 14:50:35 +0200 Subject: [PATCH] BenQ: Added power callback handling --- ESP8266-MQTT-BenQ/ESP8266-MQTT-BenQ.ino | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/ESP8266-MQTT-BenQ/ESP8266-MQTT-BenQ.ino b/ESP8266-MQTT-BenQ/ESP8266-MQTT-BenQ.ino index 1fd168d..17dd64b 100644 --- a/ESP8266-MQTT-BenQ/ESP8266-MQTT-BenQ.ino +++ b/ESP8266-MQTT-BenQ/ESP8266-MQTT-BenQ.ino @@ -108,6 +108,18 @@ void incoming_mqtt(char* topic, byte* payload, unsigned int length) { } if (strcmp(topic, mqtt_topic_power) == 0) { + if (length == 1) { + if (DEBUGTOSERIAL) { + Serial.println("Setting new power state"); + } + if ((char)payload[0] == '0') { + // Switch power off + Serial.print("\r*pow=off#\r"); + } else { + // Switch power on + Serial.print("\r*pow=on#\r"); + } + } } } @@ -121,7 +133,7 @@ void reconnect() { Serial.print("Attempting MQTT connection..."); } // Attempt to connect - if (client.connect(mqtt_device_name)) { + if (client.connect(mqtt_device)) { if (DEBUGTOSERIAL) { Serial.println("connected"); } @@ -139,7 +151,16 @@ void reconnect() { } } +void clearSerialInput() { + while(Serial.available() > 0) { + char t = Serial.read(); + } +} + void updateLamptime() { + // Clean input buffer + clearSerialInput(); + // Write request to serial Serial.print("\r*ltim=?#\r"); delay(250);