SergiuToporjinschi / settingsmanager

Saving, reading and changing settings from Json file on SPIFFS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GitHub repo size in bytes GitHub last commit GitHub stars GitHub watchers GitHub license Code Climate Build Status

Settings Manager

Intarface for reading, writing and managing settings in JSON format on SPIFFS.

Dependency

Compile macros

Settings file

{
 "esp" : {
	"delayTime": 300
 },
 "wlan": {
	"hostName": "hostName of esp",
	"ssid": "network sidd ",
	"password": "network password"
 },
 "mqtt": {
	"clientId": "mqttClientid",
	"server": "127.0.0.1",
	"port": "9999",
	"user": "mqttUser",
	"password": "mqttPassword",
	"status": {
		"topic": "topic/for/status"
	},
	"internalTopics" : {
		"settings": "Settings/topic",
		"cmd": "cmd/topic"
	}
 },
 "ledPin":13,
 "sketchVersion":"1.0",
 "updateServer":"http://testUpdateServer.com",
 "process": {
	"topic": "other/topic",
	"interval": 1000
 }
}

Syntax

For getting value o a key you can call sm.getInt(<path/key>[, defaultValue]) The default value will be returned if the path/key has not been found.

For changing value of a key sm.setInt(<path/key>, <value>) will return SM_SUCCESS if the key has been found and changed; SM_KEY_NOT_FOUND if the key has not been found;

See SettingsManager.h for more information.

Examples

Reading/loading settings from file;

#include "SettingsManager.h"

SettingsManager sm; //Initialize

void setup() {
    Serial.begin(115200);
    sm.readSettings("/config.json"); //Loading json from file config.json
}

void loop(){
    Serial.println("int ledPin: "); 
    Serial.print(sm.getInt("ledPin", 99)); //get the int value from json root
    Serial.println("Test getJsonVariant");
    
    JsonObject vr = sm.getJsonVariant("mqtt.status");
    if (!vr.isNull()) {
        serializeJsonPretty(vr, Serial);
        Serial.println("");
    } else {
        Serial.println("no variant");
    }
}

Loading json from a string

#include "SettingsManager.h"

SettingsManager sm; //Initialize

void setup() {
    Serial.begin(115200);
    sm.readSettings("/config.json"); //Loading json from file config.json
}

void loop(){
    sm.loadJson("{\"testInLoadingJson\":\"this is a string value\",\"Level1\":{\"Level2Key\":12,\"Level2Test\":true,\"Level3\":{\"float\":23.5}}}");
    JsonObject ob = sm.getRoot();
    if (!ob.isNull()) {
        serializeJsonPretty(ob, Serial);
        Serial.println("");
    } else {
        Serial.println("loadSetti: no ob");
    }
}

Change value of a key

#include "SettingsManager.h"

SettingsManager sm; //Initialize

void setup() {
    Serial.begin(115200);
    sm.readSettings("/config.json"); //Loading json from file config.json
}

void loop(){
    int res = sm.setString("updateServer", "Other Test");
    sm.setInt("ledPin", 15);
    sm.writeSettings("/config.json");
}

About

Saving, reading and changing settings from Json file on SPIFFS

License:GNU General Public License v3.0


Languages

Language:C++ 88.7%Language:Python 10.8%Language:C 0.5%