From 9d2faf4e4c3fb2961964200b511a2937f7730bcd Mon Sep 17 00:00:00 2001 From: Marcus Date: Sat, 19 Oct 2024 00:27:33 +0200 Subject: [PATCH] LittleFS Json config save and load somewhat working --- Arduino/CanGrow/CanGrow.ino | 5 ++- Arduino/CanGrow/include/CanGrow.h | 2 +- Arduino/CanGrow/include/CanGrow_LittleFS.h | 43 ++++++++++++++++++++-- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index a7b175a..713b0f0 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -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) { diff --git a/Arduino/CanGrow/include/CanGrow.h b/Arduino/CanGrow/include/CanGrow.h index d4ac8e4..9b0ee71 100644 --- a/Arduino/CanGrow/include/CanGrow.h +++ b/Arduino/CanGrow/include/CanGrow.h @@ -75,7 +75,7 @@ Config_Grow configGrow; struct Config { - char test[16] = "123"; + char test[16] = "456"; }; Config config; diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index e3c84fe..6dfeb76 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -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();