| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  | // ArduinoJson - arduinojson.org
 | 
					
						
							| 
									
										
										
										
											2018-01-05 09:20:01 +01:00
										 |  |  | // Copyright Benoit Blanchon 2014-2018
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | // }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <ArduinoJson.h>
 | 
					
						
							|  |  |  | #include <SD.h>
 | 
					
						
							|  |  |  | #include <SPI.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Configuration that we'll store on disk
 | 
					
						
							|  |  |  | struct Config { | 
					
						
							|  |  |  |   char hostname[64]; | 
					
						
							|  |  |  |   int port; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) { | 
					
						
							|  |  |  |   // Open file for reading
 | 
					
						
							|  |  |  |   File file = SD.open(filename); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   // Allocate the document on the stack.
 | 
					
						
							|  |  |  |   // Don't forget to change the capacity to match your requirements.
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:26:50 +01:00
										 |  |  |   // Use arduinojson.org/assistant to compute the capacity.
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   StaticJsonDocument<512> 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")); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   // Get the root object in the document
 | 
					
						
							| 
									
										
										
										
											2018-07-02 09:35:21 +02:00
										 |  |  |   JsonObject root = doc.as<JsonObject>(); | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  |   // Copy values from the JsonObject to the Config
 | 
					
						
							|  |  |  |   config.port = root["port"] | 2731; | 
					
						
							|  |  |  |   strlcpy(config.hostname,                   // <- destination
 | 
					
						
							|  |  |  |           root["hostname"] | "example.com",  // <- source
 | 
					
						
							|  |  |  |           sizeof(config.hostname));          // <- destination's capacity
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Close the file (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 = SD.open(filename, FILE_WRITE); | 
					
						
							|  |  |  |   if (!file) { | 
					
						
							|  |  |  |     Serial.println(F("Failed to create file")); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   // Allocate the document on the stack.
 | 
					
						
							|  |  |  |   // Don't forget to change the capacity to match your requirements.
 | 
					
						
							|  |  |  |   // Use arduinojson.org/assistant to compute the capacity.
 | 
					
						
							|  |  |  |   StaticJsonDocument<256> doc; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Make our document contain an object
 | 
					
						
							| 
									
										
										
										
											2018-07-02 09:35:21 +02:00
										 |  |  |   JsonObject root = doc.to<JsonObject>(); | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-17 21:27:45 +02:00
										 |  |  |   // Set the values in the object
 | 
					
						
							| 
									
										
										
										
											2017-12-11 17:20:27 +01:00
										 |  |  |   root["hostname"] = config.hostname; | 
					
						
							|  |  |  |   root["port"] = config.port; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 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")); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Close the file (File's destructor doesn't close the file)
 | 
					
						
							|  |  |  |   file.close(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Prints the content of a file to the Serial
 | 
					
						
							|  |  |  | void printFile(const char *filename) { | 
					
						
							|  |  |  |   // 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(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Close the file (File's destructor doesn't close the file)
 | 
					
						
							|  |  |  |   file.close(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void setup() { | 
					
						
							|  |  |  |   // Initialize serial port
 | 
					
						
							|  |  |  |   Serial.begin(9600); | 
					
						
							|  |  |  |   while (!Serial) continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Initialize SD library
 | 
					
						
							|  |  |  |   while (!SD.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
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-02 09:35:21 +02:00
										 |  |  | // Visit https://arduinojson.org/v6/example/config/ for more.
 |