Skip to content

Instantly share code, notes, and snippets.

@crosofg
Created September 12, 2016 11:30
Show Gist options
  • Save crosofg/2d865407b8f1ee4a33537b4b46cc4482 to your computer and use it in GitHub Desktop.
Save crosofg/2d865407b8f1ee4a33537b4b46cc4482 to your computer and use it in GitHub Desktop.
Sming Spiff Error
/*
* AppSettings.h
*
* Created on: 13 ìàÿ 2015 ã.
* Author: Anakod
*/
#include <SmingCore/SmingCore.h>
#ifndef INCLUDE_APPSETTINGS_H_
#define INCLUDE_APPSETTINGS_H_
#define APP_SETTINGS_FILE ".settings.conf" // leading point for security reasons :)
#define APP_SETTINGS_FILE_1 ".settings1.conf" // leading point for security reasons :)
struct ApplicationSettingsStorage {
String ssid;
String password;
String mqttUsername;
String mqttPassword;
String deviceID;
bool state = false;
void load() {
DynamicJsonBuffer jsonBuffer;
if (exist()) {
int size;
char* jsonString = new char[size + 1];
if (fileExist(APP_SETTINGS_FILE)) {
size = fileGetSize(APP_SETTINGS_FILE);
fileGetContent(APP_SETTINGS_FILE, jsonString, size + 1);
} else {
size = fileGetSize(APP_SETTINGS_FILE_1);
fileGetContent(APP_SETTINGS_FILE_1, jsonString, size + 1);
}
JsonObject& root = jsonBuffer.parseObject(jsonString);
JsonObject& network = root["network"];
ssid = network["ssid"].asString();
password = network["password"].asString();
mqttUsername = network["mqttUsername"].asString();
mqttPassword = network["mqttPassword"].asString();
deviceID = network["deviceID"].asString();
state = network["state"];
delete[] jsonString;
} else {
Serial.println("not prsent");
}
}
void save() {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonObject& network = jsonBuffer.createObject();
root["network"] = network;
network["ssid"] = ssid.c_str();
network["password"] = password.c_str();
network["mqttUsername"] = mqttUsername.c_str();
network["mqttPassword"] = mqttPassword.c_str();
network["deviceID"] = deviceID.c_str();
network["state"] = state;
// Make copy by value for temporary string objects
//TODO: add direct file stream writing
String rootString;
root.printTo(rootString);
Serial.println("Here 1");
if (fileExist(APP_SETTINGS_FILE)) {
Serial.println("Here 2");
fileSetContent(APP_SETTINGS_FILE, rootString);
} else {
Serial.println("Here 3");
fileSetContent(APP_SETTINGS_FILE_1, rootString); //<<<<<<<<<<<<<<<<<<<<<<<<<<<< This causes Error
}
}
bool exist() {
return fileExist(APP_SETTINGS_FILE) | fileExist(APP_SETTINGS_FILE_1);
}
};
static ApplicationSettingsStorage AppSettings;
#endif /* INCLUDE_APPSETTINGS_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment