Advertisement
noam76

config_json_file.h

Feb 23rd, 2018
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  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.    if (json.success())
  23.    {
  24.     PlantS1 = json["TargetSoilPlant1"];
  25.     PlantS2 = json["TargetSoilPlant2"];
  26.     PlantS3 = json["TargetSoilPlant3"];
  27.     PlantS4 = json["TargetSoilPlant4"];
  28.     PlantS5 = json["TargetSoilPlant5"];
  29.     PlantS6 = json["TargetSoilPlant6"];
  30.     PlantS7 = json["TargetSoilPlant7"];
  31.    }
  32.    else{ Serial.println("failed to load json config"); }
  33.   }
  34.   else{ Serial.println("Failed to open config file"); }
  35.   configFile.close();
  36.   Serial.print("TargetSoilPlant1 :");Serial.println(PlantS1);
  37.   Serial.print("TargetSoilPlant2 :");Serial.println(PlantS2);
  38.   Serial.print("TargetSoilPlant3 :");Serial.println(PlantS3);
  39.   Serial.print("TargetSoilPlant4 :");Serial.println(PlantS4);
  40.   Serial.print("TargetSoilPlant5 :");Serial.println(PlantS5);
  41.   Serial.print("TargetSoilPlant6 :");Serial.println(PlantS6);
  42.   Serial.print("TargetSoilPlant7 :");Serial.println(PlantS7);
  43.  }
  44.  else{ Serial.println("The JSON file doesn't exist "); }
  45. }
  46.  
  47. void saveConfig()
  48. {
  49.  if (SPIFFS.exists("/config.json"))
  50.  {
  51.   //file exists, Saving configuration
  52.   Serial.println("saving config");
  53.   DynamicJsonBuffer jsonBuffer;
  54.   JsonObject& json = jsonBuffer.createObject();
  55.   json["TargetSoilPlant1"] = PlantS1;
  56.   json["TargetSoilPlant2"] = PlantS2;
  57.   json["TargetSoilPlant3"] = PlantS3;
  58.   json["TargetSoilPlant4"] = PlantS4;
  59.   json["TargetSoilPlant5"] = PlantS5;
  60.   json["TargetSoilPlant6"] = PlantS6;
  61.   json["TargetSoilPlant7"] = PlantS7;
  62.    
  63.   File configFile = SPIFFS.open("/config.json", "w");
  64.   if (!configFile) { Serial.println("failed to open config file for writing"); }
  65.   // json.prettyPrintTo(Serial);
  66.   json.printTo(configFile);
  67.  
  68.   if (json.printTo(configFile) == 0)
  69.    { Serial.println(F("Failed to write to file")); }
  70.   configFile.close();
  71.   //end save
  72.  }
  73.  else{ Serial.println("The JSON file doesn't exist "); }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement