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) {
|
if (err == DeserializationError::Ok) {
|
||||||
int nm = doc["nm"];
|
int nm = doc["nm"];
|
||||||
int minLX = doc["minLX"];
|
int minLX = doc["minLX"];
|
||||||
setLightningProperties(nm, minLX);
|
setLightProperties(nm, minLX);
|
||||||
} else {
|
} else {
|
||||||
Serial.println(err.c_str());
|
Serial.println(err.c_str());
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/header.h
11
src/header.h
@ -15,6 +15,7 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <WebServer.h>
|
#include <WebServer.h>
|
||||||
#include <AutoConnect.h>
|
#include <AutoConnect.h>
|
||||||
|
#include "time.h"
|
||||||
|
|
||||||
// fix for core panic during wifi initialization
|
// fix for core panic during wifi initialization
|
||||||
#define configMINIMAL_STACK_SIZE 4096
|
#define configMINIMAL_STACK_SIZE 4096
|
||||||
@ -87,12 +88,20 @@ extern void setValueNM(int NM);
|
|||||||
extern void getColorBasedOnValueNM(int valueNM);
|
extern void getColorBasedOnValueNM(int valueNM);
|
||||||
extern void triggerLight();
|
extern void triggerLight();
|
||||||
|
|
||||||
|
// NTP Timestamps
|
||||||
|
void printTimestamp();
|
||||||
|
void setupNTP();
|
||||||
|
bool checkForDay();
|
||||||
|
int getCurrentHour();
|
||||||
|
|
||||||
// sensors
|
// sensors
|
||||||
void readSensors();
|
void readSensors();
|
||||||
void toggleValve();
|
void toggleValve();
|
||||||
void setSoilProperties(int FC, int PWP, int SAT);
|
void setSoilProperties(int FC, int PWP, int SAT);
|
||||||
void setupStore();
|
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 takeSemaphore();
|
||||||
void releaseSemaphore();
|
void releaseSemaphore();
|
||||||
|
|||||||
@ -31,7 +31,6 @@ void setupPWM() {
|
|||||||
|
|
||||||
void setValueNM(int NM) {
|
void setValueNM(int NM) {
|
||||||
getColorBasedOnValueNM(NM);
|
getColorBasedOnValueNM(NM);
|
||||||
// TODO: add persistenz for colorCounter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void getColorBasedOnValueNM(int valueNM) {
|
void getColorBasedOnValueNM(int valueNM) {
|
||||||
@ -75,6 +74,7 @@ bool activateLight() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void lightTask(void *parameter) {
|
void lightTask(void *parameter) {
|
||||||
|
takeSemaphore();
|
||||||
unsigned long lightTimeoutTimer = millis();
|
unsigned long lightTimeoutTimer = millis();
|
||||||
if (lightActive == false) {
|
if (lightActive == false) {
|
||||||
lightActive = activateLight();
|
lightActive = activateLight();
|
||||||
@ -82,6 +82,8 @@ void lightTask(void *parameter) {
|
|||||||
}
|
}
|
||||||
lightActive = shutdownLight();
|
lightActive = shutdownLight();
|
||||||
}
|
}
|
||||||
|
releaseSemaphore();
|
||||||
|
vTaskDelete(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void triggerLight() {
|
void triggerLight() {
|
||||||
|
|||||||
@ -29,6 +29,7 @@ void setup() {
|
|||||||
setupPWM();
|
setupPWM();
|
||||||
setupTemperatureSensor();
|
setupTemperatureSensor();
|
||||||
setupCapacitiveSoilMoistureSensor();
|
setupCapacitiveSoilMoistureSensor();
|
||||||
|
setupNTP();
|
||||||
// esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000);
|
// esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000);
|
||||||
Serial.println("Setup complete...");
|
Serial.println("Setup complete...");
|
||||||
Serial.println();
|
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.print(lxValue);
|
||||||
Serial.println(" lx");
|
Serial.println(" lx");
|
||||||
doc["brightness"] = lxValue;
|
doc["brightness"] = lxValue;
|
||||||
if(lxValue < minimumLightValueLX) {
|
if((lxValue < minimumLightValueLX) && checkForDay()) {
|
||||||
triggerLight();
|
triggerLight();
|
||||||
}
|
}
|
||||||
//sprintf(buffer, "%f", lxValue);
|
//sprintf(buffer, "%f", lxValue);
|
||||||
@ -98,7 +98,7 @@ void toggleValve() {
|
|||||||
xTaskCreate(
|
xTaskCreate(
|
||||||
valveTask, /* Task function. */
|
valveTask, /* Task function. */
|
||||||
"valveTask", /* String with name of task. */
|
"valveTask", /* String with name of task. */
|
||||||
2048, /* Stack size in bytes. */
|
2048, /* Stack size in bytes. */
|
||||||
NULL, /* Parameter passed as input of the task */
|
NULL, /* Parameter passed as input of the task */
|
||||||
1, /* Priority of the task. */
|
1, /* Priority of the task. */
|
||||||
NULL); /* Task handle. */
|
NULL); /* Task handle. */
|
||||||
@ -142,6 +142,7 @@ void restoreSoilProps() {
|
|||||||
void setupStore() {
|
void setupStore() {
|
||||||
NVS.begin("store");
|
NVS.begin("store");
|
||||||
restoreSoilProps();
|
restoreSoilProps();
|
||||||
|
restoreLightProbs();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSoilProperties(int FC, int PWP, int SAT) {
|
void setSoilProperties(int FC, int PWP, int SAT) {
|
||||||
@ -157,11 +158,38 @@ void setSoilProperties(int FC, int PWP, int SAT) {
|
|||||||
Serial.println(soilSaturation);
|
Serial.println(soilSaturation);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setLightningProperties(int NM, int minLX) {
|
void persistLightProps(int NM, int minLX) {
|
||||||
setValueNM(NM);
|
takeSemaphore();
|
||||||
minimumLightValueLX = minLX;
|
Serial.println("persistSoilProps");
|
||||||
Serial.print("new minimum Light Value LX: ");
|
bool n = NVS.setInt("nanoMeter", NM);
|
||||||
Serial.println(minimumLightValueLX);
|
bool m = NVS.setInt("minimumLux", minLX);
|
||||||
// TODO: add Persistenz for 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