noam76

Spiffs Json

Feb 8th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.24 KB | None | 0 0
  1. #include <FS.h>
  2. #include <ArduinoJson.h>
  3. int MyValue1,MyValue2,MyValue3,MyValue4;
  4.  
  5. bool loadConfiguration()
  6. {
  7.  File file = SPIFFS.open("/config.json", "r");
  8.  if (!file)
  9.  {
  10.   Serial.println("Failed to open config file");
  11.   return false;
  12.  }
  13.  
  14.   StaticJsonBuffer<512> jsonBuffer;
  15.  
  16.   // Parse the root object
  17.   JsonObject &root = jsonBuffer.parseObject(file);
  18.  
  19.   if (!root.success())
  20.     Serial.println(F("Failed to read file, using default configuration"));
  21.  
  22.   // Copy values from the JsonObject to the Config
  23.  MyValue1 = root["Value1"];
  24.  MyValue2 = root["Value2"];
  25.  MyValue3 = root["Value3"];
  26.  MyValue4 = root["Value4"];
  27.  
  28.   // Close the file (File's destructor doesn't close the file)
  29.   file.close();
  30. }
  31.  
  32. bool saveConfiguration()
  33. {
  34.  File file = SPIFFS.open("/config.json", "w");
  35.  if (!file)
  36.  {
  37.   Serial.println("Failed to open config file for writing");
  38.   return false;
  39.  }
  40.  
  41.  StaticJsonBuffer<256> jsonBuffer;
  42.  
  43.  // Parse the root object
  44.  JsonObject &root = jsonBuffer.createObject();
  45.  
  46.  // Set the values
  47.  root["Value1"] = MyValue1;
  48.  root["Value2"] = MyValue2;
  49.  root["Value3"] = MyValue3;
  50.  root["Value4"] = MyValue4;
  51.  
  52.  // Serialize JSON to file
  53.  if (root.printTo(file) == 0)
  54.  {
  55.   Serial.println(F("Failed to write to file"));
  56.  }
  57.  file.close();
  58. }
  59.  
  60. void setup()
  61. {
  62.  Serial.begin(115200);
  63.  MyValue1 = random(1,99);
  64.  MyValue2 = random(1,99);
  65.  MyValue3 = random(1,99);
  66.  MyValue4 = random(1,99);
  67.  
  68.  Serial.println("\n\nMounting FS...\n");
  69.  if (!SPIFFS.begin())
  70.  {
  71.   Serial.println("erreur SPIFFS");
  72.   while (true); // on ne va pas plus loin
  73.  }
  74.  
  75.  if(!saveConfiguration)
  76.   { Serial.println(F("failed to Save...")); }
  77.  else
  78.  { Serial.println(F("Saved OK...")); }
  79.  
  80.  if(!loadConfiguration)
  81.   { Serial.println(F("Failed to load...")); }
  82.  else
  83.  { Serial.println(F("Load OK...")); }
  84. }
  85.  
  86. ______________________________________________________________________________________________________________
  87. ***************************************
  88. Code Example Using JSON
  89. https://github.com/tzapu/WiFiManager/blob/master/examples/AutoConnectWithFSParametersAndCustomIP/AutoConnectWithFSParametersAndCustomIP.ino#L7
  90. *****************************************
  91. #include <FS.h>
  92. #include <ArduinoJson.h>
  93.  
  94. int MyValue1,MyValue2,MyValue3,MyValue4;
  95.  
  96. //Save parameters to JSON file
  97. void saveConfiguration()
  98. {
  99.  if (SPIFFS.exists("/config.json"))
  100.  {
  101.   //file exists, Saving configuration
  102.   Serial.println("saving config");
  103.   DynamicJsonBuffer jsonBuffer;
  104.   JsonObject& json = jsonBuffer.createObject();
  105.   //json["mqtt_server"] = mqtt_server;
  106.   //json["mqtt_port"] = mqtt_port;
  107.   //json["blynk_token"] = blynk_token;
  108.   json["MyValue1"] = MyValue1;
  109.   json["MyValue2"] = MyValue2;
  110.   json["MyValue3"] = MyValue3;
  111.   json["MyValue4"] = MyValue4;
  112.    
  113.   File configFile = SPIFFS.open("/config.json", "w");
  114.   if (!configFile) { Serial.println("failed to open config file for writing"); }
  115.   json.prettyPrintTo(Serial);
  116.   json.printTo(configFile);
  117.  
  118.   if (json.printTo(configFile) == 0)
  119.    { Serial.println(F("Failed to write to file")); }
  120.   configFile.close();
  121.   //end save
  122.  }
  123.  else{ Serial.println("The JSON file doesn't exist "); }
  124. }
  125.  
  126. //Load parameters from JSON file
  127. void loadConfiguration()
  128. {
  129.  if (SPIFFS.exists("/config.json"))
  130.  {
  131.   //file exists, reading and loading
  132.   Serial.println("reading config file");
  133.   File configFile = SPIFFS.open("/config.json", "r");
  134.   if (configFile)
  135.   {
  136.    Serial.println("opened config file");
  137.    size_t size = configFile.size();
  138.    // Allocate a buffer to store contents of the file.
  139.    std::unique_ptr<char[]> buf(new char[size]);
  140.  
  141.    configFile.readBytes(buf.get(), size);
  142.    DynamicJsonBuffer jsonBuffer;
  143.    JsonObject& json = jsonBuffer.parseObject(buf.get());
  144.    json.printTo(Serial);
  145.    
  146.    if (json.success())
  147.    {
  148.     //strcpy(mqtt_server, json["mqtt_server"]); // char string_c
  149.     //static_ip = json["ip"];
  150.     MyValue1 = json["MyValue1"];
  151.     MyValue2 = json["MyValue2"];
  152.     MyValue3 = json["MyValue3"];
  153.     MyValue4 = json["MyValue4"];
  154.     //strcpy(MyValue1, json["MyValue1"]); // char string_c
  155.     //static_gw = json["gateway"];
  156.     /*Serial.println("converting ip");
  157.     IPAddress ip = ipFromCharArray(static_ip);
  158.     Serial.println(ip);*/
  159.     //MyValue1 = ipFromCharArray(MyValue1);
  160.    }
  161.    else{ Serial.println("failed to load json config"); }
  162.   }
  163.   else{ Serial.println("Failed to open config file"); }
  164.   configFile.close();
  165.   Serial.print("MyValue1 :");Serial.println(MyValue1);
  166.   Serial.print("MyValue2: ");Serial.println(MyValue2);
  167.   Serial.print("MyValue3: ");Serial.println(MyValue3);
  168.   Serial.print("MyValue4: ");Serial.println(MyValue4);
  169.  }
  170.  else{ Serial.println("The JSON file doesn't exist "); }
  171. }
  172.  
  173. void setup()
  174. {
  175.  // put your setup code here, to run once:
  176.  Serial.begin(115200);
  177.  Serial.println();
  178.  //read configuration from FS json
  179.  Serial.println("mounting FS...");
  180.  
  181.  if (SPIFFS.begin())
  182.   { Serial.println("mounted file system"); }
  183.  else { Serial.println("failed to mount FS"); }
  184.  
  185.  saveConfiguration();
  186.  loadConfiguration();
  187.  
  188.   //Serial.println("local ip");
  189.   //Serial.println(WiFi.localIP());
  190.   //Serial.println(WiFi.gatewayIP());
  191.   //Serial.println(WiFi.subnetMask());
  192. }
  193.  
  194. void loop()
  195. {
  196.   // put your main code here, to run repeatedly:
  197. }
Add Comment
Please, Sign In to add comment