LittleFS json config save/load wip

This commit is contained in:
Marcus 2024-10-18 22:51:42 +02:00
parent ac10feccf7
commit b07696a1c4
3 changed files with 67 additions and 25 deletions

View file

@ -123,20 +123,16 @@ void setup() {
LFS_init();
if(existFile(CANGROW_CFG)) {
readFile(CANGROW_CFG);
Serial.printf(":: config.GrowName: %s \n", config.GrowName);
Serial.printf(":: load config.json \n");
loadConfig(config);
Serial.printf(":: config.GrowName: %s \n", config.GrowName);
Serial.print(":: config.DoG: ");
Serial.println(config.DoG);
Serial.printf(":: Before config.test: %s\n", config.test);
loadConfig();
if(!existFile(CANGROW_CFG)) {
writeFile(CANGROW_CFG, "asd");
} else {
writeFile(CANGROW_CFG, "{}");
readFile(CANGROW_CFG);
}
Serial.printf(":: After config.test: %s\n", config.test);
// read the configfile from LittleFS
/* File lfs_configfile = LittleFS.open(configfile, "r");
if(!lfs_configfile) {
@ -171,7 +167,7 @@ bool alrdySaved = false;
void loop() {
if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) {
Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json");
saveConfig(config);
saveConfig();
alrdySaved = true;
} else if( (digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == true) ) {
alrdySaved = true;

View file

@ -41,6 +41,7 @@ struct Config_WiFi {
Config_WiFi configWifi;
struct Config_System {
byte ntpOffset;
unsigned short maintenanceDuration;
@ -49,9 +50,10 @@ struct Config_System {
Config_System configSystem;
struct Config_Grow {
char GrowName[64];
unsigned short DoG;
char growName[64];
unsigned short dayOfGrow;
byte daysVeg;
byte daysBloom;
byte lightHoursVeg;
@ -73,8 +75,7 @@ Config_Grow configGrow;
struct Config {
char GrowName[64];
int DoG;
char test[16] = "123";
};
Config config;

View file

@ -148,7 +148,7 @@ void deleteFile(const char *path) {
}
// https://arduinojson.org/v7/example/config/
void loadConfig(Config& config) {
bool loadConfig() {
#ifdef ESP8266
File file = LittleFS.open(CANGROW_CFG, "r");
#endif
@ -158,23 +158,31 @@ void loadConfig(Config& config) {
File file = fs.open(CANGROW_CFG);
#endif
Serial.printf(":: [LittleFS] loading config from: %s\n", CANGROW_CFG);
JsonDocument doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, file);
if (error)
if(error) {
Serial.printf(":: [LittleFS] FAILED to load config: %s\n", CANGROW_CFG);
if (existFile(CANGROW_CFG)) {
readFile(CANGROW_CFG);
}
return false;
}
// Copy values from the JsonDocument to the Config
strlcpy(config.GrowName,
doc["GrowName"] | "CanGrow",
sizeof(config.GrowName));
config.DoG = doc["DoG"] | 3;
strlcpy(config.test,
doc["test"] | "test456",
sizeof(config.test));
// Close the file (Curiously, File's destructor doesn't close the file)
file.close();
Serial.println(":: [LittleFS] config successfully loaded.");
return true;
}
void saveConfig(Config& config) {
void saveConfig() {
#ifdef ESP8266
File file = LittleFS.open(CANGROW_CFG, "w");
#endif
@ -191,10 +199,47 @@ void saveConfig(Config& config) {
Serial.printf(":: [LittleFS] opened for writing %s\n", CANGROW_CFG);
}
/*
* Building config.json here
*/
JsonDocument doc;
doc["GrowName"] = config.GrowName;
doc["DoG"] = config.DoG + 1 ;
// * Root *
doc["test"] = config.test;
// * WiFi *
JsonObject objWifi = doc["wifi"].add<JsonObject>();
objWifi["ssid"] = configWifi.ssid;
objWifi["password"] = configWifi.password;
objWifi["ip"] = configWifi.ip;
objWifi["netmask"] = configWifi.netmask;
objWifi["gateway"] = configWifi.gateway;
objWifi["dns"] = configWifi.dns;
objWifi["dhcp"] = configWifi.dhcp;
// * System *
JsonObject objSystem = doc["system"].add<JsonObject>();
objSystem["ntpOffset"] = configSystem.ntpOffset;
objSystem["maintenanceDuration"] = configSystem.maintenanceDuration;
objSystem["esp32camIp"] = configSystem.esp32camIp;
// * Grow *
JsonObject objGrow = doc["grow"].add<JsonObject>();
objGrow["growName"] = configGrow.growName;
objGrow["dayOfGrow"] = configGrow.dayOfGrow;
objGrow["daysVeg"] = configGrow.daysVeg;
objGrow["daysBloom"] = configGrow.daysBloom;
objGrow["lightHoursVeg"] = configGrow.lightHoursVeg;
objGrow["lightHoursBloom"] = configGrow.lightHoursBloom;
objGrow["sunriseHour"] = configGrow.sunriseHour;
objGrow["sunriseMinute"] = configGrow.sunriseMinute;
objGrow["sunFade"] = configGrow.sunFade;
objGrow["sunFadeDuration"] = configGrow.sunFadeDuration;
/*
* END Building config.json here
*/
// Serialize JSON to file
if (serializeJson(doc, file) == 0) {