Advertisement
noam76

jsonconfig1.h

Jul 23rd, 2021
1,396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void loadConfiguration() {
  2.  if (LittleFS.exists("/config.json")) {
  3.   //file exists, reading and loading
  4.   Serial.println("reading config file");
  5.   File configFile = LittleFS.open("/config.json", "r");
  6.   if (configFile) {
  7.    Serial.println("opened config file");
  8.    size_t size = configFile.size();
  9.    // Allocate a buffer to store contents of the file.
  10.    std::unique_ptr<char[]> buf(new char[size]);
  11.  
  12.    configFile.readBytes(buf.get(), size);
  13.    DynamicJsonDocument doc(512);
  14.    DeserializationError error = deserializeJson(doc, (buf.get()));
  15.    if (error)
  16.   Usermax0 = doc["Usermax0"];
  17.   Usermin0 = doc["Usermax0"];
  18.   }
  19.   else{
  20.     Serial.println("Failed to open config file");
  21.   }
  22.   configFile.close();
  23.   Serial.print("Usermax0 :");Serial.println(Usermax0);
  24.   Serial.print("Usermax0 :");Serial.println(Usermax0);
  25.  }
  26.  else{ Serial.println("The JSON file doesn't exist "); }
  27. }
  28.  
  29. void saveConfiguration() {
  30.  if (LittleFS.exists("/config.json")) {
  31.   //file exists, Saving configuration
  32.   Serial.println("saving config");
  33.   StaticJsonDocument<512> doc;
  34.   doc["Usermax0"] = Usermax0;
  35.   doc["Usermin0"] = Usermin0;
  36.   doc["Usermax1"] = Usermax1;
  37.   doc["Usermin1"] = Usermin1;
  38.   doc["Usermax2"] = Usermax2;
  39.   serializeJson(doc, Serial);
  40.   File configFile = LittleFS.open("/config.json", "w");
  41.   if (!configFile) { Serial.println("failed to open config file for writing"); }
  42.  }
  43.  else{
  44.   Serial.println("The JSON file doesn't exist ");
  45.  }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement