From b67674672317a3b0c8a1a30953f452134f0cc4b7 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Sun, 26 Aug 2018 18:58:37 +0200 Subject: [PATCH] read config from json on SPIFFS --- data/config.json | 10 +++++ src/JsonStruct.h | 65 +++++++++++++++++++++++++++++---- src/MeshNet.cpp | 17 +++++++-- src/MeshNet.h | 6 +-- src/Network.h | 1 - src/Sprocket.cpp | 6 +++ src/Sprocket.h | 2 +- src/base/MeshSprocket.h | 5 +-- src/base/MeshSprocketConfig.h | 69 ++++++++++++++--------------------- src/examples/mesh/config.h | 6 +-- src/examples/mesh/main.cpp | 9 +++-- 11 files changed, 129 insertions(+), 67 deletions(-) create mode 100644 data/config.json diff --git a/data/config.json b/data/config.json new file mode 100644 index 0000000..0bb8956 --- /dev/null +++ b/data/config.json @@ -0,0 +1,10 @@ +{ + "stationMode": 1, + "channel": 11, + "meshPort": 5555, + "meshSSID": "MyMesh", + "meshPassword": "th3r31sn0sp00n", + "stationSSID": "MyAP", + "stationPassword": "myApPassword", + "hostname": "mesh-node" +} \ No newline at end of file diff --git a/src/JsonStruct.h b/src/JsonStruct.h index 5a65912..9924a08 100644 --- a/src/JsonStruct.h +++ b/src/JsonStruct.h @@ -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 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 \ No newline at end of file diff --git a/src/MeshNet.cpp b/src/MeshNet.cpp index 7c9c30f..3152fda 100644 --- a/src/MeshNet.cpp +++ b/src/MeshNet.cpp @@ -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; } diff --git a/src/MeshNet.h b/src/MeshNet.h index 07d1e4f..6996b1c 100644 --- a/src/MeshNet.h +++ b/src/MeshNet.h @@ -11,6 +11,7 @@ #include #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(); diff --git a/src/Network.h b/src/Network.h index 93a65ea..d559959 100644 --- a/src/Network.h +++ b/src/Network.h @@ -9,7 +9,6 @@ typedef std::function 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(); }; diff --git a/src/Sprocket.cpp b/src/Sprocket.cpp index 55eab57..b720c18 100644 --- a/src/Sprocket.cpp +++ b/src/Sprocket.cpp @@ -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); diff --git a/src/Sprocket.h b/src/Sprocket.h index 9d3d715..93b0f83 100644 --- a/src/Sprocket.h +++ b/src/Sprocket.h @@ -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); diff --git a/src/base/MeshSprocket.h b/src/base/MeshSprocket.h index 5fa77bf..bc25570 100644 --- a/src/base/MeshSprocket.h +++ b/src/base/MeshSprocket.h @@ -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); } diff --git a/src/base/MeshSprocketConfig.h b/src/base/MeshSprocketConfig.h index c77d720..14f89fc 100644 --- a/src/base/MeshSprocketConfig.h +++ b/src/base/MeshSprocketConfig.h @@ -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); }; }; diff --git a/src/examples/mesh/config.h b/src/examples/mesh/config.h index 7f87652..66a2092 100644 --- a/src/examples/mesh/config.h +++ b/src/examples/mesh/config.h @@ -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 diff --git a/src/examples/mesh/main.cpp b/src/examples/mesh/main.cpp index b8b8749..e8dabc6 100644 --- a/src/examples/mesh/main.cpp +++ b/src/examples/mesh/main.cpp @@ -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);