JsonConfigFile.ino 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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/v6/example/config/
  21. #include <ArduinoJson.h>
  22. #include <SD.h>
  23. #include <SPI.h>
  24. // Our configuration structure.
  25. //
  26. // Never use a JsonDocument to store the configuration!
  27. // A JsonDocument is *not* a permanent storage; it's only a temporary storage
  28. // used during the serialization phase. See:
  29. // https://arduinojson.org/v6/faq/why-must-i-create-a-separate-config-object/
  30. struct Config {
  31. char hostname[64];
  32. int port;
  33. };
  34. const char *filename = "/config.txt"; // <- SD library uses 8.3 filenames
  35. Config config; // <- global configuration object
  36. // Loads the configuration from a file
  37. void loadConfiguration(const char *filename, Config &config) {
  38. // Open file for reading
  39. File file = SD.open(filename);
  40. // Allocate a temporary JsonDocument
  41. // Don't forget to change the capacity to match your requirements.
  42. // Use arduinojson.org/v6/assistant to compute the capacity.
  43. StaticJsonDocument<512> doc;
  44. // Deserialize the JSON document
  45. DeserializationError error = deserializeJson(doc, file);
  46. if (error)
  47. Serial.println(F("Failed to read file, using default configuration"));
  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. // Close the file (Curiously, File's destructor doesn't close the file)
  54. file.close();
  55. }
  56. // Saves the configuration to a file
  57. void saveConfiguration(const char *filename, const Config &config) {
  58. // Delete existing file, otherwise the configuration is appended to the file
  59. SD.remove(filename);
  60. // Open file for writing
  61. File file = SD.open(filename, FILE_WRITE);
  62. if (!file) {
  63. Serial.println(F("Failed to create file"));
  64. return;
  65. }
  66. // Allocate a temporary JsonDocument
  67. // Don't forget to change the capacity to match your requirements.
  68. // Use arduinojson.org/assistant to compute the capacity.
  69. StaticJsonDocument<256> doc;
  70. // Set the values in the document
  71. doc["hostname"] = config.hostname;
  72. doc["port"] = config.port;
  73. // Serialize JSON to file
  74. if (serializeJson(doc, file) == 0) {
  75. Serial.println(F("Failed to write to file"));
  76. }
  77. // Close the file
  78. file.close();
  79. }
  80. // Prints the content of a file to the Serial
  81. void printFile(const char *filename) {
  82. // Open file for reading
  83. File file = SD.open(filename);
  84. if (!file) {
  85. Serial.println(F("Failed to read file"));
  86. return;
  87. }
  88. // Extract each characters by one by one
  89. while (file.available()) {
  90. Serial.print((char)file.read());
  91. }
  92. Serial.println();
  93. // Close the file
  94. file.close();
  95. }
  96. void setup() {
  97. // Initialize serial port
  98. Serial.begin(9600);
  99. while (!Serial) continue;
  100. // Initialize SD library
  101. const int chipSelect = 4;
  102. while (!SD.begin(chipSelect)) {
  103. Serial.println(F("Failed to initialize SD library"));
  104. delay(1000);
  105. }
  106. // Should load default config if run for the first time
  107. Serial.println(F("Loading configuration..."));
  108. loadConfiguration(filename, config);
  109. // Create configuration file
  110. Serial.println(F("Saving configuration..."));
  111. saveConfiguration(filename, config);
  112. // Dump config file
  113. Serial.println(F("Print config file..."));
  114. printFile(filename);
  115. }
  116. void loop() {
  117. // not used in this example
  118. }
  119. // See also
  120. // --------
  121. //
  122. // https://arduinojson.org/ contains the documentation for all the functions
  123. // used above. It also includes an FAQ that will help you solve any
  124. // serialization or deserialization problem.
  125. //
  126. // The book "Mastering ArduinoJson" contains a case study of a project that has
  127. // a complex configuration with nested members.
  128. // Contrary to this example, the project in the book uses the SPIFFS filesystem.
  129. // Learn more at https://arduinojson.org/book/
  130. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤