Advertisement
noam76

jsconfig.h

Apr 29th, 2023
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void loadConfiguration() {
  2.   // Open the configuration file for reading
  3.   File configFile = SPIFFS.open("/config.json", "r");
  4.   if (!configFile) {
  5.     Serial.println(F("Failed to open config file for reading"));
  6.     return;
  7.   }
  8.  
  9.   // Parse the JSON document from the configuration file
  10.   const size_t capacity = JSON_ARRAY_SIZE(SOLENOID_COUNT) + SOLENOID_COUNT*JSON_OBJECT_SIZE(2);
  11.   DynamicJsonDocument doc(capacity);
  12.   DeserializationError error = deserializeJson(doc, configFile);
  13.   if (error) {
  14.     Serial.println(F("Failed to parse config file"));
  15.     return;
  16.   }
  17.  
  18.   // Close the configuration file
  19.   configFile.close();
  20.  
  21.   // Get the userMax and userMin arrays from the JSON document
  22.   JsonArray maxArray = doc["userMax"];
  23.   if (maxArray.size() != MUX_COUNT) {
  24.     Serial.println(F("Invalid userMax array size"));
  25.     return;
  26.   }
  27.   for (int i = 0; i < MUX_COUNT; i++) {
  28.     userMax[i] = maxArray[i];
  29.   }
  30.   JsonArray minArray = doc["userMin"];
  31.   if (minArray.size() != MUX_COUNT) {
  32.     Serial.println(F("Invalid userMin array size"));
  33.     return;
  34.   }
  35.   for (int i = 0; i < MUX_COUNT; i++) {
  36.     userMin[i] = minArray[i];
  37.   }
  38.  
  39.   // Print the loaded user-defined maximum and minimum values for each sensor to the serial monitor
  40.   Serial.println("Loaded user-defined soil humidity thresholds:");
  41.   for (int i = 0; i < MUX_COUNT; i++) {
  42.     Serial.print("Sensor "); Serial.print(i); Serial.print(": Max = "); Serial.print(userMax[i]); Serial.print(", Min = "); Serial.println(userMin[i]);
  43.   }
  44. }
  45.  
  46. void saveConfiguration() {
  47.   // Créer un nouveau document JSON
  48.   DynamicJsonDocument doc(1024);
  49.  
  50.     // Add the userMax and userMin arrays to the JSON document
  51.   JsonArray maxArray = doc.createNestedArray("userMax");
  52.   for (int i = 0; i < SOLENOID_COUNT; i++) {
  53.     maxArray.add(userMax[i]);
  54.   }
  55.   JsonArray minArray = doc.createNestedArray("userMin");
  56.   for (int i = 0; i < SOLENOID_COUNT; i++) {
  57.     minArray.add(userMin[i]);
  58.   }
  59.  
  60.   // Open the configuration file for writing
  61.   File configFile = SPIFFS.open("/config.json", "w");
  62.   if (!configFile) {
  63.     Serial.println(F("Failed to open config file for writing"));
  64.     return;
  65.   }
  66.  
  67.   // Serialize the JSON document to the configuration file
  68.   if (serializeJson(doc, configFile) == 0) {
  69.     Serial.println(F("Failed to write to config file"));
  70.   }
  71.  
  72.   // Close the configuration file
  73.   configFile.close();
  74.   Serial.println("Data saved to file");
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement