JsonConfigFile.ino 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, 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. #include <ArduinoJson.h>
  22. #include <SD.h>
  23. #include <SPI.h>
  24. // Our configuration structure.
  25. struct Config {
  26. char hostname[64];
  27. int port;
  28. };
  29. const char* filename = "/config.txt"; // <- SD library uses 8.3 filenames
  30. Config config; // <- global configuration object
  31. // Loads the configuration from a file
  32. void loadConfiguration(const char* filename, Config& config) {
  33. // Open file for reading
  34. File file = SD.open(filename);
  35. // Allocate a temporary JsonDocument
  36. JsonDocument doc;
  37. // Deserialize the JSON document
  38. DeserializationError error = deserializeJson(doc, file);
  39. if (error)
  40. Serial.println(F("Failed to read file, using default configuration"));
  41. // Copy values from the JsonDocument to the Config
  42. config.port = doc["port"] | 2731;
  43. strlcpy(config.hostname, // <- destination
  44. doc["hostname"] | "example.com", // <- source
  45. sizeof(config.hostname)); // <- destination's capacity
  46. // Close the file (Curiously, File's destructor doesn't close the file)
  47. file.close();
  48. }
  49. // Saves the configuration to a file
  50. void saveConfiguration(const char* filename, const Config& config) {
  51. // Delete existing file, otherwise the configuration is appended to the file
  52. SD.remove(filename);
  53. // Open file for writing
  54. File file = SD.open(filename, FILE_WRITE);
  55. if (!file) {
  56. Serial.println(F("Failed to create file"));
  57. return;
  58. }
  59. // Allocate a temporary JsonDocument
  60. JsonDocument 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)
  91. continue;
  92. // Initialize SD library
  93. const int chipSelect = 4;
  94. while (!SD.begin(chipSelect)) {
  95. Serial.println(F("Failed to initialize SD library"));
  96. delay(1000);
  97. }
  98. // Should load default config if run for the first time
  99. Serial.println(F("Loading configuration..."));
  100. loadConfiguration(filename, config);
  101. // Create configuration file
  102. Serial.println(F("Saving configuration..."));
  103. saveConfiguration(filename, config);
  104. // Dump config file
  105. Serial.println(F("Print config file..."));
  106. printFile(filename);
  107. }
  108. void loop() {
  109. // not used in this example
  110. }
  111. // Performance issue?
  112. // ------------------
  113. //
  114. // File is an unbuffered stream, which is not optimal for ArduinoJson.
  115. // See: https://arduinojson.org/v7/how-to/improve-speed/
  116. // See also
  117. // --------
  118. //
  119. // https://arduinojson.org/ contains the documentation for all the functions
  120. // used above. It also includes an FAQ that will help you solve any
  121. // serialization or deserialization problem.
  122. //
  123. // The book "Mastering ArduinoJson" contains a case study of a project that has
  124. // a complex configuration with nested members.
  125. // Contrary to this example, the project in the book uses the SPIFFS filesystem.
  126. // Learn more at https://arduinojson.org/book/
  127. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤