Advertisement
noam76

config_json_file_new.h

May 2nd, 2020
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Save data choosing from the user by the WebForm after click submit in JSON file on the SPIFFS
  2.    Loading the data from the JSON file from the SPIFFS
  3. */
  4. void loadConfig()
  5. {
  6.   if (SPIFFS.exists("/config.json"))
  7.   {
  8.     //file exists, reading and loading
  9.     Serial.println("reading config file");
  10.     File configFile = SPIFFS.open("/config.json", "r");
  11.     if (configFile)
  12.     {
  13.       Serial.println("opened config file");
  14.       size_t size = configFile.size();
  15.       // Allocate a buffer to store contents of the file.
  16.       std::unique_ptr<char[]> buf(new char[size]);
  17.  
  18.       configFile.readBytes(buf.get(), size);
  19.       DynamicJsonBuffer jsonBuffer;
  20.       JsonObject& json = jsonBuffer.parseObject(buf.get());
  21.       // json.printTo(Serial); // print to serial the json file
  22.       // serializeJson(json, Serial); // print to serial the json file
  23.       if (json.success())
  24.       {
  25.         PlantS1 = json["TargetSoilPlant1"];
  26.         PlantS2 = json["TargetSoilPlant2"];
  27.         PlantS3 = json["TargetSoilPlant3"];
  28.         PlantS4 = json["TargetSoilPlant4"];
  29.         PlantS5 = json["TargetSoilPlant5"];
  30.         PlantS6 = json["TargetSoilPlant6"];
  31.         PlantS7 = json["TargetSoilPlant7"];
  32.       }
  33.       else {
  34.         Serial.println("failed to load json config");
  35.       }
  36.     }
  37.     else {
  38.       Serial.println("Failed to open config file");
  39.     }
  40.     configFile.close();
  41.     Serial.print("TargetSoilPlant1 :"); Serial.println(PlantS1);
  42.     Serial.print("TargetSoilPlant2 :"); Serial.println(PlantS2);
  43.     Serial.print("TargetSoilPlant3 :"); Serial.println(PlantS3);
  44.     Serial.print("TargetSoilPlant4 :"); Serial.println(PlantS4);
  45.     Serial.print("TargetSoilPlant5 :"); Serial.println(PlantS5);
  46.     Serial.print("TargetSoilPlant6 :"); Serial.println(PlantS6);
  47.     Serial.print("TargetSoilPlant7 :"); Serial.println(PlantS7);
  48.   }
  49.   else {
  50.     Serial.println("The JSON file doesn't exist ");
  51.   }
  52. }
  53.  
  54. void saveConfig()
  55. {
  56.   if (SPIFFS.exists("/config.json"))
  57.   {
  58.     SPIFFS.remove("/config.json");
  59.   }
  60.   //file exists, Saving configuration
  61.   Serial.println("saving config");
  62.   DynamicJsonBuffer jsonBuffer;
  63.   JsonObject& json = jsonBuffer.createObject();
  64.   json["TargetSoilPlant1"] = PlantS1;
  65.   json["TargetSoilPlant2"] = PlantS2;
  66.   json["TargetSoilPlant3"] = PlantS3;
  67.   json["TargetSoilPlant4"] = PlantS4;
  68.   json["TargetSoilPlant5"] = PlantS5;
  69.   json["TargetSoilPlant6"] = PlantS6;
  70.   json["TargetSoilPlant7"] = PlantS7;
  71.  
  72.   File configFile = SPIFFS.open("/config.json", "w");
  73.   if (!configFile)
  74.   {
  75.     Serial.println("failed to create file");
  76.     return;
  77.   }
  78.   json.prettyPrintTo(Serial);
  79.  
  80.   if (json.printTo(configFile) == 0)
  81.   {
  82.     Serial.println(F("Failed to write to file"));
  83.   }
  84.   configFile.close();
  85.   //end save
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement