From ece78093049ebba3e8662d973d809d7f38e8656b Mon Sep 17 00:00:00 2001 From: lexa Date: Sat, 28 Nov 2020 20:07:10 +0300 Subject: [PATCH] Subscruibe to set --- Sw_BigRoom/platformio.ini | 4 +- Sw_BigRoom/src/main.cpp | 85 ++++++++++++++++++++++----------------- Sw_Koridor/src/main.cpp | 22 +++++----- Sw_MidlRoom/src/main.cpp | 17 ++++---- Sw_SmallRoom/src/main.cpp | 17 ++++---- 5 files changed, 84 insertions(+), 61 deletions(-) diff --git a/Sw_BigRoom/platformio.ini b/Sw_BigRoom/platformio.ini index d291a6e..89df629 100644 --- a/Sw_BigRoom/platformio.ini +++ b/Sw_BigRoom/platformio.ini @@ -14,4 +14,6 @@ board = nodemcuv2 framework = arduino board_build.ldscript = eagle.flash.2m.ld upload_protocol = espota -upload_port = 192.168.1.129 \ No newline at end of file +upload_port = 192.168.1.129 +lib_deps = + joaolopesf/RemoteDebug @ ^3.0.5 \ No newline at end of file diff --git a/Sw_BigRoom/src/main.cpp b/Sw_BigRoom/src/main.cpp index 3987890..b2a76a5 100644 --- a/Sw_BigRoom/src/main.cpp +++ b/Sw_BigRoom/src/main.cpp @@ -5,6 +5,13 @@ #include #include +//#define DEBUG_DISABLED true +#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug +#ifndef DEBUG_DISABLED // Only if debug is not disabled (for production/release) +RemoteDebug Debug; + +#endif + const char* ssid = "wf-home"; const char* password = "0ndthnrf"; const char* mqtt_server = "192.168.1.250"; @@ -103,6 +110,12 @@ void setup() { connectToWifi(); cRun = millis(); + Debug.begin("SW-BigRoom"); // Initialize the WiFi server + + Debug.setResetCmdEnabled(true); // Enable the reset command + + Debug.showProfiler(true); // Profiler (Good to measure times, to optimize codes) + Debug.showColors(true); // Colors } void loop() { @@ -110,39 +123,22 @@ void loop() { l1.update(); l2.update(); if(l1.fell()){ - switchLight(R_LED1, !lStat1, true); lStat1 = !lStat1; + switchLight(R_LED1, lStat1, true); } if(l2.fell()){ - switchLight(R_LED2, !lStat2, true); lStat2 = !lStat2; + switchLight(R_LED2, lStat2, true); } - // if(lStat1 != oldLStat1){ - // digitalWrite(R_LED1, lStat1); - // oldLStat1 = lStat1; - // if(!rcv) - // mqttClient.publish("/home/bigroom/lamp1", 0, false, lStat1 ? "1" : "0"); - // else - // rcv = false; - // EEPROM.put(0, lStat1); - // EEPROM.commit(); - // } - // if(lStat2 != oldLStat2){ - // digitalWrite(R_LED2, lStat2); - // oldLStat2 = lStat2; - // if(!rcv) - // mqttClient.publish("/home/bigroom/lamp2", 0, false, lStat2 ? "1" : "0"); - // else - // rcv = false; - // EEPROM.put(1, lStat2); - // EEPROM.commit(); - // } if (cRun + 9999 < millis()){ cRun = millis(); char v[11]; ultoa(cRun, v, 10); mqttClient.publish("/home/bigroom/millislamp", 0, false, v); + //debugI("*Publish Millis: %u"); } + Debug.handle(); + yield(); } bool switchLight(uint8_t nLamp, bool state, bool pub) @@ -152,7 +148,10 @@ bool switchLight(uint8_t nLamp, bool state, bool pub) EEPROM.commit(); String topic = "/home/bigroom/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"); + debugI("*Publish State %d-%s, lst1:%d, lst2%d", n, state ? "true" : "false", lStat1, lStat2); + //} return state; } @@ -174,10 +173,12 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { } void onMqttConnect(bool sessionPresent) { - mqttClient.subscribe("/home/bigroom/lamp1", 1); - mqttClient.subscribe("/home/bigroom/lamp2", 1); mqttClient.publish("/home/bigroom/lamp1", 1, false, lStat1 ? "true" : "false"); mqttClient.publish("/home/bigroom/lamp2", 1, false, lStat2 ? "true" : "false"); + mqttClient.publish("/home/bigroom/lamp1_set", 1, false, lStat1 ? "true" : "false"); + mqttClient.publish("/home/bigroom/lamp2_set", 1, false, lStat2 ? "true" : "false"); + mqttClient.subscribe("/home/bigroom/lamp1_set", 1); + mqttClient.subscribe("/home/bigroom/lamp2_set", 1); digitalWrite(B_LED, LOW); } @@ -189,19 +190,31 @@ void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { } void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { - if(String(topic) == "/home/bigroom/lamp1"){ - + char s[10]; + strncpy(s, payload, len); + if(String(topic) == "/home/bigroom/lamp1_set"){ // if (atoi(payload) == 1) switchLight(R_LED1, 1, false); - if (strcmp("true", payload) == 0) switchLight(R_LED1, true, false); - //lStat1 = true; - else switchLight(R_LED1, false, false); - //lStat1 = false; - //rcv = true; - } - if(String(topic) == "/home/bigroom/lamp2"){ - if (strcmp("true", payload) == 0) switchLight(R_LED2, true, false); + if (strncmp("true", payload, 4) == 0){ + switchLight(R_LED1, true, false); + debugI("*Switch from MQTT T1: %s", s); + } //if (atoi(payload) == 1) switchLight(R_LED2, 1, false); //lStat1 = true; - else switchLight(R_LED2, false, false); + else{ + switchLight(R_LED1, false, false); + debugI("*Switch from MQTT F1: %s", s); + } + } + if(String(topic) == "/home/bigroom/lamp2_set"){ + if (strncmp("true", payload, 4) == 0){ + switchLight(R_LED2, true, false); + debugI("*Switch from MQTT T2: %s", s); + } + //if (atoi(payload) == 1) switchLight(R_LED2, 1, false); + //lStat1 = true; + else{ + switchLight(R_LED2, false, false); + debugI("*Switch from MQTT F2: %s", s); + } } } diff --git a/Sw_Koridor/src/main.cpp b/Sw_Koridor/src/main.cpp index 4a1bc75..9f7af38 100644 --- a/Sw_Koridor/src/main.cpp +++ b/Sw_Koridor/src/main.cpp @@ -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; } diff --git a/Sw_MidlRoom/src/main.cpp b/Sw_MidlRoom/src/main.cpp index 472d643..425626c 100644 --- a/Sw_MidlRoom/src/main.cpp +++ b/Sw_MidlRoom/src/main.cpp @@ -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; } } diff --git a/Sw_SmallRoom/src/main.cpp b/Sw_SmallRoom/src/main.cpp index b2b4226..c5492b9 100644 --- a/Sw_SmallRoom/src/main.cpp +++ b/Sw_SmallRoom/src/main.cpp @@ -125,7 +125,8 @@ bool switchLight(uint8_t nLamp, int state, bool pub) EEPROM.commit(); String topic = "/home/smallroom/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; } @@ -153,10 +154,12 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { } void onMqttConnect(bool sessionPresent) { - mqttClient.subscribe("/home/smallroom/lamp1", 1); - mqttClient.subscribe("/home/smallroom/lamp2", 1); mqttClient.publish("/home/smallroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false"); mqttClient.publish("/home/smallroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false"); + mqttClient.publish("/home/smallroom/lamp1_set", 1, false, digitalRead(R_LED1) == 1 ? "true" : "false"); + mqttClient.publish("/home/smallroom/lamp2_set", 1, false, digitalRead(R_LED2) == 1 ? "true" : "false"); + mqttClient.subscribe("/home/smallroom/lamp1_set", 1); + mqttClient.subscribe("/home/smallroom/lamp2_set", 1); digitalWrite(B_LED, LOW); } @@ -168,12 +171,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/smallroom/lamp1") == 0){ - if (strcmp("true", payload) == 0) switchLight(R_LED1, 1, false);//lStat1 = true; + if(strcmp(topic, "/home/smallroom/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/smallroom/lamp2") == 0){ - if (strcmp("true", payload) == 0) switchLight(R_LED2, 1, false);//lStat2 = true; + if(strcmp(topic, "/home/smallroom/lamp2_set") == 0){ + if (strncmp("true", payload, 4) == 0) switchLight(R_LED2, 1, false);//lStat2 = true; else switchLight(R_LED2, 0, false);//lStat2 = false; } }