Skip to content

Commit

Permalink
made to work also for ESP32, added data/config.json
Browse files Browse the repository at this point in the history
  • Loading branch information
robertoostenveld committed Feb 28, 2024
1 parent c4b3cb1 commit 02da39a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
5 changes: 5 additions & 0 deletions esp8266_config_spiffs/data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"var1":10,
"var2":20,
"var3":30
}
49 changes: 41 additions & 8 deletions esp8266_config_spiffs/esp8266_config_spiffs.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include <ArduinoJson.h>
#include <FS.h>

#if defined(ESP32)
#include <SPIFFS.h>
#endif

#ifndef ARDUINOJSON_VERSION
#error ArduinoJson version 5 not found, please include ArduinoJson.h in your .ino file
#endif
Expand All @@ -13,10 +17,9 @@
int var1, var2, var3;

bool loadConfig() {
Serial.println("loadConfig");
File configFile = SPIFFS.open("/config.json", "r");
if (!configFile) {
Serial.println("Failed to open config file");
Serial.println("Failed to open config file for reading");
return false;
}

Expand Down Expand Up @@ -47,7 +50,6 @@ bool loadConfig() {
}

bool saveConfig() {
Serial.println("saveConfig");
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["var1"] = var1;
Expand All @@ -64,27 +66,58 @@ bool saveConfig() {
return true;
}

void printConfig() {
bool printConfig() {
Serial.print("var1 = ");
Serial.println(var1);
Serial.print("var2 = ");
Serial.println(var2);
Serial.print("var3 = ");
Serial.println(var3);
return true;
}

void setup() {
Serial.begin(115200);
while (!Serial) {
;
}
delay(1000);
Serial.println("");
SPIFFS.begin();
loadConfig();
printConfig()
saveConfig();
Serial.println("setup start");

if (SPIFFS.begin()) {
Serial.println("SPIFFS ok");
}
else {
Serial.println("SPIFFS fail");
}

if (loadConfig()) {
Serial.println("loadConfig ok");
}
else {
Serial.println("loadConfig fail");
}

if (printConfig()) {
Serial.println("printConfig ok");
}
else {
Serial.println("printConfig fail");
}

if (saveConfig()) {
Serial.println("saveConfig ok");
}
else {
Serial.println("loadConfig fail");
}

SPIFFS.end();
Serial.println("setup end");
}

void loop() {
delay(1000);
Serial.print(".");
}

0 comments on commit 02da39a

Please sign in to comment.