JsonConfigFile.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #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. StaticJsonBuffer<512> jsonBuffer;
  31. // Parse the root object
  32. JsonObject &root = jsonBuffer.parseObject(file);
  33. if (!root.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. StaticJsonBuffer<256> jsonBuffer;
  57. // Parse the root object
  58. JsonObject &root = jsonBuffer.createObject();
  59. // Set the values
  60. root["hostname"] = config.hostname;
  61. root["port"] = config.port;
  62. // Serialize JSON to file
  63. if (root.printTo(file) == 0) {
  64. Serial.println(F("Failed to write to file"));
  65. }
  66. // Close the file (File's destructor doesn't close the file)
  67. file.close();
  68. }
  69. // Prints the content of a file to the Serial
  70. void printFile(const char *filename) {
  71. // Open file for reading
  72. File file = SD.open(filename);
  73. if (!file) {
  74. Serial.println(F("Failed to read file"));
  75. return;
  76. }
  77. // Extract each characters by one by one
  78. while (file.available()) {
  79. Serial.print((char)file.read());
  80. }
  81. Serial.println();
  82. // Close the file (File's destructor doesn't close the file)
  83. file.close();
  84. }
  85. void setup() {
  86. // Initialize serial port
  87. Serial.begin(9600);
  88. while (!Serial) continue;
  89. // Initialize SD library
  90. while (!SD.begin()) {
  91. Serial.println(F("Failed to initialize SD library"));
  92. delay(1000);
  93. }
  94. // Should load default config if run for the first time
  95. Serial.println(F("Loading configuration..."));
  96. loadConfiguration(filename, config);
  97. // Create configuration file
  98. Serial.println(F("Saving configuration..."));
  99. saveConfiguration(filename, config);
  100. // Dump config file
  101. Serial.println(F("Print config file..."));
  102. printFile(filename);
  103. }
  104. void loop() {
  105. // not used in this example
  106. }
  107. // See also
  108. // --------
  109. //
  110. // https://arduinojson.org/ contains the documentation for all the functions
  111. // used above. It also includes an FAQ that will help you solve any
  112. // serialization or deserialization problem.
  113. //
  114. // The book "Mastering ArduinoJson" contains a case study of a project that has
  115. // a complex configuration with nested members.
  116. // Contrary to this example, the project in the book uses the SPIFFS filesystem.
  117. // Learn more at https://arduinojson.org/book/
  118. // Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤