LittleFS json config save/load wip
This commit is contained in:
parent
ac10feccf7
commit
b07696a1c4
3 changed files with 67 additions and 25 deletions
|
@ -123,20 +123,16 @@ void setup() {
|
||||||
|
|
||||||
|
|
||||||
LFS_init();
|
LFS_init();
|
||||||
if(existFile(CANGROW_CFG)) {
|
Serial.printf(":: Before config.test: %s\n", config.test);
|
||||||
readFile(CANGROW_CFG);
|
loadConfig();
|
||||||
Serial.printf(":: config.GrowName: %s \n", config.GrowName);
|
if(!existFile(CANGROW_CFG)) {
|
||||||
Serial.printf(":: load config.json \n");
|
writeFile(CANGROW_CFG, "asd");
|
||||||
loadConfig(config);
|
|
||||||
Serial.printf(":: config.GrowName: %s \n", config.GrowName);
|
|
||||||
Serial.print(":: config.DoG: ");
|
|
||||||
Serial.println(config.DoG);
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
writeFile(CANGROW_CFG, "{}");
|
readFile(CANGROW_CFG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Serial.printf(":: After config.test: %s\n", config.test);
|
||||||
|
|
||||||
// read the configfile from LittleFS
|
// read the configfile from LittleFS
|
||||||
/* File lfs_configfile = LittleFS.open(configfile, "r");
|
/* File lfs_configfile = LittleFS.open(configfile, "r");
|
||||||
if(!lfs_configfile) {
|
if(!lfs_configfile) {
|
||||||
|
@ -171,7 +167,7 @@ bool alrdySaved = false;
|
||||||
void loop() {
|
void loop() {
|
||||||
if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) {
|
if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) {
|
||||||
Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json");
|
Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json");
|
||||||
saveConfig(config);
|
saveConfig();
|
||||||
alrdySaved = true;
|
alrdySaved = true;
|
||||||
} else if( (digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == true) ) {
|
} else if( (digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == true) ) {
|
||||||
alrdySaved = true;
|
alrdySaved = true;
|
||||||
|
|
|
@ -41,6 +41,7 @@ struct Config_WiFi {
|
||||||
|
|
||||||
Config_WiFi configWifi;
|
Config_WiFi configWifi;
|
||||||
|
|
||||||
|
|
||||||
struct Config_System {
|
struct Config_System {
|
||||||
byte ntpOffset;
|
byte ntpOffset;
|
||||||
unsigned short maintenanceDuration;
|
unsigned short maintenanceDuration;
|
||||||
|
@ -49,9 +50,10 @@ struct Config_System {
|
||||||
|
|
||||||
Config_System configSystem;
|
Config_System configSystem;
|
||||||
|
|
||||||
|
|
||||||
struct Config_Grow {
|
struct Config_Grow {
|
||||||
char GrowName[64];
|
char growName[64];
|
||||||
unsigned short DoG;
|
unsigned short dayOfGrow;
|
||||||
byte daysVeg;
|
byte daysVeg;
|
||||||
byte daysBloom;
|
byte daysBloom;
|
||||||
byte lightHoursVeg;
|
byte lightHoursVeg;
|
||||||
|
@ -73,8 +75,7 @@ Config_Grow configGrow;
|
||||||
|
|
||||||
|
|
||||||
struct Config {
|
struct Config {
|
||||||
char GrowName[64];
|
char test[16] = "123";
|
||||||
int DoG;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Config config;
|
Config config;
|
||||||
|
|
|
@ -148,7 +148,7 @@ void deleteFile(const char *path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://arduinojson.org/v7/example/config/
|
// https://arduinojson.org/v7/example/config/
|
||||||
void loadConfig(Config& config) {
|
bool loadConfig() {
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
File file = LittleFS.open(CANGROW_CFG, "r");
|
File file = LittleFS.open(CANGROW_CFG, "r");
|
||||||
#endif
|
#endif
|
||||||
|
@ -158,23 +158,31 @@ void loadConfig(Config& config) {
|
||||||
File file = fs.open(CANGROW_CFG);
|
File file = fs.open(CANGROW_CFG);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Serial.printf(":: [LittleFS] loading config from: %s\n", CANGROW_CFG);
|
||||||
|
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
// Deserialize the JSON document
|
// Deserialize the JSON document
|
||||||
DeserializationError error = deserializeJson(doc, file);
|
DeserializationError error = deserializeJson(doc, file);
|
||||||
if (error)
|
if(error) {
|
||||||
Serial.printf(":: [LittleFS] FAILED to load config: %s\n", CANGROW_CFG);
|
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
|
// Copy values from the JsonDocument to the Config
|
||||||
strlcpy(config.GrowName,
|
strlcpy(config.test,
|
||||||
doc["GrowName"] | "CanGrow",
|
doc["test"] | "test456",
|
||||||
sizeof(config.GrowName));
|
sizeof(config.test));
|
||||||
config.DoG = doc["DoG"] | 3;
|
|
||||||
|
|
||||||
// Close the file (Curiously, File's destructor doesn't close the file)
|
// Close the file (Curiously, File's destructor doesn't close the file)
|
||||||
file.close();
|
file.close();
|
||||||
|
Serial.println(":: [LittleFS] config successfully loaded.");
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveConfig(Config& config) {
|
void saveConfig() {
|
||||||
#ifdef ESP8266
|
#ifdef ESP8266
|
||||||
File file = LittleFS.open(CANGROW_CFG, "w");
|
File file = LittleFS.open(CANGROW_CFG, "w");
|
||||||
#endif
|
#endif
|
||||||
|
@ -191,10 +199,47 @@ void saveConfig(Config& config) {
|
||||||
Serial.printf(":: [LittleFS] opened for writing %s\n", CANGROW_CFG);
|
Serial.printf(":: [LittleFS] opened for writing %s\n", CANGROW_CFG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Building config.json here
|
||||||
|
*/
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
|
|
||||||
doc["GrowName"] = config.GrowName;
|
// * Root *
|
||||||
doc["DoG"] = config.DoG + 1 ;
|
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
|
// Serialize JSON to file
|
||||||
if (serializeJson(doc, file) == 0) {
|
if (serializeJson(doc, file) == 0) {
|
||||||
|
|
Loading…
Reference in a new issue