basic json config save and load from and to LittleFS works
This commit is contained in:
parent
f347a375ce
commit
1d55f36387
4 changed files with 93 additions and 7 deletions
|
@ -26,7 +26,7 @@
|
|||
* THE SOFTWARE.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Libraries include
|
||||
|
@ -125,6 +125,14 @@ 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);
|
||||
|
||||
|
||||
} else {
|
||||
writeFile(CANGROW_CFG, "{}");
|
||||
}
|
||||
|
@ -158,6 +166,16 @@ void setup() {
|
|||
*/
|
||||
}
|
||||
|
||||
bool alrdySaved = false;
|
||||
|
||||
void loop() {
|
||||
|
||||
if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) {
|
||||
Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json");
|
||||
saveConfig(config);
|
||||
alrdySaved = true;
|
||||
} else if( (digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == true) ) {
|
||||
alrdySaved = true;
|
||||
} else {
|
||||
alrdySaved = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
test -z $TTY && TTY="/dev/ttyUSB0"
|
||||
test -z $IP && IP="192.168.4.20"
|
||||
test -z $VER && VER="0.2-dev"
|
||||
test -z $BOARD && BOARD="esp8266:esp8266:d1_mini_clone"
|
||||
#test -z $BOARD && BOARD="esp32:esp32:d1_mini32"
|
||||
#test -z $BOARD && BOARD="esp8266:esp8266:d1_mini_clone"
|
||||
test -z $BOARD && BOARD="esp32:esp32:d1_mini32"
|
||||
|
||||
BUILD="$(git rev-parse --short HEAD)-$(echo $BOARD | cut -d : -f1)-$(date '+%Y%m%d%H%M%S')"
|
||||
|
||||
|
|
|
@ -29,4 +29,9 @@
|
|||
|
||||
#include "CanGrow_Version.h"
|
||||
|
||||
#define CANGROW_CFG "/config.json"
|
||||
struct Config {
|
||||
char GrowName[64];
|
||||
int DoG;
|
||||
};
|
||||
|
||||
Config config;
|
||||
|
|
|
@ -27,6 +27,8 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#define CANGROW_CFG "/config.json"
|
||||
|
||||
// LittleFS auto format
|
||||
#define FORMAT_LITTLEFS_IF_FAILED true
|
||||
|
||||
|
@ -96,8 +98,11 @@ void readFile(const char *path) {
|
|||
}
|
||||
|
||||
Serial.printf(":: [LittleFS] file content: %s\n", path);
|
||||
Serial.println("----");
|
||||
while (file.available()) { Serial.write(file.read()); }
|
||||
file.close();
|
||||
Serial.println("\n----");
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
void writeFile(const char *path, const char *message) {
|
||||
|
@ -119,7 +124,7 @@ void writeFile(const char *path, const char *message) {
|
|||
} else {
|
||||
Serial.printf(":: [LittleFS] writing file FAILED: %s\n", path);
|
||||
}
|
||||
delay(2000); // Make sure the CREATE and LASTWRITE times are different
|
||||
//delay(2000); // Make sure the CREATE and LASTWRITE times are different
|
||||
file.close();
|
||||
}
|
||||
|
||||
|
@ -142,6 +147,64 @@ void deleteFile(const char *path) {
|
|||
}
|
||||
}
|
||||
|
||||
// https://arduinojson.org/v7/example/config/
|
||||
void loadConfig(Config& config) {
|
||||
#ifdef ESP8266
|
||||
File file = LittleFS.open(CANGROW_CFG, "r");
|
||||
#endif
|
||||
|
||||
#ifdef ESP32
|
||||
fs::FS &fs = LittleFS;
|
||||
File file = fs.open(CANGROW_CFG);
|
||||
#endif
|
||||
|
||||
JsonDocument doc;
|
||||
// Deserialize the JSON document
|
||||
DeserializationError error = deserializeJson(doc, file);
|
||||
if (error)
|
||||
Serial.printf(":: [LittleFS] FAILED to load config: %s\n", CANGROW_CFG);
|
||||
|
||||
// Copy values from the JsonDocument to the Config
|
||||
strlcpy(config.GrowName,
|
||||
doc["GrowName"] | "CanGrow",
|
||||
sizeof(config.GrowName));
|
||||
config.DoG = doc["DoG"] | 3;
|
||||
|
||||
// Close the file (Curiously, File's destructor doesn't close the file)
|
||||
file.close();
|
||||
}
|
||||
|
||||
void saveConfig(Config& config) {
|
||||
#ifdef ESP8266
|
||||
File file = LittleFS.open(CANGROW_CFG, "w");
|
||||
#endif
|
||||
|
||||
#ifdef ESP32
|
||||
fs::FS &fs = LittleFS;
|
||||
File file = fs.open(CANGROW_CFG, FILE_WRITE);
|
||||
#endif
|
||||
|
||||
if (!file) {
|
||||
Serial.printf(":: [LittleFS] FAILED to open configfile for writing: %s\n", CANGROW_CFG);
|
||||
return;
|
||||
} else {
|
||||
Serial.printf(":: [LittleFS] opened for writing %s\n", CANGROW_CFG);
|
||||
}
|
||||
|
||||
JsonDocument doc;
|
||||
|
||||
doc["GrowName"] = config.GrowName;
|
||||
doc["DoG"] = config.DoG + 1 ;
|
||||
|
||||
// Serialize JSON to file
|
||||
if (serializeJson(doc, file) == 0) {
|
||||
Serial.printf(":: [LittleFS] FAILED to write configfile: %s\n", CANGROW_CFG);
|
||||
} else {
|
||||
Serial.printf(":: [LittleFS] successfully written %s\n", CANGROW_CFG);
|
||||
}
|
||||
|
||||
file.close();
|
||||
}
|
||||
|
||||
///*
|
||||
//* ESP8266 functions
|
||||
|
|
Loading…
Reference in a new issue