Subscruibe to set
This commit is contained in:
@@ -117,7 +117,8 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
|
||||
EEPROM.commit();
|
||||
String topic = "/home/midroom/lamp";
|
||||
char n = nLamp == R_LED1 ? '1' : '2';
|
||||
if (pub) mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "true" : "false");
|
||||
//if (pub)
|
||||
mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "true" : "false");
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -139,10 +140,12 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
|
||||
}
|
||||
|
||||
void onMqttConnect(bool sessionPresent) {
|
||||
mqttClient.subscribe("/home/midroom/lamp1", 1);
|
||||
mqttClient.subscribe("/home/midroom/lamp2", 1);
|
||||
mqttClient.publish("/home/midroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/midroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/midroom/lamp1_set", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false");
|
||||
mqttClient.publish("/home/midroom/lamp2_set", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false");
|
||||
mqttClient.subscribe("/home/midroom/lamp1_set", 1);
|
||||
mqttClient.subscribe("/home/midroom/lamp2_set", 1);
|
||||
digitalWrite(B_LED, LOW);
|
||||
}
|
||||
|
||||
@@ -154,12 +157,12 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
|
||||
}
|
||||
|
||||
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
if(strcmp(topic, "/home/midroom/lamp1") == 0){
|
||||
if (strcmp("true", payload) == 0) switchLight(R_LED1, 1, false);//lStat1 = true;
|
||||
if(strcmp(topic, "/home/midroom/lamp1_set") == 0){
|
||||
if (strncmp("true", payload, 4) == 0) switchLight(R_LED1, 1, false);//lStat1 = true;
|
||||
else switchLight(R_LED1, 0, false);//lStat1 = false;
|
||||
}
|
||||
if(strcmp(topic, "/home/midroom/lamp2") == 0){
|
||||
if (strcmp("true", payload) == 0) switchLight(R_LED2, 1, false);//lStat2 = true;
|
||||
if(strcmp(topic, "/home/midroom/lamp2_set") == 0){
|
||||
if (strncmp("true", payload, 4) == 0) switchLight(R_LED2, 1, false);//lStat2 = true;
|
||||
else switchLight(R_LED2, 0, false);//lStat2 = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user