mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-15 21:18:21 +01:00
read config from json on SPIFFS
This commit is contained in:
10
data/config.json
Normal file
10
data/config.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"stationMode": 1,
|
||||||
|
"channel": 11,
|
||||||
|
"meshPort": 5555,
|
||||||
|
"meshSSID": "MyMesh",
|
||||||
|
"meshPassword": "th3r31sn0sp00n",
|
||||||
|
"stationSSID": "MyAP",
|
||||||
|
"stationPassword": "myApPassword",
|
||||||
|
"hostname": "mesh-node"
|
||||||
|
}
|
||||||
@@ -12,16 +12,29 @@ struct JsonStruct {
|
|||||||
|
|
||||||
// ------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
virtual void mapJsonObject(JsonObject& json);
|
||||||
|
virtual void fromJsonObject(JsonObject& json);
|
||||||
virtual int verifyJsonObject(JsonObject& json){
|
virtual int verifyJsonObject(JsonObject& json){
|
||||||
return json.success();
|
return json.success();
|
||||||
//&& json.containsKey(JSON_DOMAIN)
|
//&& 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){
|
String getAttrFromJson(JsonObject& json, const char* attr){
|
||||||
if(json.containsKey(attr)){
|
if(json.containsKey(String(attr))){
|
||||||
return json[attr];
|
const char *value = json[attr];
|
||||||
|
return String(value);
|
||||||
|
//return json[attr];
|
||||||
}
|
}
|
||||||
return "";
|
return "no value";
|
||||||
}
|
}
|
||||||
int getIntAttrFromJson(JsonObject& json, const char* attr){
|
int getIntAttrFromJson(JsonObject& json, const char* attr){
|
||||||
if(json.containsKey(attr)){
|
if(json.containsKey(attr)){
|
||||||
@@ -30,14 +43,52 @@ struct JsonStruct {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
// Map a json object to this struct.
|
// Map a json object to this struct.
|
||||||
virtual int fromJsonObject(JsonObject& json);
|
|
||||||
// Parse a json string and map parsed object
|
// Parse a json string and map parsed object
|
||||||
int fromJsonString(String& str){
|
void fromJsonString(String& str){
|
||||||
//StaticJsonBuffer<200> jsonBuffer;
|
//StaticJsonBuffer<200> jsonBuffer;
|
||||||
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
||||||
JsonObject& json = jsonBuffer.parseObject(str);
|
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
|
#endif
|
||||||
@@ -1,12 +1,23 @@
|
|||||||
#include "MeshNet.h"
|
#include "MeshNet.h"
|
||||||
|
|
||||||
MeshNet::MeshNet(MeshConfig cfg) : Network() {
|
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(){
|
Network* MeshNet::init(){
|
||||||
|
|
||||||
Serial.println("init mesh");
|
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( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
|
||||||
mesh.setDebugMsgTypes( config.debugTypes );
|
mesh.setDebugMsgTypes( config.debugTypes );
|
||||||
mesh.init( config.meshSSID, config.meshPassword, scheduler, config.meshPort, WIFI_AP_STA, config.channel );
|
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){
|
if(doConnect){
|
||||||
Serial.println("connect station");
|
Serial.println("connect station");
|
||||||
mesh.stationManual(config.stationSSID, config.stationPassword);
|
mesh.stationManual(config.stationSSID, config.stationPassword);
|
||||||
mesh.setHostname(config.hostname);
|
mesh.setHostname(config.hostname.c_str());
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
#include "MeshConfig.h"
|
#include "MeshConfig.h"
|
||||||
|
#include "base/MeshSprocketConfig.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
@@ -18,12 +19,11 @@ using namespace std::placeholders;
|
|||||||
class MeshNet : public Network {
|
class MeshNet : public Network {
|
||||||
public:
|
public:
|
||||||
painlessMesh mesh;
|
painlessMesh mesh;
|
||||||
MeshConfig config;
|
MeshSprocketConfig config;
|
||||||
|
|
||||||
MeshNet(MeshConfig cfg);
|
MeshNet(MeshConfig cfg);
|
||||||
Network* init();
|
Network* init();
|
||||||
Network* connectStation(int);
|
Network* connectStation(int);
|
||||||
|
void configure(MeshSprocketConfig cfg);
|
||||||
void update();
|
void update();
|
||||||
void newConnectionCallback(uint32_t nodeId);
|
void newConnectionCallback(uint32_t nodeId);
|
||||||
void changedConnectionCallback();
|
void changedConnectionCallback();
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ typedef std::function<void(uint32_t from, String &msg)> msgReceived_cb;
|
|||||||
class Network {
|
class Network {
|
||||||
public:
|
public:
|
||||||
uint32_t id = 0;
|
uint32_t id = 0;
|
||||||
Network(){}
|
|
||||||
Scheduler* scheduler;
|
Scheduler* scheduler;
|
||||||
virtual Network* init() { return this; };
|
virtual Network* init() { return this; };
|
||||||
virtual Network* init(Scheduler* s) { scheduler = s; return init(); };
|
virtual Network* init(Scheduler* s) { scheduler = s; return init(); };
|
||||||
|
|||||||
@@ -42,6 +42,12 @@ void Sprocket::loop(){
|
|||||||
scheduler.execute();
|
scheduler.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Sprocket::dispatch( uint32_t from, String &msg ) {
|
||||||
|
MeshMessage mMsg;
|
||||||
|
if(mMsg.fromJsonString(msg)){
|
||||||
|
dispatchMessageToPlugins(mMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Sprocket::addPlugin(Plugin* p){
|
void Sprocket::addPlugin(Plugin* p){
|
||||||
plugins.reserve(1);
|
plugins.reserve(1);
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class Sprocket {
|
|||||||
virtual Sprocket* activate();
|
virtual Sprocket* activate();
|
||||||
virtual Sprocket* activate(Scheduler*) { return this; }
|
virtual Sprocket* activate(Scheduler*) { return this; }
|
||||||
virtual Sprocket* activate(Scheduler*, Network*);
|
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 addPlugin(Plugin* p);
|
||||||
void setupPlugins(Scheduler* scheduler, Network* network);
|
void setupPlugins(Scheduler* scheduler, Network* network);
|
||||||
|
|||||||
@@ -34,10 +34,7 @@ class MeshSprocket : public Sprocket {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void dispatch( uint32_t from, String &msg ) {
|
void dispatch( uint32_t from, String &msg ) {
|
||||||
MeshMessage mMsg;
|
Sprocket::dispatch(from, msg);
|
||||||
if(mMsg.fromJsonString(msg)){
|
|
||||||
dispatchMessageToPlugins(mMsg);
|
|
||||||
}
|
|
||||||
onMessage(from, msg);
|
onMessage(from, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,52 +17,39 @@
|
|||||||
#define JSON_stationPassword "stationPassword"
|
#define JSON_stationPassword "stationPassword"
|
||||||
#define JSON_hostname "hostname"
|
#define JSON_hostname "hostname"
|
||||||
|
|
||||||
#define JSON_startupDelay "startupDelay"
|
|
||||||
#define JSON_serialBaudRate "serialBaudRate"
|
|
||||||
|
|
||||||
struct MeshSprocketConfig : public JsonStruct {
|
struct MeshSprocketConfig : public JsonStruct {
|
||||||
SprocketConfig sprocket;
|
int stationMode;
|
||||||
MeshConfig mesh;
|
int channel;
|
||||||
int valid = 0;
|
int meshPort;
|
||||||
|
String meshSSID;
|
||||||
|
String meshPassword;
|
||||||
|
String stationSSID;
|
||||||
|
String stationPassword;
|
||||||
|
String hostname;
|
||||||
|
uint16_t debugTypes;
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------
|
||||||
String toJsonString(){
|
void mapJsonObject(JsonObject& root) {
|
||||||
//StaticJsonBuffer<200> jsonBuffer;
|
root[JSON_stationMode] = stationMode;
|
||||||
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
root[JSON_channel] = channel;
|
||||||
JsonObject& root = jsonBuffer.createObject();
|
root[JSON_meshPort] = meshPort;
|
||||||
root[JSON_stationMode] = mesh.stationMode;
|
root[JSON_meshSSID] = meshSSID;
|
||||||
root[JSON_channel] = mesh.channel;
|
root[JSON_meshPassword] = meshPassword;
|
||||||
root[JSON_meshPort] = mesh.meshPort;
|
root[JSON_stationSSID] = stationSSID;
|
||||||
root[JSON_meshSSID] = mesh.meshSSID;
|
root[JSON_stationPassword] = stationPassword;
|
||||||
root[JSON_meshPassword] = mesh.meshPassword;
|
root[JSON_hostname] = hostname;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Map a json object to this struct.
|
// Map a json object to this struct.
|
||||||
int fromJsonObject(JsonObject& json){
|
void fromJsonObject(JsonObject& json) {
|
||||||
if(!verifyJsonObject(json)){
|
stationMode = getIntAttrFromJson(json, JSON_stationMode);
|
||||||
Serial.println("ERROR: cannot parse JSON object");
|
channel = getIntAttrFromJson(json, JSON_channel);
|
||||||
valid = 0;
|
meshPort = getIntAttrFromJson(json, JSON_meshPort);
|
||||||
return valid;
|
meshSSID = getAttrFromJson(json, JSON_meshSSID);
|
||||||
}
|
meshPassword = getAttrFromJson(json, JSON_meshPassword);
|
||||||
mesh.stationMode = getIntAttrFromJson(json, JSON_stationMode);
|
stationSSID = getAttrFromJson(json, JSON_stationSSID);
|
||||||
mesh.channel = getIntAttrFromJson(json, JSON_channel);
|
stationPassword = getAttrFromJson(json, JSON_stationPassword);
|
||||||
mesh.meshPort = getIntAttrFromJson(json, JSON_meshPort);
|
hostname = getAttrFromJson(json, JSON_hostname);
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -15,10 +15,10 @@
|
|||||||
#define MESH_PORT 5555
|
#define MESH_PORT 5555
|
||||||
#define MESH_PREFIX "whateverYouLike"
|
#define MESH_PREFIX "whateverYouLike"
|
||||||
#define MESH_PASSWORD "somethingSneaky"
|
#define MESH_PASSWORD "somethingSneaky"
|
||||||
#define STATION_SSID "tErAx1d"
|
#define STATION_SSID "Th1ngs4p"
|
||||||
#define STATION_PASSWORD "ramalamadingdong"
|
#define STATION_PASSWORD "th3r31sn0sp00n"
|
||||||
#define HOSTNAME "mesh-node"
|
#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
|
//ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE
|
||||||
|
|
||||||
// OTA config
|
// OTA config
|
||||||
|
|||||||
@@ -2,16 +2,17 @@
|
|||||||
#include "MeshNet.h"
|
#include "MeshNet.h"
|
||||||
#include "MeshApp.cpp"
|
#include "MeshApp.cpp"
|
||||||
|
|
||||||
|
MeshApp sprocket(
|
||||||
|
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
|
||||||
|
{ OTA_PORT, OTA_PASSWORD }
|
||||||
|
);
|
||||||
|
|
||||||
MeshNet net({
|
MeshNet net({
|
||||||
SPROCKET_MODE, WIFI_CHANNEL,
|
SPROCKET_MODE, WIFI_CHANNEL,
|
||||||
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
|
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
|
||||||
STATION_SSID, STATION_PASSWORD, HOSTNAME,
|
STATION_SSID, STATION_PASSWORD, HOSTNAME,
|
||||||
MESH_DEBUG_TYPES
|
MESH_DEBUG_TYPES
|
||||||
});
|
});
|
||||||
MeshApp sprocket(
|
|
||||||
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
|
|
||||||
{ OTA_PORT, OTA_PASSWORD }
|
|
||||||
);
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
sprocket.join(net);
|
sprocket.join(net);
|
||||||
|
|||||||
Reference in New Issue
Block a user