Compare commits

...

2 commits

Author SHA1 Message Date
1d55f36387 basic json config save and load from and to LittleFS works 2024-10-18 02:45:34 +02:00
f347a375ce update geany config 2024-10-18 01:12:02 +02:00
5 changed files with 101 additions and 9 deletions

View file

@ -29,7 +29,7 @@ long_line_column=72
[files]
current_page=0
FILE_NAME_0=0;Sh;0;EUTF-8;0;1;0;.%2Fcangrow.sh;0;2
FILE_NAME_0=493;Sh;0;EUTF-8;0;1;0;.%2Fcangrow.sh;0;2
FILE_NAME_1=0;Arduino;0;EUTF-8;0;1;0;.%2FCanGrow.ino;0;2
FILE_NAME_2=0;C++;0;EUTF-8;0;1;0;.%2Finclude%2FCanGrow.h;0;2
FILE_NAME_3=0;C++;0;EUTF-8;0;1;0;.%2Finclude%2FCanGrow_Core.h;0;2
@ -43,7 +43,7 @@ FILE_NAME_8=0;C++;0;EUTF-8;0;1;0;.%2Finclude%2FCanGrow_Version.h;0;2
C++FT_00_LB=_Compile
C++FT_00_CM=cd .. ; ./cangrow.sh build
C++FT_00_WD=
filetypes=C++;Arduino;
filetypes=C++;Arduino;Sh;
ArduinoFT_00_LB=_Build
ArduinoFT_00_CM=./cangrow.sh build
ArduinoFT_00_WD=
@ -53,6 +53,12 @@ ArduinoFT_01_WD=
C++FT_01_LB=_Build & Upload
C++FT_01_CM=cd .. ; ./cangrow.sh upload
C++FT_01_WD=
ShFT_00_LB=Build
ShFT_00_CM=./cangrow.sh build
ShFT_00_WD=
ShFT_01_LB=Build & Upload
ShFT_01_CM=./cangrow.sh upload
ShFT_01_WD=
[VTE]
last_dir=/home/marcus/Dokumente/git/hl.la10cy.net-website

View file

@ -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() {
*/
}
void loop() {
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;
}
}

View file

@ -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')"

View file

@ -29,4 +29,9 @@
#include "CanGrow_Version.h"
#define CANGROW_CFG "/config.json"
struct Config {
char GrowName[64];
int DoG;
};
Config config;

View file

@ -27,6 +27,8 @@
*
*/
#define CANGROW_CFG "/config.json"
// LittleFS auto format
#define FORMAT_LITTLEFS_IF_FAILED true
@ -96,7 +98,10 @@ void readFile(const char *path) {
}
Serial.printf(":: [LittleFS] file content: %s\n", path);
Serial.println("----");
while (file.available()) { Serial.write(file.read()); }
Serial.println("\n----");
file.close();
}
@ -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