diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 183e6f4..25d297e 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -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; + } } diff --git a/Arduino/CanGrow/cangrow.sh b/Arduino/CanGrow/cangrow.sh index fe2f025..8922d6e 100755 --- a/Arduino/CanGrow/cangrow.sh +++ b/Arduino/CanGrow/cangrow.sh @@ -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')" diff --git a/Arduino/CanGrow/include/CanGrow.h b/Arduino/CanGrow/include/CanGrow.h index 2702118..33fdb51 100644 --- a/Arduino/CanGrow/include/CanGrow.h +++ b/Arduino/CanGrow/include/CanGrow.h @@ -29,4 +29,9 @@ #include "CanGrow_Version.h" -#define CANGROW_CFG "/config.json" +struct Config { + char GrowName[64]; + int DoG; +}; + +Config config; diff --git a/Arduino/CanGrow/include/CanGrow_LittleFS.h b/Arduino/CanGrow/include/CanGrow_LittleFS.h index 2ffb915..92d0a21 100644 --- a/Arduino/CanGrow/include/CanGrow_LittleFS.h +++ b/Arduino/CanGrow/include/CanGrow_LittleFS.h @@ -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