#include #include #include #include #include // Fieldcapacity of the Ground in Percentage: Standard is Humus int fieldCapacity = 44; // PWP of the Ground in Percentage: Standard is Humus int permanentWiltingPoint = 25; // Ground completely saturated by (Percentage): Standard is Humus int soilSaturation = 69; // Minimum light value before light turns on int minimumLightValueLX = 50; // Switches for automatic light and irrigation control bool automaticLight = false; bool automaticIrrigation = false; // 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); } // Method to persist save the given lightning properties to NVS 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"); } } // Method to restore light properties from Non-Volatile Storage (NVS) 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); } // Method to set given light properties 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); } } // Returns the device ID String getDeviceID() { return DEVICE_ID; } const char* getDeviceIDcharArr() { return DEVICE_ID.c_str(); }