struct WiFiConfig
{
char ssid[32] = DEF_WIFI;
char pass[15] = DEF_PASS;
char ntp[32] = DEF_NTP;
uint8_t tz = DEF_TZ;
};
...
void loadWiFiCFG()
{
File file = FFat.open(WIFI_CFG_FILE, "r");
if (file)
{
StaticJsonDocument<300> doc;
DeserializationError error = deserializeJson(doc, file);
file.close();
if (error)
{
Serial.println("Error loading cfg");
setDefWiFiCFG();
}
else
{
strlcpy(wifi_cfg.ssid, doc["wifiname"], sizeof(wifi_cfg.ssid));
strlcpy(wifi_cfg.pass, doc["pass"], sizeof(wifi_cfg.pass));
strlcpy(wifi_cfg.ntp, doc["ntp"], sizeof(wifi_cfg.ntp));
wifi_cfg.tz = doc["tz"];
}
}
Serial.println(wifi_cfg.ssid);
Serial.println(wifi_cfg.pass);
Serial.println(wifi_cfg.ntp);
Serial.println(wifi_cfg.tz);
}
...
bool saveWiFiCfg(const String filename)
{
FFat.remove(filename);
File file = FFat.open(filename, "w");
if (!file) return false;
StaticJsonDocument<300> doc;
doc["wifiname"] = wifi_cfg.ssid;
doc["pass"] = wifi_cfg.pass;
doc["ntp"] = wifi_cfg.ntp;
doc["tz"] = String(wifi_cfg.tz);
bool r = (serializeJson(doc, file) != 0);
file.flush();
file.close();
return r;
}