#include <ArduinoJson.h>
#include <FS.h>
/* Set these to desired credentials in runtime */
struct Config {
String ssid = "";
String pass = "";
bool hFlag = false;
};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("");
delay(1000);
//allows serving of files from SPIFFS
Serial.println("Mounting FS...");
if (!SPIFFS.begin()) {
Serial.println("Failed to mount file system");
return;
}
if (readConfigFile("/LaSt.json", false)) {
Serial.println("Config read");
} else {
Serial.println("Failed to read config");
}
}
bool readConfigFile(char* fileName, bool whichFile)
{
File configFile = SPIFFS.open(fileName, "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}
size_t size = configFile.size();
if (size > 1024) {
Serial.println("Config file size is too large");
return false;
}
// Allocate the memory pool on the stack.
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonBuffer<256> jsonBuffer;
// Parse the root object
JsonObject &root = jsonBuffer.parseObject(configFile);
if (!root.success())
Serial.println(F("Failed to read file, using default configuration"));
// Copy values from the JsonObject to the Config
Config.ssid = root["ssid"];
Config.pass = root["pass"];
if (whichFile)
{
Config.hFlag = root["hFlag"];
}
// We don't need the file anymore
configFile.close();
return true;
}
Compile gives error in Copy values from the JsonObject to the Config
section. Error: expected unqualified-id before '.' token
.
Want to read file from SPIFFS, and store in variable to use.