Skip to content

Commit 8ada9c9

Browse files
committed
Add example for storing JSON config file in SPIFFS
1 parent d108a6e commit 8ada9c9

File tree

1 file changed

+97
-0
lines changed
  • hardware/esp8266com/esp8266/libraries/esp8266/examples/ConfigFile

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Example: storing JSON configuration file in flash file system
2+
//
3+
// Uses ArduinoJson library by Benoit Blanchon.
4+
// https://github.com/bblanchon/ArduinoJson
5+
//
6+
// Created Aug 10, 2015 by Ivan Grokhotkov.
7+
//
8+
// This example code is in the public domain.
9+
10+
#include <ArduinoJson.h>
11+
#include "FS.h"
12+
13+
bool loadConfig() {
14+
File configFile = SPIFFS.open("/config.json", "r");
15+
if (!configFile) {
16+
Serial.println("Failed to open config file");
17+
return false;
18+
}
19+
20+
size_t size = configFile.size();
21+
if (size > 1024) {
22+
Serial.println("Config file size is too large");
23+
return false;
24+
}
25+
26+
// Allocate a buffer to store contents of the file.
27+
std::unique_ptr<char[]> buf(new char[size]);
28+
29+
// We don't use String here because ArduinoJson library requires the input
30+
// buffer to be mutable. If you don't use ArduinoJson, you may as well
31+
// use configFile.readString instead.
32+
configFile.readBytes(buf.get(), size);
33+
34+
StaticJsonBuffer<200> jsonBuffer;
35+
JsonObject& json = jsonBuffer.parseObject(buf.get());
36+
37+
if (!json.success()) {
38+
Serial.println("Failed to parse config file");
39+
return false;
40+
}
41+
42+
const char* serverName = json["serverName"];
43+
const char* accessToken = json["accessToken"];
44+
45+
// Real world application would store these values in some variables for
46+
// later use.
47+
48+
Serial.print("Loaded serverName: ");
49+
Serial.println(serverName);
50+
Serial.print("Loaded accessToken: ");
51+
Serial.println(accessToken);
52+
return true;
53+
}
54+
55+
bool saveConfig() {
56+
StaticJsonBuffer<200> jsonBuffer;
57+
JsonObject& json = jsonBuffer.createObject();
58+
json["serverName"] = "api.example.com";
59+
json["accessToken"] = "128du9as8du12eoue8da98h123ueh9h98";
60+
61+
File configFile = SPIFFS.open("/config.json", "w");
62+
if (!configFile) {
63+
Serial.println("Failed to open config file for writing");
64+
return false;
65+
}
66+
67+
json.printTo(configFile);
68+
return true;
69+
}
70+
71+
void setup() {
72+
Serial.begin(115200);
73+
Serial.println("");
74+
delay(1000);
75+
Serial.println("Mounting FS...");
76+
77+
if (!SPIFFS.begin()) {
78+
Serial.println("Failed to mount file system");
79+
return;
80+
}
81+
82+
83+
if (!saveConfig()) {
84+
Serial.println("Failed to save config");
85+
} else {
86+
Serial.println("Config saved");
87+
}
88+
89+
if (!loadConfig()) {
90+
Serial.println("Failed to load config");
91+
} else {
92+
Serial.println("Config loaded");
93+
}
94+
}
95+
96+
void loop() {
97+
}

0 commit comments

Comments
 (0)