read config from json on SPIFFS

This commit is contained in:
2018-08-26 18:58:37 +02:00
parent 5334d58433
commit b676746723
11 changed files with 129 additions and 67 deletions

10
data/config.json Normal file
View File

@@ -0,0 +1,10 @@
{
"stationMode": 1,
"channel": 11,
"meshPort": 5555,
"meshSSID": "MyMesh",
"meshPassword": "th3r31sn0sp00n",
"stationSSID": "MyAP",
"stationPassword": "myApPassword",
"hostname": "mesh-node"
}

View File

@@ -12,16 +12,29 @@ struct JsonStruct {
// ------------------------------------------------------------------------------------------
virtual void mapJsonObject(JsonObject& json);
virtual void fromJsonObject(JsonObject& json);
virtual int verifyJsonObject(JsonObject& json){
return json.success();
//&& json.containsKey(JSON_DOMAIN)
};
virtual String toJsonString();
String toJsonString(){
//StaticJsonBuffer<200> StringjsonBuffer;
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
JsonObject& root = jsonBuffer.createObject();
mapJsonObject(root);
String jsonString;
root.printTo(jsonString);
return jsonString;
}
String getAttrFromJson(JsonObject& json, const char* attr){
if(json.containsKey(attr)){
return json[attr];
if(json.containsKey(String(attr))){
const char *value = json[attr];
return String(value);
//return json[attr];
}
return "";
return "no value";
}
int getIntAttrFromJson(JsonObject& json, const char* attr){
if(json.containsKey(attr)){
@@ -30,14 +43,52 @@ struct JsonStruct {
return 0;
}
// Map a json object to this struct.
virtual int fromJsonObject(JsonObject& json);
// Parse a json string and map parsed object
int fromJsonString(String& str){
void fromJsonString(String& str){
//StaticJsonBuffer<200> jsonBuffer;
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
JsonObject& json = jsonBuffer.parseObject(str);
return fromJsonObject(json);
valid = verifyJsonObject(json);
if(valid) {
fromJsonObject(json);
}
};
void fromFile(const char* path) {
Serial.println("read json");
File configFile = SPIFFS.open(path, "r");
String cfgFileStr = configFile.readString();
// Allocate a buffer to store contents of the file.
//size_t size = configFile.size();
//std::unique_ptr<char[]> buf(new char[size]);
//configFile.readBytes(buf.get(), size);
//StaticJsonBuffer<1024> jsonBuffer;
//JsonObject& json = jsonBuffer.parseObject(buf.get());
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(cfgFileStr);
valid = verifyJsonObject(json);
if(configFile) {
Serial.println("map json object");
fromJsonObject(json);
}
if(!valid){
Serial.println("read json failed");
}
configFile.close();
}
void saveFile(const char* path) {
File configFile = SPIFFS.open(path, "w");
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
JsonObject& json = jsonBuffer.createObject();
valid = configFile && verifyJsonObject(json);
if(valid){
mapJsonObject(json);
json.printTo(configFile);
}
}
};
#endif

View File

@@ -1,12 +1,23 @@
#include "MeshNet.h"
MeshNet::MeshNet(MeshConfig cfg) : Network() {
config = cfg;
config.stationMode = cfg.stationMode;
config.channel = cfg.channel;
config.meshPort = cfg.meshPort;
config.meshSSID = cfg.meshSSID;
config.meshPassword = cfg.meshPassword;
config.stationSSID = cfg.stationSSID;
config.stationPassword = cfg.stationPassword;
config.hostname = cfg.hostname;
config.debugTypes = cfg.debugTypes;
}
Network* MeshNet::init(){
Serial.println("init mesh");
config.fromFile("/config.json");
Serial.println(config.meshSSID);
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes( config.debugTypes );
mesh.init( config.meshSSID, config.meshPassword, scheduler, config.meshPort, WIFI_AP_STA, config.channel );
@@ -23,7 +34,7 @@ Network* MeshNet::connectStation(int doConnect) {
if(doConnect){
Serial.println("connect station");
mesh.stationManual(config.stationSSID, config.stationPassword);
mesh.setHostname(config.hostname);
mesh.setHostname(config.hostname.c_str());
}
return this;
}

View File

@@ -11,6 +11,7 @@
#include <WiFiClient.h>
#include "Network.h"
#include "MeshConfig.h"
#include "base/MeshSprocketConfig.h"
using namespace std;
using namespace std::placeholders;
@@ -18,12 +19,11 @@ using namespace std::placeholders;
class MeshNet : public Network {
public:
painlessMesh mesh;
MeshConfig config;
MeshSprocketConfig config;
MeshNet(MeshConfig cfg);
Network* init();
Network* connectStation(int);
void configure(MeshSprocketConfig cfg);
void update();
void newConnectionCallback(uint32_t nodeId);
void changedConnectionCallback();

View File

@@ -9,7 +9,6 @@ typedef std::function<void(uint32_t from, String &msg)> msgReceived_cb;
class Network {
public:
uint32_t id = 0;
Network(){}
Scheduler* scheduler;
virtual Network* init() { return this; };
virtual Network* init(Scheduler* s) { scheduler = s; return init(); };

View File

@@ -42,6 +42,12 @@ void Sprocket::loop(){
scheduler.execute();
}
void Sprocket::dispatch( uint32_t from, String &msg ) {
MeshMessage mMsg;
if(mMsg.fromJsonString(msg)){
dispatchMessageToPlugins(mMsg);
}
}
void Sprocket::addPlugin(Plugin* p){
plugins.reserve(1);

View File

@@ -33,7 +33,7 @@ class Sprocket {
virtual Sprocket* activate();
virtual Sprocket* activate(Scheduler*) { return this; }
virtual Sprocket* activate(Scheduler*, Network*);
virtual void dispatch( uint32_t from, String &msg ) {}
virtual void dispatch( uint32_t from, String &msg );
void addPlugin(Plugin* p);
void setupPlugins(Scheduler* scheduler, Network* network);

View File

@@ -34,10 +34,7 @@ class MeshSprocket : public Sprocket {
};
void dispatch( uint32_t from, String &msg ) {
MeshMessage mMsg;
if(mMsg.fromJsonString(msg)){
dispatchMessageToPlugins(mMsg);
}
Sprocket::dispatch(from, msg);
onMessage(from, msg);
}

View File

@@ -17,52 +17,39 @@
#define JSON_stationPassword "stationPassword"
#define JSON_hostname "hostname"
#define JSON_startupDelay "startupDelay"
#define JSON_serialBaudRate "serialBaudRate"
struct MeshSprocketConfig : public JsonStruct {
SprocketConfig sprocket;
MeshConfig mesh;
int valid = 0;
int stationMode;
int channel;
int meshPort;
String meshSSID;
String meshPassword;
String stationSSID;
String stationPassword;
String hostname;
uint16_t debugTypes;
// ------------------------------------------------------------------------------------------
String toJsonString(){
//StaticJsonBuffer<200> jsonBuffer;
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
JsonObject& root = jsonBuffer.createObject();
root[JSON_stationMode] = mesh.stationMode;
root[JSON_channel] = mesh.channel;
root[JSON_meshPort] = mesh.meshPort;
root[JSON_meshSSID] = mesh.meshSSID;
root[JSON_meshPassword] = mesh.meshPassword;
root[JSON_stationSSID] = mesh.stationSSID;
root[JSON_stationPassword] = mesh.stationPassword;
root[JSON_hostname] = mesh.hostname;
root[JSON_startupDelay] = sprocket.startupDelay;
root[JSON_serialBaudRate] = sprocket.serialBaudRate;
String jsonString;
root.printTo(jsonString);
return jsonString;
void mapJsonObject(JsonObject& root) {
root[JSON_stationMode] = stationMode;
root[JSON_channel] = channel;
root[JSON_meshPort] = meshPort;
root[JSON_meshSSID] = meshSSID;
root[JSON_meshPassword] = meshPassword;
root[JSON_stationSSID] = stationSSID;
root[JSON_stationPassword] = stationPassword;
root[JSON_hostname] = hostname;
}
// Map a json object to this struct.
int fromJsonObject(JsonObject& json){
if(!verifyJsonObject(json)){
Serial.println("ERROR: cannot parse JSON object");
valid = 0;
return valid;
}
mesh.stationMode = getIntAttrFromJson(json, JSON_stationMode);
mesh.channel = getIntAttrFromJson(json, JSON_channel);
mesh.meshPort = getIntAttrFromJson(json, JSON_meshPort);
mesh.meshSSID = getAttrFromJson(json, JSON_meshSSID).c_str();
mesh.meshPassword = getAttrFromJson(json, JSON_meshPassword).c_str();
mesh.stationSSID = getAttrFromJson(json, JSON_stationSSID).c_str();
mesh.stationPassword = getAttrFromJson(json, JSON_stationPassword).c_str();
mesh.hostname = getAttrFromJson(json, JSON_hostname).c_str();
sprocket.startupDelay = getIntAttrFromJson(json, JSON_startupDelay);
sprocket.serialBaudRate = getIntAttrFromJson(json, JSON_serialBaudRate);
valid = 1;
return valid;
void fromJsonObject(JsonObject& json) {
stationMode = getIntAttrFromJson(json, JSON_stationMode);
channel = getIntAttrFromJson(json, JSON_channel);
meshPort = getIntAttrFromJson(json, JSON_meshPort);
meshSSID = getAttrFromJson(json, JSON_meshSSID);
meshPassword = getAttrFromJson(json, JSON_meshPassword);
stationSSID = getAttrFromJson(json, JSON_stationSSID);
stationPassword = getAttrFromJson(json, JSON_stationPassword);
hostname = getAttrFromJson(json, JSON_hostname);
};
};

View File

@@ -15,10 +15,10 @@
#define MESH_PORT 5555
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define STATION_SSID "tErAx1d"
#define STATION_PASSWORD "ramalamadingdong"
#define STATION_SSID "Th1ngs4p"
#define STATION_PASSWORD "th3r31sn0sp00n"
#define HOSTNAME "mesh-node"
#define MESH_DEBUG_TYPES ERROR | STARTUP
#define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION
//ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE
// OTA config

View File

@@ -2,16 +2,17 @@
#include "MeshNet.h"
#include "MeshApp.cpp"
MeshApp sprocket(
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
{ OTA_PORT, OTA_PASSWORD }
);
MeshNet net({
SPROCKET_MODE, WIFI_CHANNEL,
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
STATION_SSID, STATION_PASSWORD, HOSTNAME,
MESH_DEBUG_TYPES
});
MeshApp sprocket(
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
{ OTA_PORT, OTA_PASSWORD }
);
void setup() {
sprocket.join(net);