Subscruibe to set

This commit is contained in:
2020-11-28 20:07:10 +03:00
parent 50b11b44f5
commit ece7809304
5 changed files with 84 additions and 61 deletions

View File

@@ -57,12 +57,12 @@ void longPress()
{
//lStat1 = false;
switchLight(R_LED, 0, true);
mqttClient.publish("/home/bigroom/lamp1", 0, false, "false");
mqttClient.publish("/home/bigroom/lamp2", 0, false, "false");
mqttClient.publish("/home/midroom/lamp1", 0, false, "false");
mqttClient.publish("/home/midroom/lamp2", 0, false, "false");
mqttClient.publish("/home/smallroom/lamp1", 0, false, "false");
mqttClient.publish("/home/smallroom/lamp2", 0, false, "false");
mqttClient.publish("/home/bigroom/lamp1_set", 0, false, "false");
mqttClient.publish("/home/bigroom/lamp2_set", 0, false, "false");
mqttClient.publish("/home/midroom/lamp1_set", 0, false, "false");
mqttClient.publish("/home/midroom/lamp2_set", 0, false, "false");
mqttClient.publish("/home/smallroom/lamp1_set", 0, false, "false");
mqttClient.publish("/home/smallroom/lamp2_set", 0, false, "false");
mqttClient.publish("/home/kuh/lighttbl", 0, false, "false");
}
@@ -158,7 +158,8 @@ bool switchLight(uint8_t nLamp, int state, bool pub)
digitalWrite(nLamp, state);
EEPROM.put(0, state);
EEPROM.commit();
if (pub) mqttClient.publish("/home/kor/lamp1", 1, false, state ? "true" : "false");
//if (pub)
mqttClient.publish("/home/kor/lamp1", 1, false, state ? "true" : "false");
return state;
}
@@ -186,8 +187,9 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
}
void onMqttConnect(bool sessionPresent) {
mqttClient.subscribe("/home/kor/lamp1", 1);
mqttClient.publish("/home/kor/lamp1", 0, false, digitalRead(R_LED) == 1 ? "true" : "false");
mqttClient.publish("/home/kor/lamp1_set", 0, false, digitalRead(R_LED) == 1 ? "true" : "false");
mqttClient.subscribe("/home/kor/lamp1_set", 1);
digitalWrite(B_LED, LOW);
}
@@ -215,8 +217,8 @@ void onMqttUnsubscribe(uint16_t packetId) {
}
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
if(strcmp(topic, "/home/kor/lamp1") == 0){
if (strcmp("true", payload) == 0) switchLight(R_LED, 1, false);//lStat1 = true;
if(strcmp(topic, "/home/kor/lamp1_set") == 0){
if (strncmp("true", payload, 4) == 0) switchLight(R_LED, 1, false);//lStat1 = true;
else switchLight(R_LED, 0, false);//lStat1 = false;
//rcv = true;
}