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/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 c30c0e0..10f2cf3 100644 --- a/src/DHTDevice.cpp +++ b/src/DHTDevice.cpp @@ -4,19 +4,6 @@ #include "DHTDevice.h" -void DHTDevice::init(uint8_t pin, uint8_t blynkTempPin) { - pinMode(pin, INPUT_PULLUP); - dht.setup(pin, DHT_MODEL); - dhtReadInterval = (dht.getMinimumSamplingPeriod()); - Serial.printf((char*)F("DHT sensor read interval is: %u\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 +15,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 e3cdc48..9e2ed4c 100644 --- a/src/DHTDevice.h +++ b/src/DHTDevice.h @@ -8,7 +8,6 @@ #include #include #include "Device.h" -#include "BlynkManager.h" #include "MetricsCollectable.h" #define DHT_MODEL DHTesp::DHT22 @@ -17,13 +16,16 @@ 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){ + 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 5bd77b4..4616f0e 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..fb62889 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; + uint8_t _pin; 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 e866878..9e99240 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); + 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 63660dd..df72197 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,12 +14,17 @@ 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(); String status(); - + private: uint16_t getLightValue(); 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..18f7911 100644 --- a/src/WaterDevice.cpp +++ b/src/WaterDevice.cpp @@ -4,62 +4,31 @@ #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) { - 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(_pin, PUMP_ON); + Serial.println(F("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(_pin, PUMP_OFF); + Serial.println(F("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..daee2dd 100644 --- a/src/WaterDevice.h +++ b/src/WaterDevice.h @@ -7,45 +7,33 @@ #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) { + this->_pin = pin; + pinMode(pin, OUTPUT); + digitalWrite(pin, PUMP_OFF); + }; void powerOn(); void powerOff(); - String status(); - - static void pumpOn(); - - static void pumpOff(); + void water(timer_callback pumpOffFunc); + + void scheduledWater(Scheduler scheduler, timer_callback pumpOffFunc); - static void water(); - - private: - uint8_t pin; - uint8_t blynkPin; + String status(); }; diff --git a/src/main.cpp b/src/main.cpp index b9f7452..0be7f39 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,18 @@ 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 +131,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 +139,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..1449bd2 100644 --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -4,36 +4,12 @@ #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); -} - 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; } @@ -41,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 9e988e4..88fdade 100644 --- a/src/scheduler.h +++ b/src/scheduler.h @@ -23,15 +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(); + Scheduler(){}; - void init(void (*function)(), schedule, String* = new String(DEFAULT_LOCATION)); + Scheduler(void (*function)(), schedule schedule, String* location = new String(DEFAULT_LOCATION)): + function(function), + 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(); /*