JsonConfigFile.ino 4.2 KB

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