diff --git a/Holodolinik_ESP/.gitignore b/Holodolinik_ESP/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/Holodolinik_ESP/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/Holodolinik_ESP/.vscode/extensions.json b/Holodolinik_ESP/.vscode/extensions.json new file mode 100644 index 0000000..e80666b --- /dev/null +++ b/Holodolinik_ESP/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/Holodolinik_ESP/include/README b/Holodolinik_ESP/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/Holodolinik_ESP/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/Holodolinik_ESP/lib/README b/Holodolinik_ESP/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/Holodolinik_ESP/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/Holodolinik_ESP/platformio.ini b/Holodolinik_ESP/platformio.ini new file mode 100644 index 0000000..bfe49dc --- /dev/null +++ b/Holodolinik_ESP/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp07] +platform = espressif8266 +board = esp07 +framework = arduino +lib_deps = + milesburton/DallasTemperature @ ^3.9.1 + mathertel/OneButton @ 0.0.0-alpha+sha.eb583d713a + knolleary/PubSubClient @ ^2.8 + jwrw/ESP_EEPROM @ ^2.1.1 \ No newline at end of file diff --git a/Holodolinik_ESP/src/main.cpp b/Holodolinik_ESP/src/main.cpp new file mode 100644 index 0000000..b0463a5 --- /dev/null +++ b/Holodolinik_ESP/src/main.cpp @@ -0,0 +1,227 @@ +#include +#include +#include +#include +#include +#include +#include + +#define LED_WF (16) //Red +#define LED_WRK (5) //Green +#define LED_MQ (4) //Blue +#define ONE_WIRE_BUS (0) +#define BUTTON (2) +#define BUZZER (14) +#define DOOR_HOL (13) +#define DOOR_MOR (12) + +#define HOST_NAME "Holodilnik" +#define MAIN_TOPIC "/home/kuht/" + +const char* ssid = "wf-home"; +const char* password = "0ndthnrf"; +const char* mqtt_server = "192.168.1.250"; + + +unsigned long cRun = millis(); +bool buzzer_en; +float max_hol, max_mor; +unsigned long wrk_ms; +unsigned long buz_wrk; + +void connectToWifi(); +void connectToMqtt(); + +WiFiClient espClient; +PubSubClient mClient(espClient); +void reconnect(); + +WiFiEventHandler wifiConnectHandler; +WiFiEventHandler wifiDisconnectHandler; +void onWifiConnect(const WiFiEventStationModeGotIP& event); +void onWifiDisconnect(const WiFiEventStationModeDisconnected& event); + +OneWire oneWire(ONE_WIRE_BUS); +DallasTemperature tsens(&oneWire); +DeviceAddress hol = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; +DeviceAddress mor = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}; + +OneButton button(BUTTON, true); +void buttonClick(); +void buttonLongClick(); +void mqttCallback(char* topic, byte* payload, unsigned int length); + +void setup(){ + Serial.begin(9600); + WiFi.mode(WIFI_STA); + WiFi.hostname(HOST_NAME); + Serial.begin(115200); + + ArduinoOTA.begin(); + + wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect); + wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect); + + mClient.setServer("192.168.1.250", 1883); + + pinMode(LED_WF, OUTPUT); + pinMode(LED_MQ, OUTPUT); + pinMode(LED_WRK, OUTPUT); + pinMode(BUTTON, INPUT_PULLUP); + pinMode(DOOR_HOL, INPUT_PULLUP); + pinMode(DOOR_MOR, INPUT_PULLUP); + digitalWrite(LED_WF, LOW); + digitalWrite(LED_MQ, HIGH); + digitalWrite(LED_WRK, HIGH); + + tsens.begin(); + tsens.setResolution(12); + tsens.setWaitForConversion(false); + + button.attachClick(buttonClick); + button.setPressTicks(3000); + button.attachLongPressStart(buttonLongClick); + + buzzer_en = false; + buz_wrk = 0; + + EEPROM.begin(sizeof(float) * 2); + EEPROM.get(0, max_hol); + EEPROM.get(sizeof(float), max_mor); + + connectToWifi(); +} + +void loop(){ + static bool measd = false; + static uint32_t runSecs = 0; + char sVal[6]; + float t_hol, t_mor; + //static bool led_wrk = false; + if(wrk_ms < millis()) + digitalWrite(LED_WRK, HIGH); + // if(buz_wrk < millis()) + // noTone(BUZZER); + ArduinoOTA.handle(); + button.tick(); + if(!mClient.connected()){ + reconnect(); + } + if(mClient.connected()) + digitalWrite(LED_MQ, HIGH); + else + if(!WiFi.isConnected()) + digitalWrite(LED_MQ, LOW); + mClient.loop(); + if(cRun + 499 < millis()){ + cRun = millis(); + if(!measd){ + tsens.requestTemperatures(); + } + else{ + if(mClient.connected()){ + t_hol = tsens.getTempC(hol); + digitalWrite(LED_WRK, LOW); + wrk_ms = millis() + 300; + if(t_hol != DEVICE_DISCONNECTED_C) + sprintf(sVal, "%.1f", t_hol); + else + strcpy(sVal, "NaN"); + mClient.publish(MAIN_TOPIC "t_hol", sVal); + t_mor = tsens.getTempC(mor); + if(t_mor != DEVICE_DISCONNECTED_C) + sprintf(sVal, "%.1f", t_mor); + else + strcpy(sVal, "NaN"); + mClient.publish(MAIN_TOPIC "t_mor", sVal); + // if (t_mor > max_mor || t_hol > max_hol){ + // if(buzzer_en) + // if(buz_wrk+1000 < millis()) + // tone(BUZZER, 1000); + // } + // else{ + // buzzer = true; + // noTone(BUZZER); + // } + } + runSecs++; + if(runSecs % 60){ + if(mClient.connected()){ + sprintf(sVal, "%d", runSecs / 60); + mClient.publish(MAIN_TOPIC "hol_mins", sVal); + } + } + } + } +} + +void reconnect() { + // Loop until we're reconnected + if(!WiFi.isConnected()) return; + for(uint8_t i = 0; i < 3; i++){ + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + if (mClient.connect(HOST_NAME)) { + Serial.println("connected"); + mClient.subscribe(MAIN_TOPIC "max_hol"); + mClient.subscribe(MAIN_TOPIC "max_mor"); + break; + // Once connected, publish an announcement... + } else { + Serial.print("failed, rc="); + Serial.print(mClient.state()); + Serial.println(" try again in 5 seconds"); + // Wait 0.5 seconds before retrying + delay(1000); + } + } +} + +void mqttCallback(char* topic, byte* payload, unsigned int length) { + String msg = ""; + for (unsigned int i = 0; i < length; i++) { + msg += (char)payload[i]; + } + if(strcmp(topic, MAIN_TOPIC "max_hol") == 0){ + max_hol = msg.toFloat(); + EEPROM.put(0, max_hol); + EEPROM.commit(); + } + if(strcmp(topic, MAIN_TOPIC "max_mor") == 0){ + max_mor = msg.toFloat(); + EEPROM.put(sizeof(float), max_hol); + EEPROM.commit(); + } +} + +void connectToWifi() { + Serial.println("Connecting to Wi-Fi..."); + WiFi.begin(ssid, password); + while (!WiFi.isConnected()){ + Serial.print("."); + delay(1000); + } +} + +void onWifiConnect(const WiFiEventStationModeGotIP& event) { + Serial.println("Connected to Wi-Fi."); + Serial.print("IP: "); + Serial.println(WiFi.localIP()); + Serial.println("Connecting to MQTT..."); + //reconnect(); + digitalWrite(LED_WF, LOW); +} + +void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { + Serial.println("Disconnected from Wi-Fi."); + digitalWrite(LED_WF, HIGH); + digitalWrite(LED_MQ, HIGH); +} + +void buttonClick() { + buzzer_en = false; + noTone(BUZZER); +} +void buttonLongClick() { + ESP.reset(); +} \ No newline at end of file diff --git a/Holodolinik_ESP/test/README b/Holodolinik_ESP/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/Holodolinik_ESP/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html diff --git a/I2C Scan/platformio.ini b/I2C Scan/platformio.ini index 8c784d4..b9227be 100644 --- a/I2C Scan/platformio.ini +++ b/I2C Scan/platformio.ini @@ -9,11 +9,15 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -default_envs = nano +default_envs = uno_ob [env:uno] platform = atmelavr board = uno framework = arduino +[env:uno_ob] +platform = atmelavr +board = uno_ob +framework = arduino [env:nodemcuv2] platform = espressif8266 board = nodemcuv2 diff --git a/KuhLight_ESP/src/main.cpp b/KuhLight_ESP/src/main.cpp index 8d706d7..ac7c474 100644 --- a/KuhLight_ESP/src/main.cpp +++ b/KuhLight_ESP/src/main.cpp @@ -129,7 +129,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub) if (pub){ digitalWrite(LED_WRK, LOW); led_ms = millis(); - mqttClient.publish("/home/kuh/lighttbl", 1, false, state ? "true" : "false"); + mqttClient.publish("/home/kuh/lighttbl", 1, false, state ? "1" : "0"); } return state; } @@ -154,9 +154,9 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { } void onMqttConnect(bool sessionPresent) { - mqttClient.publish("/home/kuh/lighttbl_set", 1, false, digitalRead(LAMP) == 1 ? "true" : "false"); + mqttClient.publish("/home/kuh/lighttbl_set", 1, false, digitalRead(LAMP) == 1 ? "1" : "0"); mqttClient.subscribe("/home/kuh/lighttbl_set", 1); - mqttClient.publish("/home/kuh/lighttbl", 1, false, digitalRead(LAMP) == 1 ? "true" : "false"); + mqttClient.publish("/home/kuh/lighttbl", 1, false, digitalRead(LAMP) == 1 ? "1" : "0"); digitalWrite(LED_MQ, HIGH); } @@ -172,7 +172,7 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties strncpy(pl, payload, len); pl[len] = '\0'; if(strcmp(topic, "/home/kuh/lighttbl_set") == 0){ - if (strcmp("true", pl) == 0) switchLight(LAMP, 1, true);//lStat2 = true; + if (strncmp("1", pl, 1) == 0) switchLight(LAMP, 1, true);//lStat2 = true; else switchLight(LAMP, 0, true);//lStat2 = false; } } diff --git a/Sw_BigRoom/platformio.ini b/Sw_BigRoom/platformio.ini index 89df629..36286cf 100644 --- a/Sw_BigRoom/platformio.ini +++ b/Sw_BigRoom/platformio.ini @@ -15,5 +15,5 @@ framework = arduino board_build.ldscript = eagle.flash.2m.ld upload_protocol = espota upload_port = 192.168.1.129 -lib_deps = - joaolopesf/RemoteDebug @ ^3.0.5 \ No newline at end of file +;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 b2a76a5..d68aece 100644 --- a/Sw_BigRoom/src/main.cpp +++ b/Sw_BigRoom/src/main.cpp @@ -6,11 +6,11 @@ #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; +//#include "RemoteDebug.h" //https://github.com/JoaoLopesF/RemoteDebug +//#ifndef DEBUG_DISABLED // Only if debug is not disabled (for production/release) +//RemoteDebug Debug; -#endif +//#endif const char* ssid = "wf-home"; const char* password = "0ndthnrf"; @@ -110,12 +110,12 @@ void setup() { connectToWifi(); cRun = millis(); - Debug.begin("SW-BigRoom"); // Initialize the WiFi server +// Debug.begin("SW-BigRoom"); // Initialize the WiFi server - Debug.setResetCmdEnabled(true); // Enable the reset command +// Debug.setResetCmdEnabled(true); // Enable the reset command - Debug.showProfiler(true); // Profiler (Good to measure times, to optimize codes) - Debug.showColors(true); // Colors +// Debug.showProfiler(true); // Profiler (Good to measure times, to optimize codes) +// Debug.showColors(true); // Colors } void loop() { @@ -137,7 +137,7 @@ void loop() { mqttClient.publish("/home/bigroom/millislamp", 0, false, v); //debugI("*Publish Millis: %u"); } - Debug.handle(); +// Debug.handle(); yield(); } @@ -149,8 +149,8 @@ bool switchLight(uint8_t nLamp, bool state, bool pub) 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"); - debugI("*Publish State %d-%s, lst1:%d, lst2%d", n, state ? "true" : "false", lStat1, lStat2); + mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "1" : "0"); +// debugI("*Publish State %d-%s, lst1:%d, lst2%d", n, state ? "true" : "false", lStat1, lStat2); //} return state; } @@ -173,10 +173,10 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { } void onMqttConnect(bool sessionPresent) { - 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.publish("/home/bigroom/lamp1", 1, false, lStat1 ? "1" : "0"); + mqttClient.publish("/home/bigroom/lamp2", 1, false, lStat2 ? "1" : "0"); + mqttClient.publish("/home/bigroom/lamp1_set", 1, false, lStat1 ? "1" : "0"); + mqttClient.publish("/home/bigroom/lamp2_set", 1, false, lStat2 ? "1" : "0"); mqttClient.subscribe("/home/bigroom/lamp1_set", 1); mqttClient.subscribe("/home/bigroom/lamp2_set", 1); digitalWrite(B_LED, LOW); @@ -194,27 +194,27 @@ void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties strncpy(s, payload, len); if(String(topic) == "/home/bigroom/lamp1_set"){ // if (atoi(payload) == 1) switchLight(R_LED1, 1, false); - if (strncmp("true", payload, 4) == 0){ + if (strncmp("1", payload, 1) == 0){ switchLight(R_LED1, true, false); - debugI("*Switch from MQTT T1: %s", s); +// debugI("*Switch from MQTT T1: %s", s); } //if (atoi(payload) == 1) switchLight(R_LED2, 1, false); //lStat1 = true; else{ switchLight(R_LED1, false, false); - debugI("*Switch from MQTT F1: %s", s); +// debugI("*Switch from MQTT F1: %s", s); } } if(String(topic) == "/home/bigroom/lamp2_set"){ - if (strncmp("true", payload, 4) == 0){ + if (strncmp("1", payload, 1) == 0){ switchLight(R_LED2, true, false); - debugI("*Switch from MQTT T2: %s", s); +// 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); +// debugI("*Switch from MQTT F2: %s", s); } } } diff --git a/Sw_Koridor/src/main.cpp b/Sw_Koridor/src/main.cpp index e708b14..718e4ca 100644 --- a/Sw_Koridor/src/main.cpp +++ b/Sw_Koridor/src/main.cpp @@ -9,7 +9,8 @@ #include #include #include -#include +#include +//#include const char* ssid = "wf-home"; const char* password = "0ndthnrf"; @@ -31,7 +32,8 @@ unsigned long cRun; #define BUTT (5) //bool led = false; -OneButton button(BUTT); +//OneButton button(BUTT); +Bounce lSwitch = Bounce(); bool switchLight(uint8_t nLamp, int state, bool pub); void connectToWifi(); void connectToMqtt(); @@ -54,18 +56,18 @@ void oneClick() //lStat1 = !lStat1; } -void longPress() -{ - //lStat1 = false; - switchLight(R_LED, 0, true); - 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"); -} +// void longPress() +// { +// //lStat1 = false; +// switchLight(R_LED, 0, true); +// 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"); +// } void setup() { //Serial.begin(115200); @@ -126,9 +128,11 @@ void setup() { mqttClient.setServer(mqtt_server, 1883); mqttClient.setClientId("SW_Koridor"); - button.attachClick(oneClick); - button.attachLongPressStart(longPress); - button.setPressTicks(2000); + //button.attachClick(oneClick); + lSwitch.attach(BUTT); + lSwitch.interval(5); + // button.attachLongPressStart(longPress); + //button.setPressTicks(2000); //attachInterrupt(digitalPinToInterrupt(BUTT), sw_func, RISING); @@ -140,7 +144,10 @@ void setup() { void loop() { ArduinoOTA.handle(); - button.tick(); + //button.tick(); + lSwitch.update(); + if(lSwitch.fell()) + switchLight(R_LED, !digitalRead(R_LED), true); // if(lStat1 != oldLStat1){ // digitalWrite(R_LED, lStat1); // oldLStat1 = lStat1; @@ -165,7 +172,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub) EEPROM.put(0, state); EEPROM.commit(); //if (pub) - mqttClient.publish("/home/kor/lamp1", 1, false, state ? "true" : "false"); + mqttClient.publish("/home/kor/lamp1", 1, false, state ? "1" : "0"); return state; } @@ -193,8 +200,8 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { } void onMqttConnect(bool sessionPresent) { - 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.publish("/home/kor/lamp1", 0, false, digitalRead(R_LED) == 1 ? "1" : "0"); + mqttClient.publish("/home/kor/lamp1_set", 0, false, digitalRead(R_LED) == 1 ? "1" : "0"); mqttClient.subscribe("/home/kor/lamp1_set", 1); digitalWrite(B_LED, LOW); } @@ -224,7 +231,7 @@ 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_set") == 0){ - if (strncmp("true", payload, 4) == 0) switchLight(R_LED, 1, false);//lStat1 = true; + if (strncmp("1", payload, 1) == 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 425626c..4705445 100644 --- a/Sw_MidlRoom/src/main.cpp +++ b/Sw_MidlRoom/src/main.cpp @@ -118,7 +118,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub) 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"); + mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "1" : "0"); return state; } @@ -140,10 +140,10 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { } void onMqttConnect(bool sessionPresent) { - 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.publish("/home/midroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "1" : "0"); + mqttClient.publish("/home/midroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "1" : "0"); + mqttClient.publish("/home/midroom/lamp1_set", 1, false, digitalRead(R_LED1) == 1 ? "1" : "0"); + mqttClient.publish("/home/midroom/lamp2_set", 1, false, digitalRead(R_LED2) == 1 ? "1" : "0"); mqttClient.subscribe("/home/midroom/lamp1_set", 1); mqttClient.subscribe("/home/midroom/lamp2_set", 1); digitalWrite(B_LED, LOW); @@ -158,11 +158,11 @@ 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_set") == 0){ - if (strncmp("true", payload, 4) == 0) switchLight(R_LED1, 1, false);//lStat1 = true; + if (strncmp("1", payload, 1) == 0) switchLight(R_LED1, 1, false);//lStat1 = true; else switchLight(R_LED1, 0, false);//lStat1 = false; } if(strcmp(topic, "/home/midroom/lamp2_set") == 0){ - if (strncmp("true", payload, 4) == 0) switchLight(R_LED2, 1, false);//lStat2 = true; + if (strncmp("1", payload, 1) == 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 c5492b9..094d880 100644 --- a/Sw_SmallRoom/src/main.cpp +++ b/Sw_SmallRoom/src/main.cpp @@ -126,7 +126,7 @@ bool switchLight(uint8_t nLamp, int state, bool pub) 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"); + mqttClient.publish(String(topic + n).c_str(), 1, false, state ? "1" : "0"); return state; } @@ -154,10 +154,10 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { } void onMqttConnect(bool sessionPresent) { - 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.publish("/home/smallroom/lamp1", 1, false, digitalRead(R_LED1) == 1 ? "1" : "0"); + mqttClient.publish("/home/smallroom/lamp2", 1, false, digitalRead(R_LED2) == 1 ? "1" : "0"); + mqttClient.publish("/home/smallroom/lamp1_set", 1, false, digitalRead(R_LED1) == 1 ? "1" : "0"); + mqttClient.publish("/home/smallroom/lamp2_set", 1, false, digitalRead(R_LED2) == 1 ? "1" : "0"); mqttClient.subscribe("/home/smallroom/lamp1_set", 1); mqttClient.subscribe("/home/smallroom/lamp2_set", 1); digitalWrite(B_LED, LOW); @@ -172,11 +172,11 @@ 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_set") == 0){ - if (strncmp("true", payload, 4) == 0) switchLight(R_LED1, 1, false);//lStat1 = true; + if (strncmp("1", payload, 1) == 0) switchLight(R_LED1, 1, false);//lStat1 = true; else switchLight(R_LED1, 0, false);//lStat1 = false; } if(strcmp(topic, "/home/smallroom/lamp2_set") == 0){ - if (strncmp("true", payload, 4) == 0) switchLight(R_LED2, 1, false);//lStat2 = true; + if (strncmp("1", payload, 1) == 0) switchLight(R_LED2, 1, false);//lStat2 = true; else switchLight(R_LED2, 0, false);//lStat2 = false; } } diff --git a/VT_ESP8266/include/main.h b/VT_ESP8266/include/main.h index 18901fb..d5357e8 100644 --- a/VT_ESP8266/include/main.h +++ b/VT_ESP8266/include/main.h @@ -14,6 +14,7 @@ #include #include #include +#include #define DEBUG #define WIFI_SSID "wf-home" diff --git a/VT_ESP8266/platformio.ini b/VT_ESP8266/platformio.ini index 4f42a90..71d4b49 100644 --- a/VT_ESP8266/platformio.ini +++ b/VT_ESP8266/platformio.ini @@ -20,3 +20,5 @@ lib_deps = mathertel/RotaryEncoder @ ^1.3.0 robtillaart/PCF8574 @ ^0.2.1 robtillaart/I2C_EEPROM @ ^1.3.0 + ottowinter/ESPAsyncWebServer-esphome @ ^1.3.0 + ayushsharma82/WebSerial @ ^1.2.0