noam76

Config file Json 6

Jul 9th, 2021
786
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ArduinoJson.h>
  2. #include <FS.h>
  3.  
  4. bool loadConfiguration() {
  5.  File file = SPIFFS.open("/config.json", "r");
  6.  if (!file) {
  7.   Serial.println("Failed to open config file");
  8.   return false;
  9.  }
  10.  
  11.   DynamicJsonDocument jsonBuffer(2048);
  12.   DeserializationError error = deserializeJson(jsonBuffer, file);
  13.   if (error) {
  14.    Serial.print(F("deserializeJson() failed: "));
  15.    Serial.println(error.f_str());
  16.    return false;
  17.   }
  18.  
  19.   // Copy values from the JsonObject to the Config
  20.  Usermax0 = jsonBuffer["Usermax0"];
  21.  Usermin0 = jsonBuffer["Usermin0"];
  22.  Usermax1 = jsonBuffer["Usermax1"];
  23.  Usermin1 = jsonBuffer["Usermin1"];
  24.  Usermax2 = jsonBuffer["Usermax2"];
  25.  Usermin2 = jsonBuffer["Usermin2"];
  26.  Usermax3 = jsonBuffer["Usermax3"];
  27.  Usermin3 = jsonBuffer["Usermin3"];
  28.  Usermax4 = jsonBuffer["Usermax4"];
  29.  Usermin4 = jsonBuffer["Usermin4"];
  30.  Usermax5 = jsonBuffer["Usermax5"];
  31.  Usermin5 = jsonBuffer["Usermin5"];
  32.  Usermax6 = jsonBuffer["Usermax6"];
  33.  Usermin6 = jsonBuffer["Usermin6"];
  34.  Usermax7 = jsonBuffer["Usermax7"];
  35.  Usermin7 = jsonBuffer["Usermin7"];
  36.  Usermax8 = jsonBuffer["Usermax8"];
  37.  Usermin8 = jsonBuffer["Usermin8"];
  38.  Usermax9 = jsonBuffer["Usermax9"];
  39.  Usermin9 = jsonBuffer["Usermin9"];
  40.  Usermax10 = jsonBuffer["Usermax10"];
  41.  Usermin10 = jsonBuffer["Usermin10"];
  42.  Usermax11 = jsonBuffer["Usermax11"];
  43.  Usermin11 = jsonBuffer["Usermin11"];
  44.  Usermax12 = jsonBuffer["Usermax12"];
  45.  Usermin12 = jsonBuffer["Usermin12"];
  46.  Usermax13 = jsonBuffer["Usermax13"];
  47.  Usermin13 = jsonBuffer["Usermin13"];
  48.  Usermax14 = jsonBuffer["Usermax14"];
  49.  Usermin14 = jsonBuffer["Usermin14"];
  50.  Usermax15 = jsonBuffer["Usermax15"];
  51.  Usermin15 = jsonBuffer["Usermin15"];
  52.  
  53.   // Close the file (File's destructor doesn't close the file)
  54.   file.close();
  55. }
  56.  
  57. bool saveConfiguration() {
  58.  File file = SPIFFS.open("/config.json", "w");
  59.  if (!file) {
  60.   Serial.println("Failed to open config file for writing");
  61.   return false;
  62.  }
  63.  
  64.  // DynamicJsonDocument<1536> jsonBuffer;
  65.  DynamicJsonDocument jsonBuffer(1536);
  66.  
  67.  // Set the values
  68.  jsonBuffer["Usermax0"] = Usermax0;
  69.  jsonBuffer["Usermin0"] = Usermin0;
  70.  jsonBuffer["Usermax1"] = Usermax1;
  71.  jsonBuffer["Usermin1"] = Usermin1;
  72.  jsonBuffer["Usermax2"] = Usermax2;
  73.  jsonBuffer["Usermin2"] = Usermin2;
  74.  jsonBuffer["Usermax3"] = Usermax3;
  75.  jsonBuffer["Usermin3"] = Usermin3;
  76.  jsonBuffer["Usermax4"] = Usermax4;
  77.  jsonBuffer["Usermin4"] = Usermin4;
  78.  jsonBuffer["Usermax5"] = Usermax5;
  79.  jsonBuffer["Usermin5"] = Usermin5;
  80.  jsonBuffer["Usermax6"] = Usermax6;
  81.  jsonBuffer["Usermin6"] = Usermin6;
  82.  jsonBuffer["Usermax7"] = Usermax7;
  83.  jsonBuffer["Usermin7"] = Usermin7;
  84.  jsonBuffer["Usermax8"] = Usermax8;
  85.  jsonBuffer["Usermin8"] = Usermin8;
  86.  jsonBuffer["Usermax9"] = Usermax9;
  87.  jsonBuffer["Usermin9"] = Usermin9;
  88.  jsonBuffer["Usermax10"] = Usermax10;
  89.  jsonBuffer["Usermin10"] = Usermin10;
  90.  jsonBuffer["Usermax11"] = Usermax11;
  91.  jsonBuffer["Usermin11"] = Usermin11;
  92.  jsonBuffer["Usermax12"] = Usermax12;
  93.  jsonBuffer["Usermin12"] = Usermin12;
  94.  jsonBuffer["Usermax13"] = Usermax13;
  95.  jsonBuffer["Usermin13"] = Usermin13;
  96.  jsonBuffer["Usermax14"] = Usermax14;
  97.  jsonBuffer["Usermin14"] = Usermin14;
  98.  jsonBuffer["Usermax15"] = Usermax15;
  99.  jsonBuffer["Usermin15"] = Usermin15;
  100.  
  101.  // Serialize JSON to file
  102.  serializeJson(jsonBuffer, file);
  103.  file.close();
  104. }
  105.  
  106. void setup()
  107. {
  108.  Serial.begin(115200);
  109.  MyValue1 = random(1,99);
  110.  MyValue2 = random(1,99);
  111.  MyValue3 = random(1,99);
  112.  MyValue4 = random(1,99);
  113.  
  114.  Serial.println("\n\nMounting FS...\n");
  115.  if (!SPIFFS.begin())
  116.  {
  117.   Serial.println("erreur SPIFFS");
  118.   while (true); // on ne va pas plus loin
  119.  }
  120.  
  121.  if(!saveConfiguration)
  122.   { Serial.println(F("failed to Save...")); }
  123.  else
  124.  { Serial.println(F("Saved OK...")); }
  125.  
  126.  if(!loadConfiguration)
  127.   { Serial.println(F("Failed to load...")); }
  128.  else
  129.  { Serial.println(F("Load OK...")); }
  130. }
  131.  
  132. void loop() {
  133.   // put your main code here, to run repeatedly:
  134.  
  135. }
RAW Paste Data