Closed
Description
I try to load the configuration, the loading process works, but the configuration is not available.
If this is done in the memory, then it works.
What am I doing wrong or can not this be done with ArduinoJson?
Testcase 1:
- Loading from SPIFFS works
- cfgBuffer.parseObject works
Result: Configuration not loaded (see setup)
Testcase 2:
- createNestedObject... works
Result: Configuration loaded !
#include <ArduinoJson.h>
#include "FS.h"
#include "config.h"
DynamicJsonBuffer cfgBuffer;
JsonObject& cfg = cfgBuffer.createObject();
bool getConfig() {
bool result = SPIFFS.begin();
size_t n = 0;
if (result) {
// try to load the confiaguration data
File f = SPIFFS.open(APP_CONFIGFILE, "r");
if (f) {
size_t size = f.size();
if ( size == 0 ) {
f.close();
}else{
std::unique_ptr<char[]> buf (new char[size]);
f.readBytes(buf.get(), size);
JsonObject& cfg = cfgBuffer.parseObject(buf.get());
if (!cfg.success()) {
Serial.println("Parsing json failed !");
}
f.close();
n = cfg.size();
Serial.print("Configuration from SPIFFS loaded: ");
Serial.print(n);
Serial.println(" records found.");
n = 0;
}
}
}
if(n == 0){
// no data loaded, create the default
JsonObject &_wifi = cfg.createNestedObject("wifi");
_wifi["ssid"] = WIFI_SSID;
_wifi["password"] = WIFI_PASSWORD;
_wifi["hostname"] = "SMDK12";
JsonObject &_ntp = cfg.createNestedObject("ntp");
_ntp["enabled"] = false;
_ntp["server1"] = NTP_SERVER1;
_ntp["server2"] = NTP_SERVER2;
_ntp["server3"] = NTP_SERVER3;
_ntp["timeoffset"] = NTP_TIME_OFFSET;
_ntp["daylight"] = NTP_DAY_LIGHT;
_ntp["upinterval"] = NTP_UPDATE_INTERVAL;
_ntp["enabled"] = true;
}
return true;
}
void setup() {
Serial.begin(SERIAL_BAUD);
Serial.println();
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
if(getConfig()){
if(cfg.size()){
Serial.println("Configuration loaded !");
}else{
Serial.println("Configuration not loaded !");
}
}
}
void loop() {
}