| 
									
										
										
										
											2021-03-29 17:14:01 +02:00
										 |  |  | // ArduinoJson - https://arduinojson.org
 | 
					
						
							| 
									
										
										
										
											2025-02-24 15:18:26 +01:00
										 |  |  | // Copyright © 2014-2025, Benoit BLANCHON
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  | // MIT License
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This example shows how to store your project configuration in a file.
 | 
					
						
							|  |  |  | // It uses the SD library but can be easily modified for any other file-system.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // The file contains a JSON document with the following content:
 | 
					
						
							|  |  |  | // {
 | 
					
						
							|  |  |  | //   "hostname": "examples.com",
 | 
					
						
							|  |  |  | //   "port": 2731
 | 
					
						
							|  |  |  | // }
 | 
					
						
							| 
									
										
										
										
											2019-03-04 12:17:41 +01:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-08-10 14:17:23 +02:00
										 |  |  | // To run this program, you need an SD card connected to the SPI bus as follows:
 | 
					
						
							|  |  |  | // * MOSI <-> pin 11
 | 
					
						
							|  |  |  | // * MISO <-> pin 12
 | 
					
						
							|  |  |  | // * CLK  <-> pin 13
 | 
					
						
							|  |  |  | // * CS   <-> pin 4
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2023-08-19 16:08:12 +02:00
										 |  |  | // https://arduinojson.org/v7/example/config/
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <ArduinoJson.h>
 | 
					
						
							|  |  |  | #include <SD.h>
 | 
					
						
							|  |  |  | #include <SPI.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  | // Our configuration structure.
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  | struct Config { | 
					
						
							|  |  |  |   char hostname[64]; | 
					
						
							|  |  |  |   int port; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-15 14:54:55 +01:00
										 |  |  | const char* filename = "/config.txt";  // <- SD library uses 8.3 filenames
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  | Config config;                         // <- global configuration object
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Loads the configuration from a file
 | 
					
						
							| 
									
										
										
										
											2023-03-15 14:54:55 +01:00
										 |  |  | void loadConfiguration(const char* filename, Config& config) { | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  |   // Open file for reading
 | 
					
						
							|  |  |  |   File file = SD.open(filename); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  |   // Allocate a temporary JsonDocument
 | 
					
						
							| 
									
										
										
										
											2023-07-17 18:15:13 +02:00
										 |  |  |   JsonDocument doc; | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   // Deserialize the JSON document
 | 
					
						
							| 
									
										
										
										
											2018-05-15 18:23:09 +02:00
										 |  |  |   DeserializationError error = deserializeJson(doc, file); | 
					
						
							| 
									
										
										
										
											2018-03-09 16:58:01 +01:00
										 |  |  |   if (error) | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  |     Serial.println(F("Failed to read file, using default configuration")); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  |   // Copy values from the JsonDocument to the Config
 | 
					
						
							|  |  |  |   config.port = doc["port"] | 2731; | 
					
						
							|  |  |  |   strlcpy(config.hostname,                  // <- destination
 | 
					
						
							|  |  |  |           doc["hostname"] | "example.com",  // <- source
 | 
					
						
							|  |  |  |           sizeof(config.hostname));         // <- destination's capacity
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  |   // Close the file (Curiously, File's destructor doesn't close the file)
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  |   file.close(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Saves the configuration to a file
 | 
					
						
							| 
									
										
										
										
											2023-03-15 14:54:55 +01:00
										 |  |  | void saveConfiguration(const char* filename, const Config& config) { | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  |   // Delete existing file, otherwise the configuration is appended to the file
 | 
					
						
							|  |  |  |   SD.remove(filename); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Open file for writing
 | 
					
						
							|  |  |  |   File file = SD.open(filename, FILE_WRITE); | 
					
						
							|  |  |  |   if (!file) { | 
					
						
							|  |  |  |     Serial.println(F("Failed to create file")); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  |   // Allocate a temporary JsonDocument
 | 
					
						
							| 
									
										
										
										
											2023-07-17 18:15:13 +02:00
										 |  |  |   JsonDocument doc; | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  |   // Set the values in the document
 | 
					
						
							|  |  |  |   doc["hostname"] = config.hostname; | 
					
						
							|  |  |  |   doc["port"] = config.port; | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Serialize JSON to file
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   if (serializeJson(doc, file) == 0) { | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  |     Serial.println(F("Failed to write to file")); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  |   // Close the file
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  |   file.close(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Prints the content of a file to the Serial
 | 
					
						
							| 
									
										
										
										
											2023-03-15 14:54:55 +01:00
										 |  |  | void printFile(const char* filename) { | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  |   // Open file for reading
 | 
					
						
							|  |  |  |   File file = SD.open(filename); | 
					
						
							|  |  |  |   if (!file) { | 
					
						
							|  |  |  |     Serial.println(F("Failed to read file")); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Extract each characters by one by one
 | 
					
						
							|  |  |  |   while (file.available()) { | 
					
						
							|  |  |  |     Serial.print((char)file.read()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   Serial.println(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-29 14:09:09 +01:00
										 |  |  |   // Close the file
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  |   file.close(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void setup() { | 
					
						
							|  |  |  |   // Initialize serial port
 | 
					
						
							|  |  |  |   Serial.begin(9600); | 
					
						
							| 
									
										
										
										
											2023-03-15 14:54:55 +01:00
										 |  |  |   while (!Serial) | 
					
						
							|  |  |  |     continue; | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Initialize SD library
 | 
					
						
							| 
									
										
										
										
											2019-08-10 14:17:23 +02:00
										 |  |  |   const int chipSelect = 4; | 
					
						
							|  |  |  |   while (!SD.begin(chipSelect)) { | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  |     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
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-25 09:26:29 +01:00
										 |  |  | // Performance issue?
 | 
					
						
							|  |  |  | // ------------------
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // File is an unbuffered stream, which is not optimal for ArduinoJson.
 | 
					
						
							| 
									
										
										
										
											2023-08-19 16:08:12 +02:00
										 |  |  | // See: https://arduinojson.org/v7/how-to/improve-speed/
 | 
					
						
							| 
									
										
										
										
											2021-01-25 09:26:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-04 12:17:41 +01:00
										 |  |  | // See also
 | 
					
						
							|  |  |  | // --------
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // https://arduinojson.org/ contains the documentation for all the functions
 | 
					
						
							|  |  |  | // used above. It also includes an FAQ that will help you solve any
 | 
					
						
							|  |  |  | // serialization or deserialization problem.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // The book "Mastering ArduinoJson" contains a case study of a project that has
 | 
					
						
							|  |  |  | // a complex configuration with nested members.
 | 
					
						
							|  |  |  | // Contrary to this example, the project in the book uses the SPIFFS filesystem.
 | 
					
						
							|  |  |  | // Learn more at https://arduinojson.org/book/
 | 
					
						
							|  |  |  | // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
 |