Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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"];
- }
- 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);
- }
- 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;
- 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