167 lines
4.5 KiB
C++
167 lines
4.5 KiB
C++
#include <ArduinoNvs.h>
|
|
#include <ESPRandom.h>
|
|
#include <common.h>
|
|
#include <lightChecker.h>
|
|
#include <store.h>
|
|
|
|
// Feldkapazität des Bodens in Prozent: Standard ist Humus
|
|
int fieldCapacity = 44;
|
|
// PWP des Bodens in Prozent: Standard ist Humus
|
|
int permanentWiltingPoint = 25;
|
|
// Boden vollständig gesättigt bei (Prozent): Standard ist Humus
|
|
int soilSaturation = 69;
|
|
// Helligkeitswert der mindestens vorhanden sein muss
|
|
int minimumLightValueLX = 50;
|
|
// switches for automatic light and irrigation control
|
|
bool automaticLight = true;
|
|
bool automaticIrrigation = true;
|
|
// make sure device irrigates until fieldcapacity is reached
|
|
bool irrigateUntilFC = false;
|
|
|
|
void persistSoilProps(int FC, int PWP, int SAT) {
|
|
Serial.println("persistSoilProps");
|
|
bool f = NVS.setInt("fieldCapacity", FC);
|
|
bool p = NVS.setInt("permanentWilt", PWP);
|
|
bool s = NVS.setInt("soilSaturation", SAT);
|
|
if (f && p && s) {
|
|
Serial.println("Soil properties sucessfully stored.");
|
|
NVS.commit();
|
|
}
|
|
else {
|
|
Serial.println("error occured while trying to persist soil properties");
|
|
}
|
|
}
|
|
|
|
void restoreSoilProps() {
|
|
Serial.println("restoreSoilProps");
|
|
int fc = NVS.getInt("fieldCapacity");
|
|
int pwp = NVS.getInt("permanentWilt");
|
|
int sat = NVS.getInt("soilSaturation");
|
|
if (fc != 0) {
|
|
fieldCapacity = fc;
|
|
}
|
|
if (pwp != 0) {
|
|
permanentWiltingPoint = pwp;
|
|
}
|
|
if (sat != 0) {
|
|
soilSaturation = sat;
|
|
}
|
|
Serial.print("fieldCapacity: ");
|
|
Serial.println(fieldCapacity);
|
|
Serial.print("permanentWiltingPoint: ");
|
|
Serial.println(permanentWiltingPoint);
|
|
Serial.print("soilSaturation: ");
|
|
Serial.println(soilSaturation);
|
|
}
|
|
|
|
void setupStore() {
|
|
NVS.begin("store");
|
|
initDeviceID();
|
|
restoreSoilProps();
|
|
restoreLightProps();
|
|
restoreAutoProps();
|
|
}
|
|
|
|
void setSoilProperties(int FC, int PWP, int SAT) {
|
|
fieldCapacity = FC;
|
|
permanentWiltingPoint = PWP;
|
|
soilSaturation = SAT;
|
|
persistSoilProps(FC, PWP, SAT);
|
|
Serial.print("new fieldCapacity: ");
|
|
Serial.println(fieldCapacity);
|
|
Serial.print("new permanentWiltingPoint: ");
|
|
Serial.println(permanentWiltingPoint);
|
|
Serial.print("new soilSaturation: ");
|
|
Serial.println(soilSaturation);
|
|
}
|
|
|
|
void persistLightProps(int NM, int minLX) {
|
|
Serial.println("persistLightProps");
|
|
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 light properties");
|
|
}
|
|
}
|
|
|
|
void restoreLightProps() {
|
|
Serial.println("restoreLightProps");
|
|
int nm = NVS.getInt("nanoMeter");
|
|
int minLX = NVS.getInt("minimumLux");
|
|
if (nm != 0) {
|
|
setValueNM(nm);
|
|
}
|
|
if (minLX != 0) {
|
|
minimumLightValueLX = minLX;
|
|
}
|
|
Serial.println(minimumLightValueLX);
|
|
}
|
|
|
|
void setLightProperties(int NM, int minLX) {
|
|
setValueNM(NM);
|
|
minimumLightValueLX = minLX;
|
|
persistLightProps(NM, minLX);
|
|
Serial.print("new minimum Light Value LX: ");
|
|
Serial.println(minimumLightValueLX);
|
|
}
|
|
|
|
void persistAutoProps(bool light, bool irrigation) {
|
|
Serial.println("persistAutoProps");
|
|
// saved in NVS as Integer: 1 = true, 2 = false, 0 = not persisted, use standard settings
|
|
bool n = NVS.setInt("automaticLight", (light ? 1 : 2));
|
|
bool m = NVS.setInt("automaticIrrigation", (irrigation ? 1 : 2));
|
|
if (n && m) {
|
|
NVS.commit();
|
|
Serial.println("Auto properties sucessfully stored.");
|
|
} else {
|
|
Serial.println("error occured while trying to persist auto properties");
|
|
}
|
|
}
|
|
|
|
void restoreAutoProps() {
|
|
Serial.println("restoreLightProps");
|
|
int li = NVS.getInt("automaticLight");
|
|
int ir = NVS.getInt("automaticIrrigation");
|
|
automaticLight = (bool) (li != 2);
|
|
automaticIrrigation = (bool) (ir != 2);
|
|
Serial.println(minimumLightValueLX);
|
|
}
|
|
|
|
void setAutoProperties(bool light, bool irrigation) {
|
|
automaticLight = light;
|
|
automaticIrrigation = irrigation;
|
|
persistAutoProps(light, irrigation);
|
|
Serial.print("new auto settings: ");
|
|
Serial.println(automaticLight);
|
|
Serial.println(automaticIrrigation);
|
|
}
|
|
|
|
String DEVICE_ID = "";
|
|
|
|
void initDeviceID() {
|
|
DEVICE_ID = NVS.getString("UUID");
|
|
if (!DEVICE_ID.isEmpty()) {
|
|
Serial.print("Fetched Device ID from NVS: ");
|
|
Serial.println(DEVICE_ID);
|
|
} else {
|
|
uint8_t uuid[16];
|
|
ESPRandom::uuid(uuid);
|
|
DEVICE_ID = ESPRandom::uuidToString(uuid);
|
|
NVS.setString("UUID", DEVICE_ID);
|
|
NVS.commit();
|
|
Serial.print("New Device ID: ");
|
|
Serial.println(DEVICE_ID);
|
|
}
|
|
}
|
|
|
|
String getDeviceID() {
|
|
return DEVICE_ID;
|
|
}
|
|
|
|
const char* getDeviceIDcharArr() {
|
|
return DEVICE_ID.c_str();
|
|
} |