From 3314626daacd645744cebd334060b9ee949a01d7 Mon Sep 17 00:00:00 2001 From: lexa Date: Sun, 8 Nov 2020 20:53:33 +0300 Subject: [PATCH] Added Move Sensors & Light Sensor --- ESP12SmallRoom/platformio.ini | 4 +++ ESP12SmallRoom/src/main.cpp | 47 +++++++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/ESP12SmallRoom/platformio.ini b/ESP12SmallRoom/platformio.ini index 851892e..2a41d1d 100644 --- a/ESP12SmallRoom/platformio.ini +++ b/ESP12SmallRoom/platformio.ini @@ -16,3 +16,7 @@ board_build.ldscript = eagle.flash.1m.ld board_build.f_cpu = 26000000L upload_protocol = espota upload_port = 192.168.1.7 +lib_deps = + Adafruit HTU21DF Library @ ^1.1.0 + PubSubClient @ ^2.8 +; ESP_EEPROM @ ^2.0.0 \ No newline at end of file diff --git a/ESP12SmallRoom/src/main.cpp b/ESP12SmallRoom/src/main.cpp index 0ae807c..95f265e 100644 --- a/ESP12SmallRoom/src/main.cpp +++ b/ESP12SmallRoom/src/main.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #define LED_BLUE D5 //GPIO14 #define LED_GREEN D6 //GPIO12 @@ -31,11 +32,12 @@ unsigned long cRun; int minCnt = 0; int measCnt = 1; uint8_t mv, oldmv; -uint16_t adc, oldadc, delta; +int adc, oldadc, delta; void reconnect(); void publishMin(); void publishSec(); +void callback(char* topic, byte* payload, unsigned int length); void setup() { Serial.begin(9600); @@ -83,10 +85,14 @@ void setup() { rel_hum = htu.readHumidity(); //digitalWrite(PWR_SENS, LOW); client.setServer(mqtt_server, 1883); + client.setCallback(callback); + EEPROM.begin(8); + EEPROM.get(0, delta); cRun = millis(); } void loop() { + static bool lsSent = false, lbSent = false; ArduinoOTA.handle(); lGreen.tick(); lRed.tick(); @@ -103,10 +109,16 @@ void loop() { mv = digitalRead(MOVE_SENS); if(mv != oldmv){ oldmv = mv; - client.publish("/home/smallroom/light", mv == 0 ? "0" : "1"); + client.publish("/home/smallroom/move", mv == 0 ? "0" : "1"); }; - if(abs(adc - oldadc) > delta){ - //publish + //if(abs(adc - oldadc) > delta){ + if(((adc < delta) && !lsSent) || ((adc >= (delta + 5)) && !lbSent)){ + char strFVal[6]; + itoa(adc, strFVal, 10); + client.publish("/home/smallroom/light", strFVal); + oldadc = adc; + if(adc < delta) {lsSent = true; lbSent = false;} + else {lbSent = true; lsSent = false;} } if(measCnt == 0){ temp += (htu.readTemperature() - temp) / 6.0f; @@ -136,7 +148,10 @@ void reconnect() { //Serial.println("connected"); // "подключен" // подписываемся или переподписываемся на топик; // можно подписаться не только на один, а на несколько топиков - client.subscribe("home/smallroom/ldelta"); + char v[6]; + ltoa(delta, v, 10); + client.publish("/home/smallroom/ldelta", v); + client.subscribe("/home/smallroom/ldelta"); } else { //Serial.print("failed, rc="); // "подключение не удалось" //Serial.print(client.state()); @@ -160,10 +175,10 @@ void publishMin() dtostrf(rel_hum, 6, 1, strFVal); client.publish("/home/smallroom/rel_hum", strFVal); } - itoa(adc, strFVal, 10); + ultoa(adc, strFVal, 10); client.publish("/home/smallroom/light", strFVal); - // itoa(cRun, strFVal, 10); - // client.publish("/home/smallroom/millis", strFVal); + ultoa(cRun, strFVal, 10); + client.publish("/home/smallroom/millis", strFVal); //digitalWrite(LED_BLUE, LOW); } } @@ -184,10 +199,20 @@ void publishSec() } itoa(WiFi.RSSI(), strFVal, 10); client.publish("/hometest/smallroom/RSSI", strFVal); - itoa(cRun, strFVal, 10); + ultoa(cRun, strFVal, 10); client.publish("/hometest/smallroom/millis", strFVal); - itoa(adc, strFVal, 10); - client.publish("/home/smallroom/light", strFVal); + ltoa(adc, strFVal, 10); + client.publish("/hometest/smallroom/light", strFVal); + ultoa(delta, strFVal, 10); + client.publish("/hometest/smallroom/ldelta", strFVal); //digitalWrite(LED_GREEN, LOW); } } + +void callback(char* topic, byte* payload, unsigned int length) { + if(strcmp(topic,"/home/smallroom/ldelta") == 0){ + delta = atoi((char*)payload); + EEPROM.put(0, delta); + EEPROM.commit(); + } +}