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(); LFS_init();
Serial.printf(":: Before config.test: %s\n", config.test); Serial.printf(":: Before config.test: %s\n", config.test);
Serial.print(":: Before configWifi.ssid: ");
Serial.println(configWifi.ssid);
loadConfig(); loadConfig();
if(!existFile(CANGROW_CFG)) { if(!existFile(CANGROW_CFG)) {
writeFile(CANGROW_CFG, "asd"); writeFile(CANGROW_CFG, "asd");
@ -132,7 +134,8 @@ void setup() {
} }
Serial.printf(":: After config.test: %s\n", config.test); Serial.printf(":: After config.test: %s\n", config.test);
Serial.print(":: After configWifi.ssid: ");
Serial.println(configWifi.ssid);
// 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) {

View file

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

View file

@ -148,6 +148,7 @@ void deleteFile(const char *path) {
} }
// https://arduinojson.org/v7/example/config/ // https://arduinojson.org/v7/example/config/
// https://arduinojson.org/v7/assistant/
bool loadConfig() { bool loadConfig() {
#ifdef ESP8266 #ifdef ESP8266
File file = LittleFS.open(CANGROW_CFG, "r"); File file = LittleFS.open(CANGROW_CFG, "r");
@ -170,11 +171,45 @@ bool loadConfig() {
} }
return false; return false;
} }
/*
* put json values into config structs
*/
// Copy values from the JsonDocument to the Config // Copy strings from the JsonDocument to the Config struct as char
strlcpy(config.test, strlcpy(config.test, doc["test"], sizeof(config.test));
doc["test"] | "test456",
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) // Close the file (Curiously, File's destructor doesn't close the file)
file.close(); file.close();