Krolyxon
Commit 1d55752

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8ArduinoJsonexamplesJsonConfigFileJsonConfigFile.ino

152 lines · 4.0 KB

1// ArduinoJson - https://arduinojson.org
2// Copyright © 2014-2026, Benoit BLANCHON
3// MIT License
4//
5// This example shows how to store your project configuration in a file.
6// It uses the SD library but can be easily modified for any other file-system.
7//
8// The file contains a JSON document with the following content:
9// {
10// "hostname": "examples.com",
11// "port": 2731
12// }
13//
14// To run this program, you need an SD card connected to the SPI bus as follows:
15// * MOSI <-> pin 11
16// * MISO <-> pin 12
17// * CLK <-> pin 13
18// * CS <-> pin 4
19//
20// https://arduinojson.org/v7/example/config/
21
22#include <ArduinoJson.h>
23#include <SD.h>
24#include <SPI.h>
25
26// Our configuration structure.
27struct Config {
28 char hostname[64];
29 int port;
30};
31
32const char* filename = "/config.txt"; // <- SD library uses 8.3 filenames
33Config config; // <- global configuration object
34
35// Loads the configuration from a file
36void loadConfiguration(const char* filename, Config& config) {
37 // Open file for reading
38 File file = SD.open(filename);
39
40 // Allocate a temporary JsonDocument
41 JsonDocument doc;
42
43 // Deserialize the JSON document
44 DeserializationError error = deserializeJson(doc, file);
45 if (error)
46 Serial.println(F("Failed to read file, using default configuration"));
47
48 // Copy values from the JsonDocument to the Config
49 config.port = doc["port"] | 2731;
50 strlcpy(config.hostname, // <- destination
51 doc["hostname"] | "example.com", // <- source
52 sizeof(config.hostname)); // <- destination's capacity
53
54 // Close the file (Curiously, File's destructor doesn't close the file)
55 file.close();
56}
57
58// Saves the configuration to a file
59void saveConfiguration(const char* filename, const Config& config) {
60 // Delete existing file, otherwise the configuration is appended to the file
61 SD.remove(filename);
62
63 // Open file for writing
64 File file = SD.open(filename, FILE_WRITE);
65 if (!file) {
66 Serial.println(F("Failed to create file"));
67 return;
68 }
69
70 // Allocate a temporary JsonDocument
71 JsonDocument doc;
72
73 // Set the values in the document
74 doc["hostname"] = config.hostname;
75 doc["port"] = config.port;
76
77 // Serialize JSON to file
78 if (serializeJson(doc, file) == 0) {
79 Serial.println(F("Failed to write to file"));
80 }
81
82 // Close the file
83 file.close();
84}
85
86// Prints the content of a file to the Serial
87void printFile(const char* filename) {
88 // Open file for reading
89 File file = SD.open(filename);
90 if (!file) {
91 Serial.println(F("Failed to read file"));
92 return;
93 }
94
95 // Extract each characters by one by one
96 while (file.available()) {
97 Serial.print((char)file.read());
98 }
99 Serial.println();
100
101 // Close the file
102 file.close();
103}
104
105void setup() {
106 // Initialize serial port
107 Serial.begin(9600);
108 while (!Serial)
109 continue;
110
111 // Initialize SD library
112 const int chipSelect = 4;
113 while (!SD.begin(chipSelect)) {
114 Serial.println(F("Failed to initialize SD library"));
115 delay(1000);
116 }
117
118 // Should load default config if run for the first time
119 Serial.println(F("Loading configuration..."));
120 loadConfiguration(filename, config);
121
122 // Create configuration file
123 Serial.println(F("Saving configuration..."));
124 saveConfiguration(filename, config);
125
126 // Dump config file
127 Serial.println(F("Print config file..."));
128 printFile(filename);
129}
130
131void loop() {
132 // not used in this example
133}
134
135// Performance issue?
136// ------------------
137//
138// File is an unbuffered stream, which is not optimal for ArduinoJson.
139// See: https://arduinojson.org/v7/how-to/improve-speed/
140
141// See also
142// --------
143//
144// https://arduinojson.org/ contains the documentation for all the functions
145// used above. It also includes an FAQ that will help you solve any
146// serialization or deserialization problem.
147//
148// The book "Mastering ArduinoJson" contains a case study of a project that has
149// a complex configuration with nested members.
150// Contrary to this example, the project in the book uses the SPIFFS filesystem.
151// Learn more at https://arduinojson.org/book/
152// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤