From 0aab4963c498fa0cacc0ecdcb4730e505c852483 Mon Sep 17 00:00:00 2001 From: lexa Date: Wed, 7 Sep 2022 16:23:10 +0300 Subject: [PATCH] Added fade off lamp --- ESP_BigRoom/src/main.cpp | 24 ++++++++++++++++++----- ESP_Electro/platformio.ini | 4 ++-- ESP_Electro/src/main.cpp | 39 ++++++++++++++++++++++++++++++-------- 3 files changed, 52 insertions(+), 15 deletions(-) diff --git a/ESP_BigRoom/src/main.cpp b/ESP_BigRoom/src/main.cpp index 18afd42..45aa719 100644 --- a/ESP_BigRoom/src/main.cpp +++ b/ESP_BigRoom/src/main.cpp @@ -175,6 +175,7 @@ void setup() { request->send(200, "text/plain", "Hi! I am ESP32."); }); + AsyncElegantOTA.setID("BigRoom"); AsyncElegantOTA.begin(&server); // Start ElegantOTA WebSerial.begin(&server); @@ -185,6 +186,8 @@ void loop() { static uint32_t cRun = millis(); static uint8_t sec = 0; static uint16_t prevLLevel = 0; + static uint32_t levBlue = 0, levWhite = 0; + static unsigned long levRunB = 0, levRunW = 0; char s[7]; //ArduinoOTA.handle(); @@ -202,31 +205,42 @@ void loop() { samples.add(LightLev); LightLev = samples.getMedian(); if ((LightLev < spLight) && move && !bLamp){ - ledcWrite(PWM_BLUE,(levelBlue) * 2.55); + levBlue = levelBlue * 2.55; + ledcWrite(PWM_BLUE, levBlue); statLamp |= 0x01; bLamp = true; Serial.println(F("BLamp On")); WebSerial.println(F("Blue Lamp On")); } else if((LightLev > (spLight + dbLight)) || (move == 0)){ - ledcWrite(PWM_BLUE, 0); + if((levRunB + 50 <= millis() && levBlue > 0)){ + levRunB = millis(); + levBlue--; + } + ledcWrite(PWM_BLUE, levBlue); statLamp &= 0xFE; bLamp = false; } if(lightWhite && !wLamp){ - ledcWrite(PWM_WHITE, (levelWhite) * 2.55); + levWhite = levelWhite * 2.55; + ledcWrite(PWM_WHITE, levWhite); statLamp |= 0x02; wLamp = true; Serial.println(F("WLamp On")); WebSerial.println(F("White Lamp On")); } else if (!lightWhite){ - ledcWrite(PWM_WHITE, 0); + if((levRunW + 30 <= millis() && levWhite > 0)){ + //WebSerial.println(levWhite); + levRunW = millis(); + levWhite--; + } + ledcWrite(PWM_WHITE, levWhite); statLamp &= 0xFD; wLamp = false; } if(statLamp > 0) digitalWrite(POWER, HIGH); - else digitalWrite(POWER, LOW); + else if((levBlue > 0) && (levWhite > 0)) digitalWrite(POWER, LOW); if((cRun + 1000) <= millis()){ //Once per second cRun = millis(); if(abs(LightLev - prevLLevel) > 100){ diff --git a/ESP_Electro/platformio.ini b/ESP_Electro/platformio.ini index d4b5a58..7fcec4d 100644 --- a/ESP_Electro/platformio.ini +++ b/ESP_Electro/platformio.ini @@ -13,13 +13,13 @@ platform = espressif8266 ;board = nodemcuv2 board = esp07 framework = arduino -;board_build.ldscript = eagle.flash.1m.ld +board_build.ldscript = eagle.flash.1m.ld upload_protocol = espota upload_port = 192.168.1.131 -;upload_port = 192.168.1.95 monitor_speed = 115200 lib_deps = sstaub/Ticker @ ^4.3.0 knolleary/PubSubClient @ ^2.8 ottowinter/ESPAsyncWebServer-esphome @ ^3.0.0 ayushsharma82/WebSerial @ ^1.3.0 + robtillaart/RunningMedian @ ^0.3.6 diff --git a/ESP_Electro/src/main.cpp b/ESP_Electro/src/main.cpp index bea1a0e..fbf2f35 100644 --- a/ESP_Electro/src/main.cpp +++ b/ESP_Electro/src/main.cpp @@ -4,6 +4,8 @@ #include #include +#include "RunningMedian.h" + #include #include #include @@ -13,8 +15,8 @@ #define LED_WRK (16) //Green #define LED_MQ (5) //Yellow -#define HOST_NAME "ElectroT" -#define MAIN_TOPIC "/home/kort/" +#define HOST_NAME "Electro" +#define MAIN_TOPIC "home/kor/" /* Hardware: MCP3201 Pin ---------------- ESP8266 Pin @@ -35,13 +37,14 @@ const char* mqtt_server = "192.168.1.111"; const int scePinI = 15, scePinU = 4; // SCE - Chip select const double vRef = 3.3; -const int period = 125; //us +const int period = 150; //us unsigned long cRun = millis(); double curr = 0.0; double volt = 0.0; double currT = 0.0; double currM = 0.0; +float Pavg = 0.0f; int nSec, nSampl; unsigned long sumReading = 0; unsigned long sumReading2 = 0; @@ -66,6 +69,8 @@ Ticker tckrElcnt(mcp_output, period, 0, MICROS_MICROS); AsyncWebServer server(80); +RunningMedian power5s = RunningMedian(5); + void connectToWifi() { Serial.println("Connecting to Wi-Fi..."); WiFi.begin(ssid, password); @@ -88,6 +93,7 @@ void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) { digitalWrite(LED_WF, LOW); } + void setup(){ Serial.begin(9600); WiFi.mode(WIFI_STA); @@ -114,6 +120,8 @@ void setup(){ nSampl = 0; spiBegin(); tckrElcnt.start(); + WebSerial.begin(&server); + server.begin(); } void loop(){ @@ -127,11 +135,20 @@ void loop(){ } mClient.loop(); if(cRun + 999 < millis()){ - cRun = millis(); + digitalWrite(LED_WRK, LOW); nSampl++; currM += curr; I = sqrt(sumReading / double(nSampl)) / 39.7; - V = sqrt(sumReading2 / double(nSampl))/ 1.78; + V = sqrt(sumReading2 / double(nSampl))/ 1.75; + power5s.add(V*I); + WebSerial.println("Millis = " + String(cRun) + ", I = " + String(I) + ", V = " + String(V) + ", Samples = " + String(nSampl)); + char v[7]; + dtostrf(I, 7, 2, v); + mClient.publish(MAIN_TOPIC"I", v); + dtostrf(V, 7, 2, v); + mClient.publish(MAIN_TOPIC"V", v); + dtostrf(power5s.getAverage(), 7, 2, v); + mClient.publish(MAIN_TOPIC"P", v); // Serial.printf("Millis = %lu, I = %f, V = %f, Samples = %d\n", // cRun, I, V, nSampl); // if(WiFi.isConnected()){ @@ -147,17 +164,24 @@ void loop(){ if(++nSec > 59){ nSec = 0; - char v[7]; + // char v[7]; // Serial.printf("Publish min, curr=%.2f, volt=%.2f\n", currA / 60.0f, voltA / 60.0f); // sprintf(v, "%.2f", curr); //mClient.publish(MAIN_TOPIC "curr", v); //mqttClient.publish(MAIN_TOPIC "curr", 1, false, v); + dtostrf(currA/60.0f, 7, 2, v); + mClient.publish(MAIN_TOPIC"I1min", v); + dtostrf(voltA/60.0f, 7, 2, v); + mClient.publish(MAIN_TOPIC"V1min", v); + dtostrf((currA*voltA)/3600.0f, 7, 2, v); + mClient.publish(MAIN_TOPIC"P1min", v); currA = voltA = 0.0f; } + digitalWrite(LED_WRK, HIGH); + cRun = millis(); } } - void mcp_output(){ uint16_t reading; int adcI, adcU; @@ -189,7 +213,6 @@ void mcp_output(){ nSampl++; } - void spiBegin(void) { pinMode(scePinI, OUTPUT);