Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Save data choosing from the user by the WebForm after click submit in JSON file on the SPIFFS
- * Loading the data from the JSON file from the SPIFFS
- */
- void loadConfig()
- {
- if (SPIFFS.exists("/config.json"))
- {
- //file exists, reading and loading
- Serial.println("reading config file");
- File configFile = SPIFFS.open("/config.json", "r");
- if (configFile)
- {
- Serial.println("opened config file");
- size_t size = configFile.size();
- // Allocate a buffer to store contents of the file.
- std::unique_ptr<char[]> buf(new char[size]);
- configFile.readBytes(buf.get(), size);
- DynamicJsonBuffer jsonBuffer;
- JsonObject& json = jsonBuffer.parseObject(buf.get());
- // json.printTo(Serial); // print to serial the json file
- if (json.success())
- {
- PlantS1 = json["TargetSoilPlant1"];
- PlantS2 = json["TargetSoilPlant2"];
- PlantS3 = json["TargetSoilPlant3"];
- PlantS4 = json["TargetSoilPlant4"];
- PlantS5 = json["TargetSoilPlant5"];
- PlantS6 = json["TargetSoilPlant6"];
- PlantS7 = json["TargetSoilPlant7"];
- }
- else{ Serial.println("failed to load json config"); }
- }
- else{ Serial.println("Failed to open config file"); }
- configFile.close();
- Serial.print("TargetSoilPlant1 :");Serial.println(PlantS1);
- Serial.print("TargetSoilPlant2 :");Serial.println(PlantS2);
- Serial.print("TargetSoilPlant3 :");Serial.println(PlantS3);
- Serial.print("TargetSoilPlant4 :");Serial.println(PlantS4);
- Serial.print("TargetSoilPlant5 :");Serial.println(PlantS5);
- Serial.print("TargetSoilPlant6 :");Serial.println(PlantS6);
- Serial.print("TargetSoilPlant7 :");Serial.println(PlantS7);
- }
- else{ Serial.println("The JSON file doesn't exist "); }
- }
- void saveConfig()
- {
- if (SPIFFS.exists("/config.json"))
- {
- //file exists, Saving configuration
- Serial.println("saving config");
- DynamicJsonBuffer jsonBuffer;
- JsonObject& json = jsonBuffer.createObject();
- json["TargetSoilPlant1"] = PlantS1;
- json["TargetSoilPlant2"] = PlantS2;
- json["TargetSoilPlant3"] = PlantS3;
- json["TargetSoilPlant4"] = PlantS4;
- json["TargetSoilPlant5"] = PlantS5;
- json["TargetSoilPlant6"] = PlantS6;
- json["TargetSoilPlant7"] = PlantS7;
- File configFile = SPIFFS.open("/config.json", "w");
- if (!configFile) { Serial.println("failed to open config file for writing"); }
- // json.prettyPrintTo(Serial);
- json.printTo(configFile);
- if (json.printTo(configFile) == 0)
- { Serial.println(F("Failed to write to file")); }
- configFile.close();
- //end save
- }
- else{ Serial.println("The JSON file doesn't exist "); }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement