MQTT-BenQ: Added dedicated power state topic

This commit is contained in:
Nis Wechselberg 2018-05-20 14:28:20 +02:00
parent 60fd193b35
commit 657a13a12a

View file

@ -44,7 +44,7 @@
// Timestamp for last publish
long lastMsg = 0;
long publishInterval = 10000;
long publishInterval = 5000;
// Global mqtt client object
WiFiClient espClient;
@ -182,6 +182,28 @@ void updateLamptime() {
}
}
void updatePowerState() {
// Clean input buffer
clearSerialInput();
// Write request to serial
Serial.print("\r*pow=?#\r");
delay(250);
// Extract lamp time from response
String message = Serial.readString();
int startIndex = message.lastIndexOf('=');
int endIndex = message.lastIndexOf('#');
if (startIndex != -1 && endIndex != -1) {
String state = message.substring(startIndex + 1, endIndex);
if (state.equalsIgnoreCase("off")) {
client.publish(mqtt_topic_powerState, "0");
} else if (state.equalsIgnoreCase("on")) {
client.publish(mqtt_topic_powerState, "1");
}
}
}
void loop() {
// Check OTA update
ArduinoOTA.handle();
@ -199,5 +221,6 @@ void loop() {
if (now - lastMsg > publishInterval) {
lastMsg = now;
updateLamptime();
updatePowerState();
}
}