//https://github.com/bblanchon/ArduinoJson/issues/1473
#include <ArduinoJson.h>
#include "FS.h"
#include <LittleFS.h>
// Our configuration structure.
struct Config {
char cardHolder[64];
int cardNumber;
};
const char *filename = "/config.txt"; // <- SD library uses 8.3 filenames
Config config; // <- global configuration object
// Loads the configuration from a file
void loadConfiguration(const char *filename, Config &config) {
File file = LittleFS.open(filename, "r");
if (!file) {
Serial.println("Failed to open data file");
return;
}
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/v6/assistant to compute the capacity.
StaticJsonDocument<512> doc;
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, file);
if (error)
Serial.println(F("Failed to read file, using default configuration"));
// Copy values from the JsonDocument to the Config
config.cardNumber = doc["cardNumber"];
strlcpy(config.cardHolder, // <- destination
doc["cardHolder"] | "example.com", // <- source
sizeof(config.cardHolder)); // <- destination's capacity
// Close the file (Curiously, File's destructor doesn't close the file)
file.close();
}
// Saves the configuration to a file
void saveConfiguration(const char *filename, const Config &config) {
// Delete existing file, otherwise the configuration is appended to the file
//SD.remove(filename);
// Open file for writing
File file = LittleFS.open(filename, "w");
if (!file) {
Serial.println("Failed to open config file for writing");
return;
}
// Allocate a temporary JsonDocument
// Don't forget to change the capacity to match your requirements.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonDocument<256> doc;
// Set the values in the document
doc["cardHolder"] = config.cardHolder;
doc["cardNumber"] = config.cardNumber;
// Serialize JSON to file
if (serializeJson(doc, file) == 0) {
Serial.println(F("Failed to write to file"));
}
// Close the file
file.close();
//help
}
// Prints the content of a file to the Serial
void printFile(const char *filename) {
// Open file for reading
File file = LittleFS.open(filename, "r");
if (!file) {
Serial.println("Failed to open data file");
return;
}
// Extract each characters by one by one
while (file.available()) {
Serial.print((char)file.read());
}
Serial.println();
// Close the file
file.close();
}
void setup() {
// Initialize serial port
Serial.begin(9600);
while (!Serial) continue;
// Initialize SD library
while (!LittleFS.begin()) {
Serial.println(F("Failed to initialize SD library"));
delay(1000);
}
// Should load default config if run for the first time
Serial.println(F("Loading configuration..."));
loadConfiguration(filename, config);
// Create configuration file
Serial.println(F("Saving configuration..."));
saveConfiguration(filename, config);
// Dump config file
Serial.println(F("Print config file..."));
printFile(filename);
}
void loop() {
// not used in this example
config.cardNumber = config.cardNumber + 1;
Serial.println(config.cardNumber);
saveConfiguration(filename, config);
delay(1000);
}