From 5d7c43fd088b15a94c106daa00eb5ed409facafe Mon Sep 17 00:00:00 2001 From: Vitaliy D Date: Fri, 19 Mar 2021 13:31:52 +0100 Subject: [PATCH 1/4] Disable Blynk Blynk cloud server cert expired thus preventing us from connecting to it. Blynk usage in the code commented out to prevent device from connecting to it until further decision from our side --- include/grower_version.h | 2 +- src/DHTDevice.cpp | 4 ++-- src/DHTDevice.h | 2 +- src/LDRDevice.cpp | 2 +- src/LDRDevice.h | 4 ++-- src/main.cpp | 8 ++++---- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/include/grower_version.h b/include/grower_version.h index f74dcd1..f50a2cd 100644 --- a/include/grower_version.h +++ b/include/grower_version.h @@ -11,7 +11,7 @@ #ifndef GROWER_VERSION_H #define GROWER_VERSION_H -const char GROWER_VERSION[] = "0.8.0-beta2-prom"; +const char GROWER_VERSION[] = "0.8.0-beta3"; // Example version with appended build version (autoincrementing part): // const char VERSION[] = "0.4.0-230498" diff --git a/src/DHTDevice.cpp b/src/DHTDevice.cpp index c30c0e0..d5044f1 100644 --- a/src/DHTDevice.cpp +++ b/src/DHTDevice.cpp @@ -37,7 +37,7 @@ void DHTDevice::tempDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEMP_H } temp = currTemp; PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, device->pin, device->led); - BlynkManager::sendTempToBlynk(temp, blynkTempPin); + // BlynkManager::sendTempToBlynk(temp, blynkTempPin); } void DHTDevice::rhDataHandler(Device* device, uint8_t MAX_RH, uint8_t RH_HYSTERESIS) { @@ -50,7 +50,7 @@ void DHTDevice::rhDataHandler(Device* device, uint8_t MAX_RH, uint8_t RH_HYSTERE } rH = currRH; PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, device->pin, device->led); - BlynkManager::sendRhToBlynk(rH, blynkRhPin); + // BlynkManager::sendRhToBlynk(rH, blynkRhPin); } void DHTDevice::powerOn() { diff --git a/src/DHTDevice.h b/src/DHTDevice.h index e3cdc48..3740249 100644 --- a/src/DHTDevice.h +++ b/src/DHTDevice.h @@ -8,8 +8,8 @@ #include #include #include "Device.h" -#include "BlynkManager.h" #include "MetricsCollectable.h" +// #include "BlynkManager.h" #define DHT_MODEL DHTesp::DHT22 #define DHT_NAME "DHT22" diff --git a/src/LDRDevice.cpp b/src/LDRDevice.cpp index e866878..0894e5d 100644 --- a/src/LDRDevice.cpp +++ b/src/LDRDevice.cpp @@ -28,7 +28,7 @@ void LDRDevice::lampStatus() { Serial.println(F("LDR sensor: lamp is off")); isPowerOn = false; } - BlynkManager::sendLampToBlynk(ledBrightness, led); + // BlynkManager::sendLampToBlynk(ledBrightness, led); } diff --git a/src/LDRDevice.h b/src/LDRDevice.h index 63660dd..ccc3aa5 100644 --- a/src/LDRDevice.h +++ b/src/LDRDevice.h @@ -7,7 +7,7 @@ #include #include "Device.h" -#include "BlynkManager.h" +// #include "BlynkManager.h" // LDR sensor values. The lower value is the brighter is light. const uint8_t LAMP_ON_VALUE = 200; @@ -20,7 +20,7 @@ class LDRDevice: public Device { void lampStatus(); String status(); - + private: uint16_t getLightValue(); diff --git a/src/main.cpp b/src/main.cpp index b9f7452..c482914 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -66,7 +66,7 @@ WebServer server(80); Device lamp, fan, hum; WaterDevice waterDevice; DHTDevice dht; -LDRDevice ldr; +LDRDevice ldr; Scheduler waterScheduler; Scheduler lampOnScheduler, lampOffScheduler; @@ -114,7 +114,7 @@ void setup() { Serial.println(F("************\n\n")); initWiFi(WIFI_SSID, WIFI_PSK); - BlynkManager::init(BLYNK_LCDPIN); + // BlynkManager::init(BLYNK_LCDPIN); dht.init(DHTPIN, BLYNK_GRAPHTEMPPIN, BLYNK_GRAPHRHPIN); ldr.init(LDRPIN, BLYNK_LAMPBRLEDPIN); @@ -128,7 +128,7 @@ void setup() { timer.setInterval(dht.dhtReadInterval, []{dht.tempDataHandler(&lamp, MAX_TEMP, TEMP_HYSTERESIS);}); timer.setInterval(dht.dhtReadInterval, []{dht.rhDataHandler(&hum, MAX_RH, RH_HYSTERESIS);}); timer.setInterval(LIGHT_CHECK_INTERVAL * 1000, []{ldr.lampStatus();}); - timer.setInterval(BLYNK_CHECK_INTERVAL * 1000, BlynkManager::ensureBlynkConnection); + // timer.setInterval(BLYNK_CHECK_INTERVAL * 1000, BlynkManager::ensureBlynkConnection); lampOnScheduler.init([]{lamp.scheduledPowerOn(lampOnScheduler);}, LAMP_ON_SCHEDULE); @@ -162,7 +162,7 @@ void setup() { /****************/ void loop() { - Blynk.run(); + // Blynk.run(); timer.run(); ezt::events(); server.handleClient(); From 59f8951be87d7a2b27dc207bedb9b17fb9fbd754 Mon Sep 17 00:00:00 2001 From: Kristina Dmitrieva Date: Sun, 25 Apr 2021 13:25:03 +0200 Subject: [PATCH 2/4] Get rid of Blynk + refactoring --- include/secrets.h.example | 1 - platformio.ini | 1 - src/BlynkManager.cpp | 53 ---------------------------------- src/BlynkManager.h | 29 ------------------- src/DHTDevice.cpp | 41 +++++++------------------- src/DHTDevice.h | 6 +--- src/Device.cpp | 25 ++++------------ src/Device.h | 15 +++++----- src/LDRDevice.cpp | 10 ------- src/LDRDevice.h | 8 +++-- src/PowerManager.cpp | 33 +++++++++------------ src/PowerManager.h | 9 ++---- src/WaterDevice.cpp | 61 ++++++++++++--------------------------- src/WaterDevice.h | 24 ++++----------- src/main.cpp | 61 ++++++++++----------------------------- src/scheduler.cpp | 19 ------------ src/scheduler.h | 17 +++++++++-- 17 files changed, 103 insertions(+), 310 deletions(-) delete mode 100644 src/BlynkManager.cpp delete mode 100644 src/BlynkManager.h diff --git a/include/secrets.h.example b/include/secrets.h.example index caabb5e..cecc109 100644 --- a/include/secrets.h.example +++ b/include/secrets.h.example @@ -11,7 +11,6 @@ #define ORANGE_SSID "orange-tram" #define ASUS_SSID "tram4-2g" -const char BLYNK_AUTH[] = "blabla124324234234234223dsfassa"; const char WIFI_SSID[] = ASUS_SSID; const char WIFI_PSK[] = "access-point-password"; diff --git a/platformio.ini b/platformio.ini index 4da4a15..528e9d3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -22,6 +22,5 @@ monitor_dtr = 0 lib_deps = ezTime@0.8.3 - https://github.com/Kristty/blynk-library.git#v0.6.1-linker-patch DHT sensor library for ESPx@1.17 https://github.com/jfturcot/SimpleTimer.git#b30890b8f7046bf3b0258e5870e5dda78ac5c71a \ No newline at end of file diff --git a/src/BlynkManager.cpp b/src/BlynkManager.cpp deleted file mode 100644 index d943495..0000000 --- a/src/BlynkManager.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/** - * BlynkManager.cpp - **/ - -#include "BlynkManager.h" -#include "secrets.h" - - -BlynkWifi > Blynk(_blynkTransport); -WidgetLCD blynkLcd(0); - -void BlynkManager::init(uint8_t pin) { - blynkLcd.setVPin(pin); - Blynk.config(BLYNK_AUTH); -} - -// TODO: candidate for debug logging -// Serial.println(F("Blynk: sending temperature and RH data")); -void BlynkManager::sendTempToBlynk(float temp, uint8_t blynkPin) { - // send dta to the LCD widget - String tempStr = "Temp: " + String(temp, 1) + "℃"; - blynkLcd.print(0, 0, tempStr); - // send data to the SuperChart widget - Blynk.virtualWrite(blynkPin, temp); -} - -void BlynkManager::sendRhToBlynk(float rh, uint8_t blynkPin) { - String rHStr = "RH: " + String(rh, 0) + "% "; - blynkLcd.print(0, 1, rHStr); - Blynk.virtualWrite(blynkPin, rh); -} - -// TODO: check if this really needed, assuming that Blynk connection still fails after WiFi issues -void BlynkManager::ensureBlynkConnection() { - if (Blynk.connected()) { - // TODO: candidate for debug logging - // Serial.println(F("Blynk watchdog: connected to the server")); - return; - } else { - // TODO: candidate for error logging - Serial.println(F("Blynk watchdog: connection to the server FAILED! Reconnecting...")); - Blynk.disconnect(); - Blynk.connect(BLYNK_CHECK_INTERVAL); - } -} - - -void BlynkManager::sendLampToBlynk(uint8_t brightness, WidgetLED *led) { - - // TODO: candidate for debug logging - // Serial.println(F("Blynk: sending lamp brightness")); - led->setValue(brightness); -} \ No newline at end of file diff --git a/src/BlynkManager.h b/src/BlynkManager.h deleted file mode 100644 index 86d7761..0000000 --- a/src/BlynkManager.h +++ /dev/null @@ -1,29 +0,0 @@ -/** - * BlynkManager.h - **/ - -#ifndef BLYNKMANAGER_H -#define BLYNKMANAGER_H - - -#include -#include - -// blynk connection check interval in seconds -const uint8_t BLYNK_CHECK_INTERVAL = 30; - -class BlynkManager { - public: - static void init(uint8_t pin); - - static void sendTempToBlynk(float temp, uint8_t blynkPin); - - static void sendRhToBlynk(float rh, uint8_t blynkPin); - - static void ensureBlynkConnection(); - - static void sendLampToBlynk(uint8_t brightness, WidgetLED *led); - -}; - -#endif \ No newline at end of file diff --git a/src/DHTDevice.cpp b/src/DHTDevice.cpp index d5044f1..513e1ac 100644 --- a/src/DHTDevice.cpp +++ b/src/DHTDevice.cpp @@ -4,19 +4,14 @@ #include "DHTDevice.h" -void DHTDevice::init(uint8_t pin, uint8_t blynkTempPin) { +DHTDevice::DHTDevice(uint8_t pin) { + this->pin=pin; pinMode(pin, INPUT_PULLUP); dht.setup(pin, DHT_MODEL); dhtReadInterval = (dht.getMinimumSamplingPeriod()); - Serial.printf((char*)F("DHT sensor read interval is: %u\n"), dhtReadInterval); + Serial.printf((char*)F("DHT sensor read interval is: %ums\n"), dhtReadInterval); } -void DHTDevice::init(uint8_t pin, uint8_t blynkTempPin, uint8_t blynkRhPin) { - init(pin, blynkTempPin); - this->blynkTempPin = blynkTempPin; - this->blynkRhPin = blynkRhPin; -}; - /************ LOGGING ************/ // TODO: candidate for debug logging // Serial.print("DHT " + String(dht.getStatusString())); @@ -28,37 +23,23 @@ void DHTDevice::init(uint8_t pin, uint8_t blynkTempPin, uint8_t blynkRhPin) { // Serial.println(rH); /************ END LOGGING ************/ void DHTDevice::tempDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEMP_HYSTERESIS) { - float currTemp; - currTemp = dht.getTemperature(); - if (isnan(currTemp)) { + temp = dht.getTemperature(); + if (isnan(temp)) { _tempReadErrs++; - Serial.printf((char*)F("DHT: Failed to read temperature!\n\tcurrent value:\t\t\t%f\n\tlast valid value:\t\t%f\n"), currTemp, temp); + Serial.printf((char*)F("DHT: Failed to read temperature! Device status: %s\n"), dht.getStatusString()); return; } - temp = currTemp; - PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, device->pin, device->led); - // BlynkManager::sendTempToBlynk(temp, blynkTempPin); + PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, this->pin); } void DHTDevice::rhDataHandler(Device* device, uint8_t MAX_RH, uint8_t RH_HYSTERESIS) { - float currRH; - currRH = dht.getHumidity(); - if (isnan(currRH)) { + rH = dht.getHumidity(); + if (isnan(rH)) { _rHReadErrs++; - Serial.printf((char*)F("DHT: Failed to read humidity!\n\tcurrent value:\t\t\t%f\n\tlast valid value:\t\t%f\n"), currRH, rH); + Serial.printf((char*)F("DHT: Failed to read humidity! Device status: %s\n"), dht.getStatusString()); return; } - rH = currRH; - PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, device->pin, device->led); - // BlynkManager::sendRhToBlynk(rH, blynkRhPin); -} - -void DHTDevice::powerOn() { - -} - -void DHTDevice::powerOff() { - + PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, this->pin); } String DHTDevice::status() { diff --git a/src/DHTDevice.h b/src/DHTDevice.h index 3740249..1aefd7c 100644 --- a/src/DHTDevice.h +++ b/src/DHTDevice.h @@ -9,7 +9,6 @@ #include #include "Device.h" #include "MetricsCollectable.h" -// #include "BlynkManager.h" #define DHT_MODEL DHTesp::DHT22 #define DHT_NAME "DHT22" @@ -17,13 +16,10 @@ class DHTDevice: public Device, MetricsCollectable { public: - uint16_t blynkTempPin, blynkRhPin; uint16_t dhtReadInterval; float temp, rH; - void init(uint8_t pin, uint8_t blynkTempPin); - - void init(uint8_t pin, uint8_t blynkTempPin, uint8_t blynkRhPin); + DHTDevice(uint8_t pin); void tempDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEMP_HYSTERESIS); diff --git a/src/Device.cpp b/src/Device.cpp index 5bd77b4..187b05f 100644 --- a/src/Device.cpp +++ b/src/Device.cpp @@ -4,36 +4,23 @@ #include "Device.h" -Device::Device() { -}; - -void Device::init(uint8_t pin, uint8_t blynkPin) { - this->pin = pin; - this->led = new WidgetLED(blynkPin); - - pinMode(pin, OUTPUT); - - isPowerOn = PowerManager::manualPower(true, pin, led); - isAutoPowerOn = true; -}; - void Device::powerOn() { - isPowerOn = PowerManager::manualPower(true, pin, led); - isAutoPowerOn = true; + this->isPowerOn = PowerManager::manualPowerOn(pin); + this->isAutoPowerOn = true; } void Device::powerOff() { - this->isPowerOn = PowerManager::manualPower(false, pin, led); - isAutoPowerOn = false; + this->isPowerOn = PowerManager::manualPowerOff(pin); + this->isAutoPowerOn = false; } void Device::scheduledPowerOn(Scheduler scheduler) { - powerOn(); + this->powerOn(); scheduler.setNextEvent(); } void Device::scheduledPowerOff(Scheduler scheduler) { - powerOff(); + this->powerOff(); scheduler.setNextEvent(); } diff --git a/src/Device.h b/src/Device.h index 78ed3f4..1fa34c3 100644 --- a/src/Device.h +++ b/src/Device.h @@ -6,23 +6,24 @@ #define DEVICE_H #include -#include #include "PowerManager.h" #include "scheduler.h" class Device { public: - - Device(); - uint8_t pin; - uint8_t blynkPin; - WidgetLED *led; bool isPowerOn; bool isAutoPowerOn; - void init(uint8_t pin, uint8_t blynkPin); + Device(){}; + + Device(uint8_t pin): + pin(pin), + isAutoPowerOn(true) { + pinMode(pin, OUTPUT); + this->isPowerOn = PowerManager::manualPowerOn(pin); + }; void powerOn(); diff --git a/src/LDRDevice.cpp b/src/LDRDevice.cpp index 0894e5d..0bf3ad6 100644 --- a/src/LDRDevice.cpp +++ b/src/LDRDevice.cpp @@ -4,21 +4,12 @@ #include "LDRDevice.h" -void LDRDevice::init(uint8_t pin, uint8_t blynkPin) { - this->pin = pin; - this->blynkPin = blynkPin; - this->led = new WidgetLED(blynkPin); - uint16_t lightVal = getLightValue(); - this->isPowerOn = lightVal < LAMP_ON_VALUE ? true : false; -} - uint16_t LDRDevice::getLightValue() { return analogRead(pin); } void LDRDevice::lampStatus() { uint16_t lightVal = getLightValue(); - uint8_t ledBrightness = map(lightVal, 0, 1023, 255, 0); // TODO: candidate for debug logging // Serial.println("LDR sensor: " + String(lightVal)); if (lightVal < LAMP_ON_VALUE && !isPowerOn) { @@ -28,7 +19,6 @@ void LDRDevice::lampStatus() { Serial.println(F("LDR sensor: lamp is off")); isPowerOn = false; } - // BlynkManager::sendLampToBlynk(ledBrightness, led); } diff --git a/src/LDRDevice.h b/src/LDRDevice.h index ccc3aa5..07cd99d 100644 --- a/src/LDRDevice.h +++ b/src/LDRDevice.h @@ -7,7 +7,6 @@ #include #include "Device.h" -// #include "BlynkManager.h" // LDR sensor values. The lower value is the brighter is light. const uint8_t LAMP_ON_VALUE = 200; @@ -15,7 +14,12 @@ const uint16_t LAMP_OFF_VALUE = 900; class LDRDevice: public Device { public: - void init(uint8_t pin, uint8_t blynkPin); + + LDRDevice(uint8_t pin) { + this->pin = pin; + uint16_t lightVal = getLightValue(); + this->isPowerOn = lightVal < LAMP_ON_VALUE ? true : false; + }; void lampStatus(); diff --git a/src/PowerManager.cpp b/src/PowerManager.cpp index fb69685..a4f538e 100644 --- a/src/PowerManager.cpp +++ b/src/PowerManager.cpp @@ -5,29 +5,22 @@ #include "PowerManager.h" - -void PowerManager::autoPower(bool *autoControl, bool *isOn, float *currVal, float maxVal, float valHyst, uint8_t pin, WidgetLED *led) { +void PowerManager::autoPower(bool *autoControl, bool *currentState, float *currVal, float maxVal, float valHyst, uint8_t pin) { if (!*autoControl) return; - if (*currVal >= maxVal && *isOn) { - *isOn = manualPower(false, pin, led); + if (*currVal >= maxVal && *currentState) { + *currentState = manualPowerOff(pin); } - else if (*currVal < maxVal - valHyst && !*isOn) { - *isOn = manualPower(true, pin, led); + else if (*currVal < maxVal - valHyst && !*currentState) { + *currentState = manualPowerOn(pin); } } -bool PowerManager::manualPower(bool enabled, uint8_t pin, WidgetLED *led) { - bool isOn; - if (enabled) { - digitalWrite(pin, RELAY_ON); - isOn = true; - Serial.println("Blynk: enabling led for pin: " + String(pin)); - led->on(); - } else { - digitalWrite(pin, RELAY_OFF); - isOn = false; - Serial.println("Blynk: disabling led for pin: " + String(pin)); - led->off(); - } - return isOn; +bool PowerManager::manualPowerOn(uint8_t pin) { + digitalWrite(pin, RELAY_ON); + return true; +} + +bool PowerManager::manualPowerOff(uint8_t pin) { + digitalWrite(pin, RELAY_OFF); + return false; } \ No newline at end of file diff --git a/src/PowerManager.h b/src/PowerManager.h index f0db237..3bd4842 100644 --- a/src/PowerManager.h +++ b/src/PowerManager.h @@ -10,16 +10,13 @@ #define RELAY_OFF HIGH #include -#include class PowerManager { public: - - static void autoPower(bool *autoControl, bool *isOn, float *currVal, float maxVal, float valHyst, uint8_t pin, WidgetLED *led); - - static bool manualPower(bool enabled, uint8_t pin, WidgetLED *led); + static void autoPower(bool *autoControl, bool *isOn, float *currVal, float maxVal, float valHyst, uint8_t pin); + static bool manualPowerOn(uint8_t pin); + static bool manualPowerOff(uint8_t pin); }; - #endif \ No newline at end of file diff --git a/src/WaterDevice.cpp b/src/WaterDevice.cpp index 988087c..62bc097 100644 --- a/src/WaterDevice.cpp +++ b/src/WaterDevice.cpp @@ -4,62 +4,39 @@ #include "WaterDevice.h" - -WaterDevice::WaterDevice() { -}; - -void WaterDevice::init() { - pinMode(PUMPPIN, OUTPUT); - digitalWrite(PUMPPIN, PUMP_OFF); - Blynk.virtualWrite(BLYNK_GRAPHPUMPPIN, 0); - - waterScheduler.init(water, WATER_SCHEDULE); -} - -void WaterDevice::init(uint8_t pin, uint8_t blynkPin) { +WaterDevice::WaterDevice(uint8_t pin) { this->pin = pin; - this->blynkPin = blynkPin; pinMode(pin, OUTPUT); digitalWrite(pin, PUMP_OFF); - Blynk.virtualWrite(blynkPin, 0); }; void WaterDevice::powerOn() { - // TODO implement after scheduler init will be moved to main + digitalWrite(PUMPPIN, PUMP_ON); + Serial.println(F("[MAIN] [I] Pump is on")); } void WaterDevice::powerOff() { - // TODO implement after scheduler init will be moved to main -} - -String WaterDevice::status() { - return "{\"date_time\":\"" + waterScheduler.getStartDateTime(ISO8601) +"\"," - + "\"date_time\":\"" + waterScheduler.getNextDateTime(ISO8601) + "\"," - + "\"days\":\"" + String(WATER_SCHEDULE.intervalDays) +"\"," - + "\"duration\":\"" + String(WATER_DURATION) +"\"}"; + digitalWrite(PUMPPIN, PUMP_OFF); + Serial.println(F("[MAIN] [I] Pump is off")); } -// TODO toBeRemoved after scheduler init will be moved to main -void WaterDevice::pumpOn() { - digitalWrite(PUMPPIN, PUMP_ON); - Serial.println(F("[MAIN] [I] Pump is on")); - Serial.println(F("Blynk: sending pump status")); - Blynk.virtualWrite(BLYNK_GRAPHPUMPPIN, 1); +void WaterDevice::water(timer_callback pumpOffFunc) { + this->powerOn(); + timer.setTimeout(WATER_DURATION * 1000, pumpOffFunc); } -// TODO toBeRemoved after scheduler init will be moved to main -void WaterDevice::pumpOff() { - digitalWrite(PUMPPIN, PUMP_OFF); - Serial.println(F("[MAIN] [I] Pump is off")); +void WaterDevice::scheduledWater(Scheduler scheduler, timer_callback pumpOffFunc) { + this->water(pumpOffFunc); + scheduler.setNextEvent(); +}; - Serial.println(F("Blynk: sending pump status")); - Blynk.virtualWrite(BLYNK_GRAPHPUMPPIN, 0); -} +String WaterDevice::status() { +// TODO: generate from template using snprintf -void WaterDevice::water() { - pumpOn(); - timer.setTimeout(WATER_DURATION * 1000, pumpOff); - waterScheduler.setNextEvent(); -}; \ No newline at end of file + // return "{\"date_time\":\"" + waterScheduler.getStartDateTime(ISO8601) +"\"," + // + "\"date_time\":\"" + waterScheduler.getNextDateTime(ISO8601) + "\"," + // + "\"days\":\"" + String(WATER_SCHEDULE.intervalDays) +"\"," + // + "\"duration\":\"" + String(WATER_DURATION) +"\"}"; +} \ No newline at end of file diff --git a/src/WaterDevice.h b/src/WaterDevice.h index 03ae4b4..7c5be7a 100644 --- a/src/WaterDevice.h +++ b/src/WaterDevice.h @@ -7,45 +7,31 @@ #define PUMP_ON HIGH #define PUMP_OFF LOW -#define BLYNK_GRAPHPUMPPIN V6 #define PUMPPIN D2 #include #include -#include #include #include #include "Device.h" extern SimpleTimer timer; -extern Scheduler waterScheduler; -// TODO get rid of static, after scheduler init will be moved to main class WaterDevice: public Device { public: - WaterDevice(); - - static void init(); - - void init(uint8_t pin, uint8_t blynkPin); + WaterDevice(uint8_t pin); void powerOn(); void powerOff(); - String status(); - - static void pumpOn(); + void water(timer_callback pumpOffFunc); + + void scheduledWater(Scheduler scheduler, timer_callback pumpOffFunc); - static void pumpOff(); - - static void water(); - - private: - uint8_t pin; - uint8_t blynkPin; + String status(); }; diff --git a/src/main.cpp b/src/main.cpp index c482914..338d43c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,8 @@ /* * main.cpp - * - * !!! IMPORTANT !!! don't send more that 10 values per second to Blynk - * + * */ -/* Comment this out to disable Blynk prints and save space: */ -//#define BLYNK_PRINT Serial // Defines the object that is used for printing -//#define BLYNK_DEBUG // Optional, this enables more detailed prints - #include #include #include @@ -39,16 +33,6 @@ #define LDRPIN A0 #define PUMPPIN D2 -/* Blynk defines */ -#define BLYNK_LCDPIN V0 -#define BLYNK_GRAPHTEMPPIN V1 -#define BLYNK_GRAPHRHPIN V2 -#define BLYNK_LAMPBRLEDPIN V4 -#define BLYNK_LAMPLEDPIN V5 -#define BLYNK_GRAPHPUMPPIN V6 -#define BLYNK_FANLEDPIN V7 -#define BLYNK_HUMLEDPIN V8 - /* monitoring constants */ const uint8_t MAX_TEMP = 40; const uint8_t TEMP_HYSTERESIS = 10; @@ -63,10 +47,12 @@ const uint8_t LIGHT_CHECK_INTERVAL = 1; SimpleTimer timer; WebServer server(80); -Device lamp, fan, hum; -WaterDevice waterDevice; -DHTDevice dht; -LDRDevice ldr; +Device fan(FANRELAYPIN); +Device hum(HUMRELAYPIN); +Device lamp(LAMPRELAYPIN); +WaterDevice waterDevice(PUMPPIN); +DHTDevice dht(DHTPIN); +LDRDevice ldr(LDRPIN); Scheduler waterScheduler; Scheduler lampOnScheduler, lampOffScheduler; @@ -114,29 +100,11 @@ void setup() { Serial.println(F("************\n\n")); initWiFi(WIFI_SSID, WIFI_PSK); - // BlynkManager::init(BLYNK_LCDPIN); - dht.init(DHTPIN, BLYNK_GRAPHTEMPPIN, BLYNK_GRAPHRHPIN); - ldr.init(LDRPIN, BLYNK_LAMPBRLEDPIN); - - // delay Blynk dependant init functions - timer.setTimeout(2000, []{lamp.init(LAMPRELAYPIN, BLYNK_LAMPLEDPIN);}); - timer.setTimeout(3000, []{fan.init(FANRELAYPIN, BLYNK_FANLEDPIN);}); - timer.setTimeout(4000, []{hum.init(HUMRELAYPIN, BLYNK_HUMLEDPIN);}); - timer.setTimeout(5000, []{waterDevice.init(PUMPPIN, BLYNK_GRAPHPUMPPIN);}); /* SimpleTimer function execution scheduling */ timer.setInterval(dht.dhtReadInterval, []{dht.tempDataHandler(&lamp, MAX_TEMP, TEMP_HYSTERESIS);}); timer.setInterval(dht.dhtReadInterval, []{dht.rhDataHandler(&hum, MAX_RH, RH_HYSTERESIS);}); timer.setInterval(LIGHT_CHECK_INTERVAL * 1000, []{ldr.lampStatus();}); - // timer.setInterval(BLYNK_CHECK_INTERVAL * 1000, BlynkManager::ensureBlynkConnection); - - - lampOnScheduler.init([]{lamp.scheduledPowerOn(lampOnScheduler);}, LAMP_ON_SCHEDULE); - lampOffScheduler.init([]{lamp.scheduledPowerOff(lampOffScheduler);}, LAMP_OFF_SCHEDULE); - fanOnScheduler.init([]{fan.scheduledPowerOn(fanOnScheduler);}, FAN_ON_SCHEDULE); - fanOffScheduler.init([]{fan.scheduledPowerOff(fanOffScheduler);}, FAN_OFF_SCHEDULE); - humOnScheduler.init([]{hum.scheduledPowerOn(humOnScheduler);}, HUM_ON_SCHEDULE); - humOffScheduler.init([]{hum.scheduledPowerOff(humOffScheduler);}, HUM_OFF_SCHEDULE); // TODO: candidate for debug logging // Serial.print(F("[MAIN] [D] Requested watering start time: ")); @@ -144,6 +112,14 @@ void setup() { // Serial.print(F("[MAIN] [D] Next watering scheduled on: ")); // Serial.println(waterScheduler.getNextDateTime()); + waterScheduler = Scheduler([]{waterDevice.scheduledWater(waterScheduler, []{waterDevice.powerOff();});}, WATER_SCHEDULE); + lampOnScheduler = Scheduler([]{lamp.scheduledPowerOn(lampOnScheduler);}, LAMP_ON_SCHEDULE); + lampOffScheduler = Scheduler([]{lamp.scheduledPowerOff(lampOffScheduler);}, LAMP_OFF_SCHEDULE); + fanOnScheduler = Scheduler([]{fan.scheduledPowerOn(fanOnScheduler);}, FAN_ON_SCHEDULE); + fanOffScheduler = Scheduler([]{fan.scheduledPowerOff(fanOffScheduler);}, FAN_OFF_SCHEDULE); + humOnScheduler = Scheduler([]{hum.scheduledPowerOn(humOnScheduler);}, HUM_ON_SCHEDULE); + humOffScheduler = Scheduler([]{hum.scheduledPowerOff(humOffScheduler);}, HUM_OFF_SCHEDULE); + server.registerEndpoint(&lamp, LAMP_ENDPOINTS, sizeof(LAMP_ENDPOINTS)/sizeof(LAMP_ENDPOINTS[0])); server.registerEndpoint(&fan, FAN_ENDPOINTS, sizeof(FAN_ENDPOINTS)/sizeof(FAN_ENDPOINTS[0])); server.registerEndpoint(&hum, HUM_ENDPOINTS, sizeof(HUM_ENDPOINTS)/sizeof(HUM_ENDPOINTS[0])); @@ -151,10 +127,7 @@ void setup() { server.registerEndpoint(&dht, DHT_ENDPOINTS, sizeof(DHT_ENDPOINTS)/sizeof(DHT_ENDPOINTS[0])); server.registerEndpoint(&ldr, LDR_ENDPOINTS, sizeof(LDR_ENDPOINTS)/sizeof(LDR_ENDPOINTS[0])); - - server.begin(); - } /****************/ @@ -162,9 +135,7 @@ void setup() { /****************/ void loop() { - // Blynk.run(); timer.run(); ezt::events(); server.handleClient(); -} - +} \ No newline at end of file diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 06306ec..8eb0438 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -4,25 +4,6 @@ #include "scheduler.h" -Scheduler::Scheduler() { -}; - -void Scheduler::init(void (*function)(), schedule schedule, String *location) { - this->function = function; - - startSec = schedule.sec; - startMin = schedule.min; - startHr = schedule.hr; - startDay = schedule.day; - startMnth = schedule.mnth; - startYear = schedule.year; - intervalDays = schedule.intervalDays; - - this->_tz = new Timezone(); - initTimezone(location); - setNextEvent(); -} - time_t Scheduler::getStartUnixTime() { return makeTime(startHr, startMin, startSec, startDay, startMnth, startYear); } diff --git a/src/scheduler.h b/src/scheduler.h index 9e988e4..31d9a98 100644 --- a/src/scheduler.h +++ b/src/scheduler.h @@ -27,9 +27,22 @@ class Scheduler { uint16_t startYear; uint8_t intervalDays; - Scheduler(); + Scheduler(){}; - void init(void (*function)(), schedule, String* = new String(DEFAULT_LOCATION)); +// TODO: replace start* fields with startUnixTime field initialized in constructor. + Scheduler(void (*function)(), schedule schedule, String* location = new String(DEFAULT_LOCATION)): + function(function), + startSec(schedule.sec), + startMin(schedule.min), + startHr(schedule.hr), + startDay(schedule.day), + startMnth(schedule.mnth), + startYear(schedule.year), + intervalDays(schedule.intervalDays) { + this->_tz = new Timezone(); + initTimezone(location); + setNextEvent(); + }; time_t getStartUnixTime(); time_t getNextUnixTime(); From e33ef6ffef17132bc5906cfd589947dbeb3e16f1 Mon Sep 17 00:00:00 2001 From: Kristina Dmitrieva Date: Tue, 4 May 2021 15:58:16 +0200 Subject: [PATCH 3/4] Refactor scheduler --- src/scheduler.cpp | 11 +++-------- src/scheduler.h | 15 ++++----------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/src/scheduler.cpp b/src/scheduler.cpp index 8eb0438..1449bd2 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -4,17 +4,12 @@ #include "scheduler.h" -time_t Scheduler::getStartUnixTime() { - return makeTime(startHr, startMin, startSec, startDay, startMnth, startYear); -} - time_t Scheduler::getNextUnixTime() { - time_t t = getStartUnixTime(); + time_t t = startUnixTime; time_t intervalDaySecs = intervalDays * SECS_PER_DAY; - while (t <= _tz->now()) - { + while (t <= _tz->now()) { t += intervalDaySecs; } @@ -22,7 +17,7 @@ time_t Scheduler::getNextUnixTime() { }; String Scheduler::getStartDateTime(const String format) { - return _tz->dateTime(getStartUnixTime(), format); + return _tz->dateTime(startUnixTime, format); } String Scheduler::getNextDateTime(const String format) { diff --git a/src/scheduler.h b/src/scheduler.h index 31d9a98..a4565da 100644 --- a/src/scheduler.h +++ b/src/scheduler.h @@ -23,28 +23,21 @@ typedef struct { class Scheduler { public: void (*function)(); - uint8_t startSec, startMin, startHr, startDay, startMnth; - uint16_t startYear; uint8_t intervalDays; + time_t startUnixTime; Scheduler(){}; -// TODO: replace start* fields with startUnixTime field initialized in constructor. Scheduler(void (*function)(), schedule schedule, String* location = new String(DEFAULT_LOCATION)): function(function), - startSec(schedule.sec), - startMin(schedule.min), - startHr(schedule.hr), - startDay(schedule.day), - startMnth(schedule.mnth), - startYear(schedule.year), intervalDays(schedule.intervalDays) { this->_tz = new Timezone(); + startUnixTime = makeTime(schedule.hr, schedule.min, schedule.sec, schedule.day, schedule.mnth, schedule.year); initTimezone(location); setNextEvent(); - }; + Serial.println("Scheduler initialized with start time" + getStartDateTime()); + }; - time_t getStartUnixTime(); time_t getNextUnixTime(); /* From ee18f4c414794c48f503a7d31fd350ee3f8cd5eb Mon Sep 17 00:00:00 2001 From: Kristina Dmitrieva Date: Wed, 15 Sep 2021 21:15:38 +0200 Subject: [PATCH 4/4] Minor refactoring after code review --- src/DHTDevice.cpp | 12 ++---------- src/DHTDevice.h | 8 +++++++- src/Device.cpp | 4 ++-- src/Device.h | 4 ++-- src/LDRDevice.cpp | 2 +- src/LDRDevice.h | 2 +- src/WaterDevice.cpp | 16 ++++------------ src/WaterDevice.h | 8 +++++--- src/main.cpp | 6 +++++- src/scheduler.h | 2 +- 10 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/DHTDevice.cpp b/src/DHTDevice.cpp index 513e1ac..10f2cf3 100644 --- a/src/DHTDevice.cpp +++ b/src/DHTDevice.cpp @@ -4,14 +4,6 @@ #include "DHTDevice.h" -DHTDevice::DHTDevice(uint8_t pin) { - this->pin=pin; - pinMode(pin, INPUT_PULLUP); - dht.setup(pin, DHT_MODEL); - dhtReadInterval = (dht.getMinimumSamplingPeriod()); - Serial.printf((char*)F("DHT sensor read interval is: %ums\n"), dhtReadInterval); -} - /************ LOGGING ************/ // TODO: candidate for debug logging // Serial.print("DHT " + String(dht.getStatusString())); @@ -29,7 +21,7 @@ void DHTDevice::tempDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEMP_H Serial.printf((char*)F("DHT: Failed to read temperature! Device status: %s\n"), dht.getStatusString()); return; } - PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, this->pin); + PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &temp, MAX_TEMP, TEMP_HYSTERESIS, this->_pin); } void DHTDevice::rhDataHandler(Device* device, uint8_t MAX_RH, uint8_t RH_HYSTERESIS) { @@ -39,7 +31,7 @@ void DHTDevice::rhDataHandler(Device* device, uint8_t MAX_RH, uint8_t RH_HYSTERE Serial.printf((char*)F("DHT: Failed to read humidity! Device status: %s\n"), dht.getStatusString()); return; } - PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, this->pin); + PowerManager::autoPower(&device->isAutoPowerOn, &device->isPowerOn, &rH, MAX_RH, RH_HYSTERESIS, this->_pin); } String DHTDevice::status() { diff --git a/src/DHTDevice.h b/src/DHTDevice.h index 1aefd7c..9e2ed4c 100644 --- a/src/DHTDevice.h +++ b/src/DHTDevice.h @@ -19,7 +19,13 @@ class DHTDevice: public Device, MetricsCollectable { uint16_t dhtReadInterval; float temp, rH; - DHTDevice(uint8_t pin); + DHTDevice(uint8_t pin){ + this->_pin = pin; + pinMode(pin, INPUT_PULLUP); + dht.setup(pin, DHT_MODEL); + dhtReadInterval = (dht.getMinimumSamplingPeriod()); + Serial.printf((char *)F("DHT sensor read interval is: %ums\n"), dhtReadInterval); + } void tempDataHandler(Device* device, uint8_t MAX_TEMP, uint8_t TEMP_HYSTERESIS); diff --git a/src/Device.cpp b/src/Device.cpp index 187b05f..4616f0e 100644 --- a/src/Device.cpp +++ b/src/Device.cpp @@ -5,12 +5,12 @@ #include "Device.h" void Device::powerOn() { - this->isPowerOn = PowerManager::manualPowerOn(pin); + this->isPowerOn = PowerManager::manualPowerOn(_pin); this->isAutoPowerOn = true; } void Device::powerOff() { - this->isPowerOn = PowerManager::manualPowerOff(pin); + this->isPowerOn = PowerManager::manualPowerOff(_pin); this->isAutoPowerOn = false; } diff --git a/src/Device.h b/src/Device.h index 1fa34c3..fb62889 100644 --- a/src/Device.h +++ b/src/Device.h @@ -12,14 +12,14 @@ class Device { public: - uint8_t pin; + uint8_t _pin; bool isPowerOn; bool isAutoPowerOn; Device(){}; Device(uint8_t pin): - pin(pin), + _pin(pin), isAutoPowerOn(true) { pinMode(pin, OUTPUT); this->isPowerOn = PowerManager::manualPowerOn(pin); diff --git a/src/LDRDevice.cpp b/src/LDRDevice.cpp index 0bf3ad6..9e99240 100644 --- a/src/LDRDevice.cpp +++ b/src/LDRDevice.cpp @@ -5,7 +5,7 @@ #include "LDRDevice.h" uint16_t LDRDevice::getLightValue() { - return analogRead(pin); + return analogRead(_pin); } void LDRDevice::lampStatus() { diff --git a/src/LDRDevice.h b/src/LDRDevice.h index 07cd99d..df72197 100644 --- a/src/LDRDevice.h +++ b/src/LDRDevice.h @@ -16,7 +16,7 @@ class LDRDevice: public Device { public: LDRDevice(uint8_t pin) { - this->pin = pin; + this->_pin = pin; uint16_t lightVal = getLightValue(); this->isPowerOn = lightVal < LAMP_ON_VALUE ? true : false; }; diff --git a/src/WaterDevice.cpp b/src/WaterDevice.cpp index 62bc097..18f7911 100644 --- a/src/WaterDevice.cpp +++ b/src/WaterDevice.cpp @@ -4,24 +4,16 @@ #include "WaterDevice.h" -WaterDevice::WaterDevice(uint8_t pin) { - this->pin = pin; - pinMode(pin, OUTPUT); - digitalWrite(pin, PUMP_OFF); -}; - void WaterDevice::powerOn() { - digitalWrite(PUMPPIN, PUMP_ON); - Serial.println(F("[MAIN] [I] Pump is on")); + digitalWrite(_pin, PUMP_ON); + Serial.println(F("Pump is on")); } - void WaterDevice::powerOff() { - digitalWrite(PUMPPIN, PUMP_OFF); - Serial.println(F("[MAIN] [I] Pump is off")); + digitalWrite(_pin, PUMP_OFF); + Serial.println(F("Pump is off")); } - void WaterDevice::water(timer_callback pumpOffFunc) { this->powerOn(); timer.setTimeout(WATER_DURATION * 1000, pumpOffFunc); diff --git a/src/WaterDevice.h b/src/WaterDevice.h index 7c5be7a..daee2dd 100644 --- a/src/WaterDevice.h +++ b/src/WaterDevice.h @@ -7,8 +7,6 @@ #define PUMP_ON HIGH #define PUMP_OFF LOW -#define PUMPPIN D2 - #include #include @@ -21,7 +19,11 @@ extern SimpleTimer timer; class WaterDevice: public Device { public: - WaterDevice(uint8_t pin); + WaterDevice(uint8_t pin) { + this->_pin = pin; + pinMode(pin, OUTPUT); + digitalWrite(pin, PUMP_OFF); + }; void powerOn(); diff --git a/src/main.cpp b/src/main.cpp index 338d43c..0be7f39 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -112,7 +112,11 @@ void setup() { // Serial.print(F("[MAIN] [D] Next watering scheduled on: ")); // Serial.println(waterScheduler.getNextDateTime()); - waterScheduler = Scheduler([]{waterDevice.scheduledWater(waterScheduler, []{waterDevice.powerOff();});}, WATER_SCHEDULE); + waterScheduler = Scheduler([]{ + waterDevice.scheduledWater( + waterScheduler, []{waterDevice.powerOff();} + ); + }, WATER_SCHEDULE); lampOnScheduler = Scheduler([]{lamp.scheduledPowerOn(lampOnScheduler);}, LAMP_ON_SCHEDULE); lampOffScheduler = Scheduler([]{lamp.scheduledPowerOff(lampOffScheduler);}, LAMP_OFF_SCHEDULE); fanOnScheduler = Scheduler([]{fan.scheduledPowerOn(fanOnScheduler);}, FAN_ON_SCHEDULE); diff --git a/src/scheduler.h b/src/scheduler.h index a4565da..88fdade 100644 --- a/src/scheduler.h +++ b/src/scheduler.h @@ -35,7 +35,7 @@ class Scheduler { startUnixTime = makeTime(schedule.hr, schedule.min, schedule.sec, schedule.day, schedule.mnth, schedule.year); initTimezone(location); setNextEvent(); - Serial.println("Scheduler initialized with start time" + getStartDateTime()); + Serial.println("Scheduler initialized with start time " + getStartDateTime()); }; time_t getNextUnixTime();