Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FS.h>
- #include <ArduinoJson.h>
- int MyValue1,MyValue2,MyValue3,MyValue4;
- bool loadConfiguration()
- {
- File file = SPIFFS.open("/config.json", "r");
- if (!file)
- {
- Serial.println("Failed to open config file");
- return false;
- }
- StaticJsonBuffer<512> jsonBuffer;
- // Parse the root object
- JsonObject &root = jsonBuffer.parseObject(file);
- if (!root.success())
- Serial.println(F("Failed to read file, using default configuration"));
- // Copy values from the JsonObject to the Config
- MyValue1 = root["Value1"];
- MyValue2 = root["Value2"];
- MyValue3 = root["Value3"];
- MyValue4 = root["Value4"];
- // Close the file (File's destructor doesn't close the file)
- file.close();
- }
- bool saveConfiguration()
- {
- File file = SPIFFS.open("/config.json", "w");
- if (!file)
- {
- Serial.println("Failed to open config file for writing");
- return false;
- }
- StaticJsonBuffer<256> jsonBuffer;
- // Parse the root object
- JsonObject &root = jsonBuffer.createObject();
- // Set the values
- root["Value1"] = MyValue1;
- root["Value2"] = MyValue2;
- root["Value3"] = MyValue3;
- root["Value4"] = MyValue4;
- // Serialize JSON to file
- if (root.printTo(file) == 0)
- {
- Serial.println(F("Failed to write to file"));
- }
- file.close();
- }
- void setup()
- {
- Serial.begin(115200);
- MyValue1 = random(1,99);
- MyValue2 = random(1,99);
- MyValue3 = random(1,99);
- MyValue4 = random(1,99);
- Serial.println("\n\nMounting FS...\n");
- if (!SPIFFS.begin())
- {
- Serial.println("erreur SPIFFS");
- while (true); // on ne va pas plus loin
- }
- if(!saveConfiguration)
- { Serial.println(F("failed to Save...")); }
- else
- { Serial.println(F("Saved OK...")); }
- if(!loadConfiguration)
- { Serial.println(F("Failed to load...")); }
- else
- { Serial.println(F("Load OK...")); }
- }
- ______________________________________________________________________________________________________________
- ***************************************
- Code Example Using JSON
- https://github.com/tzapu/WiFiManager/blob/master/examples/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino#L7
- *****************************************
- #include <FS.h>
- #include <ArduinoJson.h>
- int MyValue1,MyValue2,MyValue3,MyValue4;
- //Save parameters to JSON file
- void saveConfiguration()
- {
- if (SPIFFS.exists("/config.json"))
- {
- //file exists, Saving configuration
- Serial.println("saving config");
- DynamicJsonBuffer jsonBuffer;
- JsonObject& json = jsonBuffer.createObject();
- //json["mqtt_server"] = mqtt_server;
- //json["mqtt_port"] = mqtt_port;
- //json["blynk_token"] = blynk_token;
- json["MyValue1"] = MyValue1;
- json["MyValue2"] = MyValue2;
- json["MyValue3"] = MyValue3;
- json["MyValue4"] = MyValue4;
- 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 "); }
- }
- //Load parameters from JSON file
- void loadConfiguration()
- {
- 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);
- if (json.success())
- {
- //strcpy(mqtt_server, json["mqtt_server"]); // char string_c
- //static_ip = json["ip"];
- MyValue1 = json["MyValue1"];
- MyValue2 = json["MyValue2"];
- MyValue3 = json["MyValue3"];
- MyValue4 = json["MyValue4"];
- //strcpy(MyValue1, json["MyValue1"]); // char string_c
- //static_gw = json["gateway"];
- /*Serial.println("converting ip");
- IPAddress ip = ipFromCharArray(static_ip);
- Serial.println(ip);*/
- //MyValue1 = ipFromCharArray(MyValue1);
- }
- else{ Serial.println("failed to load json config"); }
- }
- else{ Serial.println("Failed to open config file"); }
- configFile.close();
- Serial.print("MyValue1 :");Serial.println(MyValue1);
- Serial.print("MyValue2: ");Serial.println(MyValue2);
- Serial.print("MyValue3: ");Serial.println(MyValue3);
- Serial.print("MyValue4: ");Serial.println(MyValue4);
- }
- else{ Serial.println("The JSON file doesn't exist "); }
- }
- void setup()
- {
- // put your setup code here, to run once:
- Serial.begin(115200);
- Serial.println();
- //read configuration from FS json
- Serial.println("mounting FS...");
- if (SPIFFS.begin())
- { Serial.println("mounted file system"); }
- else { Serial.println("failed to mount FS"); }
- saveConfiguration();
- loadConfiguration();
- //Serial.println("local ip");
- //Serial.println(WiFi.localIP());
- //Serial.println(WiFi.gatewayIP());
- //Serial.println(WiFi.subnetMask());
- }
- void loop()
- {
- // put your main code here, to run repeatedly:
- }
Add Comment
Please, Sign In to add comment