#include #include #include "FS.h" #include "SPIFFS.h" #include #define FORMAT_SPIFFS_IF_FAILED true void listDir(fs::FS &fs, const char * dirname, uint8_t levels){ Serial.printf("Listing SPIFFS directory: %s\r\n", dirname); File root = fs.open(dirname); if(!root){ Serial.println("- failed to open directory"); return; } if(!root.isDirectory()){ Serial.println(" - not a directory"); return; } File file = root.openNextFile(); while(file){ if(file.isDirectory()){ Serial.print(" DIR : "); Serial.println(file.name()); if(levels){ listDir(fs, file.name(), levels -1); } } else { Serial.print(" FILE: "); Serial.print(file.name()); Serial.print("\tSIZE: "); Serial.println(file.size()); } file = root.openNextFile(); } } std::string get_file_content(std::string filename){ if( SPIFFS.exists(filename.c_str()) ){ File file = SPIFFS.open(filename.c_str()); std::string file_str=""; if(!file){ Serial.println("Failed to open file for reading"); return ""; }else{ char buf[1024]; int siz = file.size(); while(siz > 0) { size_t len = std::min((int)(sizeof(buf) - 1), siz); file.read((uint8_t *)buf, len); buf[len] = 0; file_str += buf; siz -= sizeof(buf) - 1; } } file.close(); return file_str; }else{ return ""; } } void setup() { Serial.begin(115200); delay(500); Serial.println("Starting internal SPIFFS filesystem"); while ( !SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED) ) { // if we sit in this loop something is wrong; // if no existing spiffs partition exists one should be automagically created. Serial.println("SPIFFS Mount failed, this can happen on first-run initialisation"); Serial.println("If it happens repeatedly check if a SPIFFS partition is present for your board?"); delay(1000); } listDir(SPIFFS, "/", 0); //Read Json values https://arduinojson.org/v7/example/config/ struct MyConfig { const char* ssid; const char* ssid_password; bool ssid_dhcp; int port; }; MyConfig myconfig; if( SPIFFS.exists("/config.json") ){ // Open file for reading File file = SPIFFS.open("/config.json"); // Allocate a temporary JsonDocument // Don't forget to change the capacity to match your requirements. // Use arduinojson.org/v6/assistant to compute the capacity. StaticJsonDocument<512> doc; // Deserialize the JSON document DeserializationError error = deserializeJson(doc, file); if (error){ Serial.println(F("Failed to read file, using default configuration")); return; } // Copy values from the JsonDocument to the Config myconfig.ssid = doc["ssid"] | "No ssid"; myconfig.ssid_password = doc["ssid_password"] | "No ssid_password"; myconfig.ssid_dhcp = doc["ssid_dhcp"] | false; myconfig.port = doc["port"] | 0000; // Close the file (Curiously, File's destructor doesn't close the file) file.close(); } String ssid(myconfig.ssid); String ssid_password(myconfig.ssid_password); Serial.println(ssid); Serial.println(ssid_password); if(myconfig.ssid_dhcp) Serial.println("true"); else Serial.println("false"); int port=myconfig.port; Serial.println(port); //Create config file on flight MyConfig newconfig; if( SPIFFS.exists("/myconfig.json") ){ // Delete existing file, otherwise the configuration is appended to the file SPIFFS.remove("/myconfig.json"); } // Open file for writing File newfile = SPIFFS.open("/myconfig.json", FILE_WRITE); if (!newfile) { Serial.println(F("Failed to create file")); return; } // Allocate a temporary JsonDocument // Don't forget to change the capacity to match your requirements. // Use arduinojson.org/assistant to compute the capacity. StaticJsonDocument<256> newdoc; // Set the values in the document newdoc["ssid"] = "pwb_space_hello"; newdoc["ssid_password"] = "spacewalk657_hello"; newdoc["ssid_dhcp"] = false; newdoc["port"] = 9999; // Serialize JSON to file if (serializeJson(newdoc, newfile) == 0) { Serial.println(F("Failed to write to file")); } // Close the file newfile.close(); String ssid_new=newdoc["ssid"]; String ssid_password_new=newdoc["ssid_password"]; Serial.println(ssid_new); Serial.println(ssid_password_new); if(newdoc["ssid_dhcp"]) Serial.println("true"); else Serial.println("false"); int port_new=newdoc["port"]; Serial.println(port_new); std::string myconfig_str=get_file_content("/myconfig.json"); Serial.println(myconfig_str.c_str()); } void loop() { // put your main code here, to run repeatedly: }