diff --git a/platformio.ini b/platformio.ini
index c8231b5..78a249a 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -55,7 +55,7 @@ lib_deps = ${common.lib_deps}
painlessMesh
ESP8266mDNS
ArduinoOTA
-upload_port = 192.168.1.247
+;upload_port = 192.168.1.247
[env:meshMqttBridge]
src_filter = +<*> - +
diff --git a/src/JsonStruct.h b/src/JsonStruct.h
new file mode 100644
index 0000000..5a65912
--- /dev/null
+++ b/src/JsonStruct.h
@@ -0,0 +1,43 @@
+#ifndef __JSON_STRUCT__
+#define __JSON_STRUCT__
+
+#include
+#include
+
+
+struct JsonStruct {
+ SprocketConfig soricketConfig;
+ MeshConfig meshConfig;
+ int valid = 0;
+
+ // ------------------------------------------------------------------------------------------
+
+ virtual int verifyJsonObject(JsonObject& json){
+ return json.success();
+ //&& json.containsKey(JSON_DOMAIN)
+ };
+ virtual String toJsonString();
+ String getAttrFromJson(JsonObject& json, const char* attr){
+ if(json.containsKey(attr)){
+ return json[attr];
+ }
+ return "";
+ }
+ int getIntAttrFromJson(JsonObject& json, const char* attr){
+ if(json.containsKey(attr)){
+ return json[attr];
+ }
+ 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){
+ //StaticJsonBuffer<200> jsonBuffer;
+ DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
+ JsonObject& json = jsonBuffer.parseObject(str);
+ return fromJsonObject(json);
+ };
+};
+
+#endif
\ No newline at end of file
diff --git a/src/MeshConfig.h b/src/MeshConfig.h
new file mode 100644
index 0000000..1a1d6e0
--- /dev/null
+++ b/src/MeshConfig.h
@@ -0,0 +1,18 @@
+#ifndef __MESHCONFIG__
+#define __MESHCONFIG__
+
+// FIXME non-mesh config should have it's own struct
+struct MeshConfig {
+ int stationMode;
+ int channel;
+ int meshPort;
+ const char* meshSSID;
+ const char* meshPassword;
+ const char* stationSSID;
+ const char* stationPassword;
+ const char* hostname;
+ uint16_t debugTypes;
+};
+
+
+#endif
\ No newline at end of file
diff --git a/src/MeshNet.cpp b/src/MeshNet.cpp
index 2c35f0c..7c9c30f 100644
--- a/src/MeshNet.cpp
+++ b/src/MeshNet.cpp
@@ -20,9 +20,8 @@ Network* MeshNet::init(){
return this;
}
Network* MeshNet::connectStation(int doConnect) {
- Serial.println("connect station?");
if(doConnect){
- Serial.println("connect station!");
+ Serial.println("connect station");
mesh.stationManual(config.stationSSID, config.stationPassword);
mesh.setHostname(config.hostname);
}
diff --git a/src/MeshNet.h b/src/MeshNet.h
index 97ce245..07d1e4f 100644
--- a/src/MeshNet.h
+++ b/src/MeshNet.h
@@ -10,23 +10,11 @@
#include
#include
#include "Network.h"
+#include "MeshConfig.h"
using namespace std;
using namespace std::placeholders;
-// FIXME non-mesh config should have it's own struct
-struct MeshConfig {
- int stationMode;
- int channel;
- int meshPort;
- const char* meshSSID;
- const char* meshPassword;
- const char* stationSSID;
- const char* stationPassword;
- const char* hostname;
- uint16_t debugTypes;
-};
-
class MeshNet : public Network {
public:
painlessMesh mesh;
diff --git a/src/Sprocket.cpp b/src/Sprocket.cpp
index aa3751b..55eab57 100644
--- a/src/Sprocket.cpp
+++ b/src/Sprocket.cpp
@@ -18,6 +18,11 @@ Sprocket* Sprocket::init(SprocketConfig cfg){
Sprocket* Sprocket::activate() {
return activate(&scheduler);
}
+Sprocket* Sprocket::activate(Scheduler* scheduler, Network* network) {
+ // setup plugins
+ setupPlugins(scheduler, network);
+ return this;
+}
Sprocket* Sprocket::join(Network& net){
Serial.println("join network");
@@ -35,4 +40,25 @@ Sprocket* Sprocket::addTask(Task& tsk){
void Sprocket::loop(){
scheduler.execute();
+}
+
+
+void Sprocket::addPlugin(Plugin* p){
+ plugins.reserve(1);
+ plugins.push_back(p);
+}
+
+void Sprocket::setupPlugins(Scheduler* scheduler, Network* network){
+ for(Plugin* p : plugins){
+ p->setup(scheduler, network);
+ }
+}
+
+void Sprocket::dispatchMessageToPlugins(MeshMessage msg){
+ if(msg.type != MeshMessage::NONE){ // baaaa
+ for(Plugin* p : plugins){
+ Serial.println("dispatch to plugins");
+ p->onMessage(msg);
+ }
+ }
}
\ No newline at end of file
diff --git a/src/Sprocket.h b/src/Sprocket.h
index f9a2532..9d3d715 100644
--- a/src/Sprocket.h
+++ b/src/Sprocket.h
@@ -3,11 +3,14 @@
#include
#include
+#include
#include "FS.h"
#ifdef ESP32
#include "SPIFFS.h"
#endif
#include "Network.h"
+#include "SprocketConfig.h"
+#include "Plugin.h"
using namespace std;
using namespace std::placeholders;
@@ -15,16 +18,12 @@ using namespace std::placeholders;
// FIXME move to some global fnc lib
#define ARRAY_LENGTH(array) sizeof(array)/sizeof(array[0])
-struct SprocketConfig {
- int startupDelay;
- int serialBaudRate;
-};
-
class Sprocket {
protected:
Scheduler scheduler;
public:
SprocketConfig config;
+ std::vector plugins;
Sprocket();
Sprocket(SprocketConfig);
Sprocket* init(SprocketConfig);
@@ -33,8 +32,12 @@ class Sprocket {
virtual void loop();
virtual Sprocket* activate();
virtual Sprocket* activate(Scheduler*) { return this; }
- virtual Sprocket* activate(Scheduler*, Network*) { return this; }
+ virtual Sprocket* activate(Scheduler*, Network*);
virtual void dispatch( uint32_t from, String &msg ) {}
+
+ void addPlugin(Plugin* p);
+ void setupPlugins(Scheduler* scheduler, Network* network);
+ void dispatchMessageToPlugins(MeshMessage msg);
};
#endif
\ No newline at end of file
diff --git a/src/SprocketConfig.h b/src/SprocketConfig.h
new file mode 100644
index 0000000..645cf86
--- /dev/null
+++ b/src/SprocketConfig.h
@@ -0,0 +1,9 @@
+#ifndef __SPROCKET_CONFIG__
+#define __SPROCKET_CONFIG__
+
+struct SprocketConfig {
+ int startupDelay;
+ int serialBaudRate;
+};
+
+#endif
\ No newline at end of file
diff --git a/src/base/MeshSprocket.h b/src/base/MeshSprocket.h
index 790323d..5fa77bf 100644
--- a/src/base/MeshSprocket.h
+++ b/src/base/MeshSprocket.h
@@ -7,6 +7,7 @@
#include
#include
#include
+#include
#include
#include "config.h"
@@ -16,37 +17,15 @@ using namespace std::placeholders;
class MeshSprocket : public Sprocket {
public:
MeshNet* net;
- std::vector plugins;
MeshSprocket(SprocketConfig cfg, OtaConfig otaCfg) : Sprocket(cfg) {
addPlugin(new OtaTcpPlugin(otaCfg));
}
-
- void addPlugin(Plugin* p){
- plugins.reserve(1);
- plugins.push_back(p);
- }
-
- void setupPlugins(Scheduler* scheduler, Network* network){
- for(Plugin* p : plugins){
- p->setup(scheduler, network);
- }
- }
-
- void dispatchMessageToPlugins(MeshMessage msg){
- if(msg.type != MeshMessage::NONE){ // baaaa
- for(Plugin* p : plugins){
- Serial.println("dispatch to plugins");
- p->onMessage(msg);
- }
- }
- }
Sprocket* activate(Scheduler* scheduler, Network* network) {
+ Sprocket::activate(scheduler, network);
net = static_cast(network);
net->onReceive(bind(&MeshSprocket::dispatch,this, _1, _2));
- // setup plugins
- setupPlugins(scheduler, network);
return this;
} using Sprocket::activate;
diff --git a/src/base/MeshSprocketConfig.h b/src/base/MeshSprocketConfig.h
new file mode 100644
index 0000000..c77d720
--- /dev/null
+++ b/src/base/MeshSprocketConfig.h
@@ -0,0 +1,69 @@
+#ifndef __MESH_SPROCKET_CONFIG__
+#define __MESH_SPROCKET_CONFIG__
+
+#include
+#include
+#include
+#include "MeshConfig.h"
+#include "SprocketConfig.h"
+#include "JsonStruct.h"
+
+#define JSON_stationMode "stationMode"
+#define JSON_channel "channel"
+#define JSON_meshPort "meshPort"
+#define JSON_meshSSID "meshSSID"
+#define JSON_meshPassword "meshPassword"
+#define JSON_stationSSID "stationSSID"
+#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;
+
+ // ------------------------------------------------------------------------------------------
+ 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;
+ }
+ // 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;
+ };
+};
+
+#endif
\ No newline at end of file
diff --git a/src/plugins/OtaTcpPlugin.cpp b/src/plugins/OtaTcpPlugin.cpp
index 25cc569..f0c9cd1 100644
--- a/src/plugins/OtaTcpPlugin.cpp
+++ b/src/plugins/OtaTcpPlugin.cpp
@@ -36,9 +36,12 @@ class OtaTcpPlugin : public Plugin {
void onMessage(MeshMessage msg) {
if(msg.type == MeshMessage::OTA){
Serial.println("OTA msg received");
+ WiFi.disconnect();
connectUpdateNetwork();
enable();
//net->mesh.sendBroadcast("my ip:" + WiFi.localIP().toString(), true);
+ //net->mesh.sendBroadcast("gw ip:" + WiFi.gatewayIP().toString(), true);
+ WiFi.gatewayIP().printTo(Serial);
}
}
void setup(Scheduler* userScheduler, Network* network){