Added class to get timestamps from NTP server
This commit is contained in:
parent
a50ca3f4eb
commit
18b5c0bfec
@ -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());
|
||||
}
|
||||
|
||||
11
src/header.h
11
src/header.h
@ -15,6 +15,7 @@
|
||||
#include <WiFi.h>
|
||||
#include <WebServer.h>
|
||||
#include <AutoConnect.h>
|
||||
#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();
|
||||
|
||||
@ -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() {
|
||||
|
||||
@ -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();
|
||||
|
||||
41
src/ntpManager.cpp
Normal file
41
src/ntpManager.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
#include <header.h>
|
||||
|
||||
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");
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user