JsonConfigFile.ino 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2018
  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. #include <ArduinoJson.h>
  14. #include <SD.h>
  15. #include <SPI.h>
  16. // Our configuration structure.
  17. //
  18. // Never use a JsonDocument to store the configuration!
  19. // A JsonDocument is *not* a permanent storage; it's only a temporary storage
  20. // used during the serialization phase.
  21. struct Config {
  22. char hostname[64];
  23. int port;
  24. };
  25. const char *filename = "/config.txt"; // <- SD library uses 8.3 filenames
  26. Config config; // <- global configuration object
  27. // Loads the configuration from a file
  28. void loadConfiguration(const char *filename, Config &config) {
  29. // Open file for reading
  30. File file = SD.open(filename);
  31. // Allocate a temporary JsonDocument
  32. // Don't forget to change the capacity to match your requirements.
  33. // Use arduinojson.org/v6/assistant to compute the capacity.
  34. StaticJsonDocument<512> doc;
  35. // Deserialize the JSON document
  36. DeserializationError error = deserializeJson(doc, file);
  37. if (error)
  38. Serial.println(F("Failed to read file, using default configuration"));
  39. // Copy values from the JsonDocument to the Config
  40. config.port = doc["port"] | 2731;
  41. strlcpy(config.hostname, // <- destination
  42. doc["hostname"] | "example.com", // <- source
  43. sizeof(config.hostname)); // <- destination's capacity
  44. // Close the file (Curiously, File's destructor doesn't close the file)
  45. file.close();
  46. }
  47. // Saves the configuration to a file
  48. void saveConfiguration(const char *filename, const Config &config) {
  49. // Delete existing file, otherwise the configuration is appended to the file
  50. SD.remove(filename);
  51. // Open file for writing
  52. File file = SD.open(filename, FILE_WRITE);
  53. if (!file) {
  54. Serial.println(F("Failed to create file"));
  55. return;
  56. }
  57. // Allocate a temporary JsonDocument
  58. // Don't forget to change the capacity to match your requirements.
  59. // Use arduinojson.org/assistant to compute the capacity.
  60. StaticJsonDocument<256> doc;
  61. // Set the values in the document
  62. doc["hostname"] = config.hostname;
  63. doc["port"] = config.port;
  64. // Serialize JSON to file
  65. if (serializeJson(doc, file) == 0) {
  66. Serial.println(F("Failed to write to file"));
  67. }
  68. // Close the file
  69. file.close();
  70. }
  71. // Prints the content of a file to the Serial
  72. void printFile(const char *filename) {
  73. // Open file for reading
  74. File file = SD.open(filename);
  75. if (!file) {
  76. Serial.println(F("Failed to read file"));
  77. return;
  78. }
  79. // Extract each characters by one by one
  80. while (file.available()) {
  81. Serial.print((char)file.read());
  82. }
  83. Serial.println();
  84. // Close the file
  85. file.close();
  86. }
  87. void setup() {
  88. // Initialize serial port
  89. Serial.begin(9600);
  90. while (!Serial) continue;
  91. // Initialize SD library
  92. while (!SD.begin()) {
  93. Serial.println(F("Failed to initialize SD library"));
  94. delay(1000);
  95. }
  96. // Should load default config if run for the first time
  97. Serial.println(F("Loading configuration..."));
  98. loadConfiguration(filename, config);
  99. // Create configuration file
  100. Serial.println(F("Saving configuration..."));
  101. saveConfiguration(filename, config);
  102. // Dump config file
  103. Serial.println(F("Print config file..."));
  104. printFile(filename);
  105. }
  106. void loop() {
  107. // not used in this example
  108. }
  109. // Visit https://arduinojson.org/v6/example/config/ for more.