JsonConfigFile.ino 4.0 KB

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