diff --git a/src/connections.cpp b/src/connections.cpp index a2cea3d..bb0dca5 100644 --- a/src/connections.cpp +++ b/src/connections.cpp @@ -121,7 +121,7 @@ Serial.println("receiving light treshold..."); if (err == DeserializationError::Ok) { int nm = doc["nm"]; int minLX = doc["minLX"]; - setLightningProperties(nm, minLX); + setLightProperties(nm, minLX); } else { Serial.println(err.c_str()); } diff --git a/src/header.h b/src/header.h index c9ec72a..df7db75 100644 --- a/src/header.h +++ b/src/header.h @@ -15,6 +15,7 @@ #include #include #include +#include "time.h" // fix for core panic during wifi initialization #define configMINIMAL_STACK_SIZE 4096 @@ -87,12 +88,20 @@ extern void setValueNM(int NM); extern void getColorBasedOnValueNM(int valueNM); extern void triggerLight(); +// NTP Timestamps +void printTimestamp(); +void setupNTP(); +bool checkForDay(); +int getCurrentHour(); + // sensors void readSensors(); void toggleValve(); void setSoilProperties(int FC, int PWP, int SAT); void setupStore(); -void setLightningProperties(int NM, int minLX); +void setLightProperties(int NM, int minLX); +void restoreLightProbs(); +void persistLightProps(int NM, int minLX); void takeSemaphore(); void releaseSemaphore(); diff --git a/src/lightChecker.cpp b/src/lightChecker.cpp index 923189c..5ecf944 100644 --- a/src/lightChecker.cpp +++ b/src/lightChecker.cpp @@ -31,7 +31,6 @@ void setupPWM() { void setValueNM(int NM) { getColorBasedOnValueNM(NM); - // TODO: add persistenz for colorCounter } void getColorBasedOnValueNM(int valueNM) { @@ -75,6 +74,7 @@ bool activateLight() { } void lightTask(void *parameter) { + takeSemaphore(); unsigned long lightTimeoutTimer = millis(); if (lightActive == false) { lightActive = activateLight(); @@ -82,6 +82,8 @@ void lightTask(void *parameter) { } lightActive = shutdownLight(); } + releaseSemaphore(); + vTaskDelete(NULL); } void triggerLight() { diff --git a/src/main.cpp b/src/main.cpp index 56f2f2c..9b624f6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,6 +29,7 @@ void setup() { setupPWM(); setupTemperatureSensor(); setupCapacitiveSoilMoistureSensor(); + setupNTP(); // esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000); Serial.println("Setup complete..."); Serial.println(); diff --git a/src/ntpManager.cpp b/src/ntpManager.cpp new file mode 100644 index 0000000..ef5be48 --- /dev/null +++ b/src/ntpManager.cpp @@ -0,0 +1,41 @@ +#include + +const char* ntpServer = "pool.ntp.org"; +const long gmtOffset_sec = 3600; +const int daylightOffset_sec = 3600; + +// start and end hour of the date +const int dayStart = 8; +const int dayEnd = 20; +struct tm timeinfo; + +void setupNTP() { + if (WiFi.isConnected()) { + Serial.println("WiFi OK. Getting Timestamp"); + configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); + printTimestamp(); + } +} + +bool checkForDay() { + printTimestamp(); + if((getCurrentHour() > dayStart) && (getCurrentHour() < dayEnd)) { + return true; + } + else { + return false; + } +} + +int getCurrentHour() { + int currentHour = timeinfo.tm_hour; + return currentHour; +} + +void printTimestamp() { + if(!getLocalTime(&timeinfo)){ + Serial.println("Failed to obtain time"); + return; + } + Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); +} \ No newline at end of file diff --git a/src/peripherals.cpp b/src/peripherals.cpp index 1a47872..730aeb9 100644 --- a/src/peripherals.cpp +++ b/src/peripherals.cpp @@ -29,7 +29,7 @@ void readSensors() { Serial.print(lxValue); Serial.println(" lx"); doc["brightness"] = lxValue; - if(lxValue < minimumLightValueLX) { + if((lxValue < minimumLightValueLX) && checkForDay()) { triggerLight(); } //sprintf(buffer, "%f", lxValue); @@ -98,7 +98,7 @@ void toggleValve() { xTaskCreate( valveTask, /* Task function. */ "valveTask", /* String with name of task. */ - 2048, /* Stack size in bytes. */ + 2048, /* Stack size in bytes. */ NULL, /* Parameter passed as input of the task */ 1, /* Priority of the task. */ NULL); /* Task handle. */ @@ -142,6 +142,7 @@ void restoreSoilProps() { void setupStore() { NVS.begin("store"); restoreSoilProps(); + restoreLightProbs(); } void setSoilProperties(int FC, int PWP, int SAT) { @@ -157,11 +158,38 @@ void setSoilProperties(int FC, int PWP, int SAT) { Serial.println(soilSaturation); } -void setLightningProperties(int NM, int minLX) { - setValueNM(NM); - minimumLightValueLX = minLX; - Serial.print("new minimum Light Value LX: "); - Serial.println(minimumLightValueLX); - // TODO: add Persistenz for minLX +void persistLightProps(int NM, int minLX) { + takeSemaphore(); + Serial.println("persistSoilProps"); + bool n = NVS.setInt("nanoMeter", NM); + bool m = NVS.setInt("minimumLux", minLX); + if (n && m) { + Serial.println("Light properties sucessfully stored."); + NVS.commit(); + } + else { + Serial.println("error occured while trying to persist soil properties"); + } + releaseSemaphore(); } +void restoreLightProbs() { + Serial.println("restoreLightProps"); + int nm = NVS.getInt("nanoMeter"); + int minLX = NVS.getInt("minimumLux"); + if (nm != 0) { + setValueNM(nm); + } + if (minLX != 0) { + minimumLightValueLX = minLX; + } + Serial.print(minimumLightValueLX); +} + +void setLightProperties(int NM, int minLX) { + setValueNM(NM); + minimumLightValueLX = minLX; + persistLightProps(NM, minLX); + Serial.print("new minimum Light Value LX: "); + Serial.println(minimumLightValueLX); +}