LittleFS Json config save and load somewhat working

This commit is contained in:
Marcus 2024-10-19 00:27:33 +02:00
parent b07696a1c4
commit 9d2faf4e4c
3 changed files with 44 additions and 6 deletions

View file

@ -124,6 +124,8 @@ void setup() {
LFS_init();
Serial.printf(":: Before config.test: %s\n", config.test);
Serial.print(":: Before configWifi.ssid: ");
Serial.println(configWifi.ssid);
loadConfig();
if(!existFile(CANGROW_CFG)) {
writeFile(CANGROW_CFG, "asd");
@ -132,7 +134,8 @@ void setup() {
}
Serial.printf(":: After config.test: %s\n", config.test);
Serial.print(":: After configWifi.ssid: ");
Serial.println(configWifi.ssid);
// read the configfile from LittleFS
/* File lfs_configfile = LittleFS.open(configfile, "r");
if(!lfs_configfile) {

View file

@ -75,7 +75,7 @@ Config_Grow configGrow;
struct Config {
char test[16] = "123";
char test[16] = "456";
};
Config config;

View file

@ -148,6 +148,7 @@ void deleteFile(const char *path) {
}
// https://arduinojson.org/v7/example/config/
// https://arduinojson.org/v7/assistant/
bool loadConfig() {
#ifdef ESP8266
File file = LittleFS.open(CANGROW_CFG, "r");
@ -170,11 +171,45 @@ bool loadConfig() {
}
return false;
}
/*
* put json values into config structs
*/
// Copy values from the JsonDocument to the Config
strlcpy(config.test,
doc["test"] | "test456",
sizeof(config.test));
// Copy strings from the JsonDocument to the Config struct as char
strlcpy(config.test, doc["test"], sizeof(config.test));
// * WiFi *
JsonObject objWifi = doc["wifi"][0];
strlcpy(configWifi.ssid, objWifi["ssid"], sizeof(configWifi.ssid));
strlcpy(configWifi.password, objWifi["password"], sizeof(configWifi.password));
strlcpy(configWifi.ip, objWifi["ip"], sizeof(configWifi.ip));
strlcpy(configWifi.netmask, objWifi["netmask"], sizeof(configWifi.netmask));
strlcpy(configWifi.gateway, objWifi["gateway"], sizeof(configWifi.gateway));
strlcpy(configWifi.dns, objWifi["dns"], sizeof(configWifi.dns));
// Copy bool / int directly into struct
configWifi.dhcp = objWifi["dhcp"];
// * System *
JsonObject objSystem = doc["system"][0];
configSystem.ntpOffset = objSystem["ntpOffset"];
configSystem.maintenanceDuration = objSystem["maintenanceDuration"];
strlcpy(configSystem.esp32camIp, objSystem["esp32camIp"], sizeof(configSystem.esp32camIp));
// * Grow *
JsonObject objGrow = doc["grow"][0];
strlcpy(configGrow.growName, objGrow["growName"], sizeof(configGrow.growName));
configGrow.dayOfGrow = objGrow["dayOfGrow"];
configGrow.daysVeg = objGrow["daysVeg"];
configGrow.daysBloom = objGrow["daysBloom"];
configGrow.lightHoursVeg = objGrow["lightHoursVeg"];
configGrow.lightHoursBloom = objGrow["lightHoursBloom"];
configGrow.sunriseHour = objGrow["sunriseHour"];
configGrow.sunriseMinute = objGrow["sunriseMinute"];
configGrow.sunFade = objGrow["sunFade"];
configGrow.sunFadeDuration = objGrow["sunFadeDuration"];
// Close the file (Curiously, File's destructor doesn't close the file)
file.close();