mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-15 13:08:21 +01:00
Merge branch 'feature/12-refactor-lifecycle' into 'develop'
Resolve "Refactor lifecycle" Closes #12 See merge request wirelos/sprocket-lib!14
This commit is contained in:
@@ -34,11 +34,10 @@ firmware-build:
|
|||||||
stage: build
|
stage: build
|
||||||
image: python:2.7-stretch
|
image: python:2.7-stretch
|
||||||
script:
|
script:
|
||||||
- pio run -t clean
|
- pio run --target clean
|
||||||
- pio run --environment basic
|
- pio run --environment basic
|
||||||
- pio run --environment mesh
|
- pio run --environment wifi
|
||||||
- pio run --environment meshMqttBridge
|
- pio run --environment wifiMesh
|
||||||
- pio run --environment standalone
|
|
||||||
artifacts:
|
artifacts:
|
||||||
paths:
|
paths:
|
||||||
- .pioenvs/*/firmware.*
|
- .pioenvs/*/firmware.*
|
||||||
|
|||||||
@@ -8,8 +8,8 @@
|
|||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; http://docs.platformio.org/page/projectconf.html
|
; http://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[platformio]
|
;[platformio]
|
||||||
env_default = mesh
|
;env_default = mesh
|
||||||
|
|
||||||
[common]
|
[common]
|
||||||
framework = arduino
|
framework = arduino
|
||||||
@@ -46,8 +46,8 @@ monitor_baud = ${common.monitor_baud}
|
|||||||
framework = ${common.framework}
|
framework = ${common.framework}
|
||||||
lib_deps = ${common.lib_deps}
|
lib_deps = ${common.lib_deps}
|
||||||
|
|
||||||
[env:mesh]
|
[env:wifiMesh]
|
||||||
src_filter = +<*> -<examples/> +<examples/mesh/>
|
src_filter = +<*> -<examples/> +<examples/wifiMesh/>
|
||||||
platform = ${common.platform}
|
platform = ${common.platform}
|
||||||
board = ${common.board}
|
board = ${common.board}
|
||||||
upload_speed = ${common.upload_speed}
|
upload_speed = ${common.upload_speed}
|
||||||
@@ -55,31 +55,13 @@ monitor_baud = ${common.monitor_baud}
|
|||||||
framework = ${common.framework}
|
framework = ${common.framework}
|
||||||
lib_deps = ${common.lib_deps}
|
lib_deps = ${common.lib_deps}
|
||||||
painlessMesh
|
painlessMesh
|
||||||
ESP8266mDNS
|
|
||||||
ArduinoOTA
|
|
||||||
ESP Async WebServer
|
|
||||||
ESPAsyncTCP
|
|
||||||
;upload_port = 192.168.1.247
|
|
||||||
|
|
||||||
[env:meshMqttBridge]
|
[env:wifi]
|
||||||
src_filter = +<*> -<examples/> +<examples/meshMqttBridge/>
|
src_filter = +<*> -<examples/> +<examples/wifi/>
|
||||||
platform = espressif8266
|
|
||||||
board = esp12e
|
|
||||||
upload_speed = ${common.upload_speed}
|
|
||||||
monitor_baud = ${common.monitor_baud}
|
|
||||||
framework = ${common.framework}
|
|
||||||
lib_deps = ${common.lib_deps}
|
|
||||||
painlessMesh
|
|
||||||
PubSubClient
|
|
||||||
|
|
||||||
|
|
||||||
[env:standalone]
|
|
||||||
src_filter = +<*> -<examples/> +<examples/standalone/>
|
|
||||||
platform = ${common.platform}
|
platform = ${common.platform}
|
||||||
board = ${common.board}
|
board = ${common.board}
|
||||||
upload_speed = ${common.upload_speed}
|
upload_speed = ${common.upload_speed}
|
||||||
monitor_baud = ${common.monitor_baud}
|
monitor_baud = ${common.monitor_baud}
|
||||||
framework = ${common.framework}
|
framework = ${common.framework}
|
||||||
lib_deps = ${common.lib_deps}
|
lib_deps = ${common.lib_deps}
|
||||||
ArduinoOTA
|
|
||||||
ESP Async WebServer
|
ESP Async WebServer
|
||||||
@@ -16,10 +16,8 @@ struct JsonStruct {
|
|||||||
virtual void fromJsonObject(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)
|
|
||||||
};
|
};
|
||||||
String toJsonString(){
|
String toJsonString(){
|
||||||
//StaticJsonBuffer<200> StringjsonBuffer;
|
|
||||||
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
DynamicJsonBuffer jsonBuffer(JSON_ARRAY_SIZE(300));
|
||||||
JsonObject& root = jsonBuffer.createObject();
|
JsonObject& root = jsonBuffer.createObject();
|
||||||
mapJsonObject(root);
|
mapJsonObject(root);
|
||||||
@@ -44,7 +42,6 @@ struct JsonStruct {
|
|||||||
// Map a json object to this struct.
|
// Map a json object to this struct.
|
||||||
// Parse a json string and map parsed object
|
// Parse a json string and map parsed object
|
||||||
void fromJsonString(String& str){
|
void fromJsonString(String& str){
|
||||||
//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);
|
||||||
valid = verifyJsonObject(json);
|
valid = verifyJsonObject(json);
|
||||||
@@ -56,13 +53,6 @@ struct JsonStruct {
|
|||||||
void fromFile(const char* path) {
|
void fromFile(const char* path) {
|
||||||
File configFile = SPIFFS.open(path, "r");
|
File configFile = SPIFFS.open(path, "r");
|
||||||
String cfgFileStr = configFile.readString();
|
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;
|
DynamicJsonBuffer jsonBuffer;
|
||||||
JsonObject& json = jsonBuffer.parseObject(cfgFileStr);
|
JsonObject& json = jsonBuffer.parseObject(cfgFileStr);
|
||||||
|
|
||||||
|
|||||||
@@ -13,37 +13,36 @@ MeshNet::MeshNet(MeshConfig cfg) : Network() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Network* MeshNet::init(){
|
Network* MeshNet::init(){
|
||||||
|
|
||||||
Serial.println("init mesh");
|
Serial.println("init mesh");
|
||||||
config.fromFile("/config.json");
|
config.fromFile("/config.json");
|
||||||
|
|
||||||
//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 );
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
int MeshNet::connect(){
|
||||||
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 );
|
||||||
|
|
||||||
mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1));
|
mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1));
|
||||||
mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this));
|
mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this));
|
||||||
mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1));
|
mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1));
|
||||||
|
if(config.stationMode){
|
||||||
connectStation(config.stationMode);
|
connectStation();
|
||||||
|
}
|
||||||
return this;
|
return 1;
|
||||||
}
|
}
|
||||||
int MeshNet::connectStation(int doConnect) {
|
|
||||||
if(doConnect){
|
int MeshNet::connectStation() {
|
||||||
Serial.println("connect station");
|
Serial.println("connect station");
|
||||||
mesh.stationManual(config.stationSSID, config.stationPassword);
|
mesh.stationManual(config.stationSSID, config.stationPassword);
|
||||||
mesh.setHostname(config.hostname.c_str());
|
mesh.setHostname(config.hostname.c_str());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
void MeshNet::sendTo(uint32_t target, String msg){
|
void MeshNet::sendTo(uint32_t target, String msg){
|
||||||
mesh.sendSingle(target, msg);
|
mesh.sendSingle(target, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MeshNet::broadcast(String msg){
|
void MeshNet::broadcast(String msg, bool self){
|
||||||
mesh.sendBroadcast(msg);
|
mesh.sendBroadcast(msg, self);
|
||||||
|
|
||||||
}
|
}
|
||||||
void MeshNet::update(){
|
void MeshNet::update(){
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
#include "MeshConfig.h"
|
#include "MeshConfig.h"
|
||||||
#include "base/MeshSprocketConfig.h"
|
#include "MeshSprocketConfig.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
@@ -22,13 +22,14 @@ class MeshNet : public Network {
|
|||||||
MeshSprocketConfig config;
|
MeshSprocketConfig config;
|
||||||
MeshNet(MeshConfig cfg);
|
MeshNet(MeshConfig cfg);
|
||||||
Network* init();
|
Network* init();
|
||||||
int connectStation(int);
|
int connect();
|
||||||
|
int connectStation();
|
||||||
void configure(MeshSprocketConfig cfg);
|
void configure(MeshSprocketConfig cfg);
|
||||||
void update();
|
void update();
|
||||||
void newConnectionCallback(uint32_t nodeId);
|
void newConnectionCallback(uint32_t nodeId);
|
||||||
void changedConnectionCallback();
|
void changedConnectionCallback();
|
||||||
void nodeTimeAdjustedCallback(int32_t offset);
|
void nodeTimeAdjustedCallback(int32_t offset);
|
||||||
void broadcast(String msg);
|
void broadcast(String msg, bool self = false);
|
||||||
void sendTo(uint32_t target, String msg);
|
void sendTo(uint32_t target, String msg);
|
||||||
void onReceive(std::function<void(uint32_t from, String &msg)>);
|
void onReceive(std::function<void(uint32_t from, String &msg)>);
|
||||||
int isConnected(){
|
int isConnected(){
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class Network {
|
|||||||
virtual int connectStation() { return 0; };
|
virtual int connectStation() { return 0; };
|
||||||
virtual int isConnected(){ return 0; };
|
virtual int isConnected(){ return 0; };
|
||||||
virtual void update() {};
|
virtual void update() {};
|
||||||
virtual void broadcast(String msg){};
|
virtual void broadcast(String msg, bool self = false){};
|
||||||
virtual void sendTo(uint32_t target, String msg) {};
|
virtual void sendTo(uint32_t target, String msg) {};
|
||||||
virtual void onReceive(std::function<void(uint32_t from, String &msg)>) {};
|
virtual void onReceive(std::function<void(uint32_t from, String &msg)>) {};
|
||||||
Network* setScheduler(Scheduler* s) {
|
Network* setScheduler(Scheduler* s) {
|
||||||
|
|||||||
14
src/Plugin.h
14
src/Plugin.h
@@ -1,18 +1,22 @@
|
|||||||
#ifndef __SPROCKET_PLUGIN__
|
#ifndef __SPROCKET_PLUGIN__
|
||||||
#define __SPROCKET_PLUGIN__
|
#define __SPROCKET_PLUGIN__
|
||||||
|
|
||||||
|
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||||
|
#define _TASK_STD_FUNCTION
|
||||||
|
#define _TASK_PRIORITY
|
||||||
|
|
||||||
#include <TaskSchedulerDeclarations.h>
|
#include <TaskSchedulerDeclarations.h>
|
||||||
#include <Network.h>
|
#include <Network.h>
|
||||||
#include <base/SprocketMessage.h>
|
#include <SprocketMessage.h>
|
||||||
#include <Mediator.h>
|
#include <Mediator.h>
|
||||||
|
|
||||||
class Plugin {
|
class Plugin {
|
||||||
public:
|
public:
|
||||||
Mediator* mediator;
|
Mediator* mediator;
|
||||||
virtual void activate(Scheduler*, Network*);
|
virtual void activate(Scheduler*);
|
||||||
virtual void enable(){};
|
virtual void enable(){}
|
||||||
virtual void disable(){};
|
virtual void disable(){}
|
||||||
virtual void onMessage(SprocketMessage msg){};
|
virtual void onMessage(SprocketMessage msg){}
|
||||||
Plugin* mediate(Mediator* m) {
|
Plugin* mediate(Mediator* m) {
|
||||||
mediator = m;
|
mediator = m;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
@@ -13,34 +13,22 @@ Sprocket* Sprocket::init(SprocketConfig cfg){
|
|||||||
delay(cfg.startupDelay);
|
delay(cfg.startupDelay);
|
||||||
Serial.begin(cfg.serialBaudRate);
|
Serial.begin(cfg.serialBaudRate);
|
||||||
SPIFFS.begin();
|
SPIFFS.begin();
|
||||||
|
scheduler = new Scheduler();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
Sprocket* Sprocket::activate() {
|
Sprocket* Sprocket::activate() {
|
||||||
return activate(&scheduler);
|
activatePlugins(scheduler);
|
||||||
}
|
return activate(scheduler);
|
||||||
Sprocket* Sprocket::activate(Scheduler* scheduler, Network* network) {
|
|
||||||
// setup plugins
|
|
||||||
activatePlugins(scheduler, network);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Sprocket* Sprocket::join(Network& net){
|
|
||||||
Serial.println("join network");
|
|
||||||
net.init(&scheduler);
|
|
||||||
//net.onReceive(bind(&Sprocket::dispatch,this, _1, _2));
|
|
||||||
net.connect();
|
|
||||||
network = net;
|
|
||||||
return activate(&scheduler, &net);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Sprocket* Sprocket::addTask(Task& tsk){
|
Sprocket* Sprocket::addTask(Task& tsk){
|
||||||
scheduler.addTask(tsk);
|
scheduler->addTask(tsk);
|
||||||
tsk.enable();
|
tsk.enable();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sprocket::loop(){
|
void Sprocket::loop(){
|
||||||
scheduler.execute();
|
scheduler->execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sprocket::dispatch( uint32_t from, String &msg ) {
|
void Sprocket::dispatch( uint32_t from, String &msg ) {
|
||||||
@@ -57,8 +45,8 @@ void Sprocket::addPlugin(Plugin* p){
|
|||||||
plugins.push_back(p);
|
plugins.push_back(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sprocket::activatePlugins(Scheduler* scheduler, Network* network){
|
void Sprocket::activatePlugins(Scheduler* scheduler){
|
||||||
for(Plugin* p : plugins){
|
for(Plugin* p : plugins){
|
||||||
p->activate(scheduler, network);
|
p->activate(scheduler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
#ifndef __SPROCKET_H__
|
#ifndef __SPROCKET_H__
|
||||||
#define __SPROCKET_H__
|
#define __SPROCKET_H__
|
||||||
|
|
||||||
|
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||||
|
#define _TASK_STD_FUNCTION
|
||||||
|
#define _TASK_PRIORITY
|
||||||
|
|
||||||
#include <TaskSchedulerDeclarations.h>
|
#include <TaskSchedulerDeclarations.h>
|
||||||
//#include <TaskScheduler.h>
|
//#include <TaskScheduler.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
@@ -23,7 +27,7 @@ class Sprocket : public Mediator {
|
|||||||
protected:
|
protected:
|
||||||
// TODO move scheduler out of Sprocket
|
// TODO move scheduler out of Sprocket
|
||||||
// => see difference between standalone and mesh sprochet usage of scheduler
|
// => see difference between standalone and mesh sprochet usage of scheduler
|
||||||
Scheduler scheduler;
|
Scheduler* scheduler;
|
||||||
Network network;
|
Network network;
|
||||||
private:
|
private:
|
||||||
SprocketMessage currentMessage;
|
SprocketMessage currentMessage;
|
||||||
@@ -33,17 +37,14 @@ class Sprocket : public Mediator {
|
|||||||
Sprocket();
|
Sprocket();
|
||||||
Sprocket(SprocketConfig);
|
Sprocket(SprocketConfig);
|
||||||
Sprocket* init(SprocketConfig);
|
Sprocket* init(SprocketConfig);
|
||||||
Sprocket* join(Network&);
|
|
||||||
Sprocket* addTask(Task&);
|
Sprocket* addTask(Task&);
|
||||||
virtual void loop();
|
virtual void loop();
|
||||||
virtual Sprocket* activate();
|
virtual Sprocket* activate();
|
||||||
virtual Sprocket* activate(Scheduler*) { return this; }
|
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 addPlugin(Plugin* p);
|
||||||
void activatePlugins(Scheduler* scheduler, Network* network);
|
void activatePlugins(Scheduler* scheduler);
|
||||||
void dispatchMessageToPlugins(SprocketMessage msg);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -22,14 +22,12 @@ Network* WiFiNet::init() {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
int WiFiNet::connect(){
|
int WiFiNet::connect(){
|
||||||
if(config.valid){
|
WiFi.hostname(config.hostname);
|
||||||
WiFi.hostname(config.hostname);
|
Serial.println("Hostname: " + config.hostname);
|
||||||
Serial.println("Hostname: " + config.hostname);
|
if(!connectStation()) {
|
||||||
if(!connectStation()) {
|
createAccessPoint();
|
||||||
createAccessPoint();
|
|
||||||
}
|
|
||||||
startDNS();
|
|
||||||
}
|
}
|
||||||
|
startDNS();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,6 +39,7 @@ int WiFiNet::connectStation(){
|
|||||||
WiFi.begin(config.stationSSID.c_str(), config.stationPassword.c_str());
|
WiFi.begin(config.stationSSID.c_str(), config.stationPassword.c_str());
|
||||||
Serial.println("connect to " + config.stationSSID);
|
Serial.println("connect to " + config.stationSSID);
|
||||||
|
|
||||||
|
// TODO use tasks
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
|
|||||||
@@ -1,50 +0,0 @@
|
|||||||
#ifndef __MESH_SPROCKET__
|
|
||||||
#define __MESH_SPROCKET__
|
|
||||||
|
|
||||||
#define DEBUG_ESP_OTA
|
|
||||||
#include <vector>
|
|
||||||
#include <painlessMesh.h>
|
|
||||||
#include <Sprocket.h>
|
|
||||||
#include <MeshNet.h>
|
|
||||||
#include <base/SprocketMessage.h>
|
|
||||||
#include <base/MeshSprocketConfig.h>
|
|
||||||
#include "config.h"
|
|
||||||
#include "utils_print.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace std::placeholders;
|
|
||||||
|
|
||||||
class MeshSprocket : public Sprocket {
|
|
||||||
public:
|
|
||||||
MeshNet* net;
|
|
||||||
|
|
||||||
MeshSprocket(){};
|
|
||||||
MeshSprocket(SprocketConfig cfg) : Sprocket(cfg) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
|
||||||
Sprocket::activate(scheduler, network);
|
|
||||||
net = static_cast<MeshNet*>(network);
|
|
||||||
net->onReceive(bind(&MeshSprocket::dispatch,this, _1, _2));
|
|
||||||
|
|
||||||
return this;
|
|
||||||
} using Sprocket::activate;
|
|
||||||
|
|
||||||
// TODO transmit SprocketMessage
|
|
||||||
virtual void dispatch( uint32_t from, String &msg ) {
|
|
||||||
SprocketMessage sMsg;
|
|
||||||
sMsg.fromJsonString(msg);
|
|
||||||
if(sMsg.valid){
|
|
||||||
sMsg.from = from;
|
|
||||||
publish(sMsg.topic, sMsg.payload);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
net->update();
|
|
||||||
scheduler.execute();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
#ifndef __MESH_APP__
|
|
||||||
#define __MESH_APP__
|
|
||||||
|
|
||||||
#include <painlessMesh.h>
|
|
||||||
#include <base/MeshSprocket.h>
|
|
||||||
#include <MeshNet.h>
|
|
||||||
#include <plugins/WebSO.h>
|
|
||||||
#include <plugins/OtaTcpPlugin.cpp>
|
|
||||||
#include <plugins/WebServerPlugin.cpp>
|
|
||||||
#include <plugins/WebConfigPlugin.cpp>
|
|
||||||
#include <plugins/MeshManPlugin.cpp>
|
|
||||||
#include "Mediator.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace std::placeholders;
|
|
||||||
|
|
||||||
AsyncWebServer WEBSERVER(80);
|
|
||||||
|
|
||||||
class MeshApp : public MeshSprocket {
|
|
||||||
public:
|
|
||||||
Task heartbeatTask;
|
|
||||||
|
|
||||||
MeshApp(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : MeshSprocket(cfg) {
|
|
||||||
addPlugin(new OtaTcpPlugin(otaCfg));
|
|
||||||
addPlugin(new WebServerPlugin(webCfg, &WEBSERVER));
|
|
||||||
addPlugin(new WebConfigPlugin(&WEBSERVER));
|
|
||||||
addPlugin(new MeshManPlugin(&WEBSERVER));
|
|
||||||
subscribe("mesh/heartbeat", bind(&MeshApp::messageHandler, this, _1));
|
|
||||||
}
|
|
||||||
|
|
||||||
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
|
||||||
// call parent method that enables dispatching and plugins
|
|
||||||
MeshSprocket::activate(scheduler, network);
|
|
||||||
|
|
||||||
// add a task that sends stuff to the mesh
|
|
||||||
heartbeatTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this, net));
|
|
||||||
addTask(heartbeatTask);
|
|
||||||
|
|
||||||
return this;
|
|
||||||
} using MeshSprocket::activate;
|
|
||||||
|
|
||||||
void messageHandler(String msg){
|
|
||||||
Serial.println(String("MeshApp: ") + msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void heartbeat(MeshNet* network){
|
|
||||||
SprocketMessage msg; // = { "wirelos", "broadcast", "local", "alive", 0, };
|
|
||||||
msg.domain = "wirelos";
|
|
||||||
msg.to = "broadcast";
|
|
||||||
msg.payload = "alive";
|
|
||||||
msg.topic = "mesh/heartbeat";
|
|
||||||
msg.type = SprocketMessage::APP;
|
|
||||||
String msgStr = msg.toJsonString();
|
|
||||||
network->mesh.sendBroadcast(msgStr, true);
|
|
||||||
//String mMsg = String("hoi");
|
|
||||||
//publish("mediatorMsg", "hi mediator");
|
|
||||||
}
|
|
||||||
//void onMessage( uint32_t from, String &msg ) {
|
|
||||||
// Serial.printf("MeshApp onMessage: received from %u msg=%s\n", from, msg.c_str());
|
|
||||||
//}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
#include "config.h"
|
|
||||||
#include "MeshNet.h"
|
|
||||||
#include "MeshApp.h"
|
|
||||||
|
|
||||||
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 },
|
|
||||||
{ WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE }
|
|
||||||
);
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
sprocket.join(net);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
sprocket.loop();
|
|
||||||
yield();
|
|
||||||
}
|
|
||||||
@@ -1,112 +0,0 @@
|
|||||||
#ifndef __MESH_MQTT_BRIDGE_APP__
|
|
||||||
#define __MESH_MQTT_BRIDGE_APP__
|
|
||||||
|
|
||||||
#include <WiFiClient.h>
|
|
||||||
#include <painlessMesh.h>
|
|
||||||
#include <PubSubClient.h>
|
|
||||||
#include "Sprocket.h"
|
|
||||||
#include "MeshNet.h"
|
|
||||||
|
|
||||||
#define MQTT_TOPIC_FROM "mesh/from/"
|
|
||||||
#define MQTT_TOPIC_FROM_GATEWAY "mesh/from/gateway"
|
|
||||||
#define MQTT_TOPIC_TO_ALL "mesh/to/#"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace std::placeholders;
|
|
||||||
|
|
||||||
struct MqttConfig {
|
|
||||||
const char* clientName;
|
|
||||||
const char* brokerHost;
|
|
||||||
int brokerPort;
|
|
||||||
const char* topicRoot;
|
|
||||||
};
|
|
||||||
|
|
||||||
class MqttMeshBridge : public Sprocket {
|
|
||||||
public:
|
|
||||||
MeshNet* net;
|
|
||||||
PubSubClient* client;
|
|
||||||
WiFiClient wifiClient;
|
|
||||||
Task connectTask;
|
|
||||||
Task processTask;
|
|
||||||
MqttConfig mqttConfig;
|
|
||||||
|
|
||||||
MqttMeshBridge(SprocketConfig sprktCfg, MqttConfig cfg) : Sprocket(sprktCfg) {
|
|
||||||
mqttConfig = cfg;
|
|
||||||
}
|
|
||||||
|
|
||||||
Sprocket* activate(Scheduler* scheduler){
|
|
||||||
enableConnectTask(scheduler);
|
|
||||||
enableProcessTask(scheduler);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
|
||||||
Serial.println("activate MQTT bridge");
|
|
||||||
net = static_cast<MeshNet*>(network);
|
|
||||||
net->mesh.onReceive(bind(&MqttMeshBridge::receivedCallback,this, _1, _2));
|
|
||||||
client = new PubSubClient(mqttConfig.brokerHost, mqttConfig.brokerPort, bind(&MqttMeshBridge::mqttCallback, this, _1, _2, _3), wifiClient);
|
|
||||||
return activate(scheduler);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
void enableConnectTask(Scheduler* scheduler) {
|
|
||||||
connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::connect, this));
|
|
||||||
scheduler->addTask(connectTask);
|
|
||||||
connectTask.enable();
|
|
||||||
}
|
|
||||||
|
|
||||||
void enableProcessTask(Scheduler* scheduler) {
|
|
||||||
processTask.set(TASK_MILLISECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::process, this));
|
|
||||||
scheduler->addTask(processTask);
|
|
||||||
processTask.enable();
|
|
||||||
}
|
|
||||||
|
|
||||||
void process(){
|
|
||||||
client->loop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void connect() {
|
|
||||||
if (!client->connected()) {
|
|
||||||
if (client->connect(mqttConfig.clientName)) {
|
|
||||||
Serial.println("MQTT connected");
|
|
||||||
client->publish(MQTT_TOPIC_FROM_GATEWAY,"Ready!");
|
|
||||||
client->subscribe(MQTT_TOPIC_TO_ALL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void receivedCallback( uint32_t from, String &msg ) {
|
|
||||||
Serial.printf("bridge: Received from %u msg=%s\n", from, msg.c_str());
|
|
||||||
String topic = MQTT_TOPIC_FROM + String(from);
|
|
||||||
client->publish(topic.c_str(), msg.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
void mqttCallback(char* topic, uint8_t* payload, unsigned int length) {
|
|
||||||
char* cleanPayload = (char*)malloc(length+1);
|
|
||||||
payload[length] = '\0';
|
|
||||||
memcpy(cleanPayload, payload, length+1);
|
|
||||||
String msg = String(cleanPayload);
|
|
||||||
free(cleanPayload);
|
|
||||||
|
|
||||||
int topicRootLength = String(mqttConfig.topicRoot).length();
|
|
||||||
String targetStr = String(topic).substring(topicRootLength + 4);
|
|
||||||
|
|
||||||
if(targetStr == "gateway"){
|
|
||||||
if(msg == "getNodes") {
|
|
||||||
client->publish(MQTT_TOPIC_FROM_GATEWAY, net->mesh.subConnectionJson().c_str());
|
|
||||||
}
|
|
||||||
} else if(targetStr == "broadcast") {
|
|
||||||
net->mesh.sendBroadcast(msg);
|
|
||||||
} else {
|
|
||||||
uint32_t target = strtoul(targetStr.c_str(), NULL, 10);
|
|
||||||
if(net->mesh.isConnected(target)){
|
|
||||||
net->mesh.sendSingle(target, msg);
|
|
||||||
} else {
|
|
||||||
client->publish(MQTT_TOPIC_FROM_GATEWAY, "Client not connected!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#ifndef __BRIDGE_CONFIG__
|
|
||||||
#define __BRIDGE_CONFIG__
|
|
||||||
|
|
||||||
// Scheduler config
|
|
||||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
|
||||||
#define _TASK_STD_FUNCTION
|
|
||||||
|
|
||||||
// Chip config
|
|
||||||
#define SERIAL_BAUD_RATE 115200
|
|
||||||
#define STARTUP_DELAY 3000
|
|
||||||
|
|
||||||
// Mesh config
|
|
||||||
#define STATION_MODE 1
|
|
||||||
#define WIFI_CHANNEL 11
|
|
||||||
#define MESH_PORT 5555
|
|
||||||
#define MESH_PREFIX "whateverYouLike"
|
|
||||||
#define MESH_PASSWORD "somethingSneaky"
|
|
||||||
#define STATION_SSID "Th1ngs4P"
|
|
||||||
#define STATION_PASSWORD "th3r31sn0sp00n"
|
|
||||||
#define HOSTNAME "mqtt-bridge"
|
|
||||||
#define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION
|
|
||||||
|
|
||||||
// Bridge config
|
|
||||||
#define MQTT_CLIENT_NAME HOSTNAME
|
|
||||||
#define MQTT_BROKER "citadel.lan"
|
|
||||||
#define MQTT_PORT 1883
|
|
||||||
#define MQTT_TOPIC_ROOT "mesh"
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
#include "config.h"
|
|
||||||
#include "MeshNet.h"
|
|
||||||
#include "MqttMeshBridge.cpp"
|
|
||||||
|
|
||||||
MeshNet net({
|
|
||||||
STATION_MODE, WIFI_CHANNEL,
|
|
||||||
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
|
|
||||||
STATION_SSID, STATION_PASSWORD, HOSTNAME,
|
|
||||||
MESH_DEBUG_TYPES
|
|
||||||
});
|
|
||||||
|
|
||||||
MqttMeshBridge sprocket(
|
|
||||||
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
|
|
||||||
{ MQTT_CLIENT_NAME, MQTT_BROKER, MQTT_PORT, MQTT_TOPIC_ROOT }
|
|
||||||
);
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
sprocket.join(net);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
sprocket.loop();
|
|
||||||
yield();
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
|
|
||||||
#ifndef __WIFI_APP__
|
|
||||||
#define __WIFI_APP__
|
|
||||||
|
|
||||||
#include <TaskScheduler.h>
|
|
||||||
#include <Sprocket.h>
|
|
||||||
#include <plugins/WebSO.h>
|
|
||||||
#include <plugins/OtaTcpPlugin.cpp>
|
|
||||||
#include <plugins/WebServerPlugin.cpp>
|
|
||||||
#include <plugins/WebConfigPlugin.cpp>
|
|
||||||
#include "Mediator.h"
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
using namespace std::placeholders;
|
|
||||||
|
|
||||||
AsyncWebServer WEBSERVER(80);
|
|
||||||
|
|
||||||
class WiFiApp : public Sprocket {
|
|
||||||
public:
|
|
||||||
Scheduler* ts;
|
|
||||||
Task someTask;
|
|
||||||
WiFiApp(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) {
|
|
||||||
//addPlugin(new OtaTcpPlugin(otaCfg));
|
|
||||||
addPlugin(new WebServerPlugin(webCfg, &WEBSERVER));
|
|
||||||
addPlugin(new WebConfigPlugin(&WEBSERVER));
|
|
||||||
ts = new Scheduler();
|
|
||||||
}
|
|
||||||
|
|
||||||
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
|
||||||
Sprocket::activate(ts, network);
|
|
||||||
Serial.println("activate WiFiApp");
|
|
||||||
// add a task
|
|
||||||
someTask.set(TASK_SECOND, TASK_FOREVER, [](){
|
|
||||||
Serial.println("do stuff in task");
|
|
||||||
});
|
|
||||||
//addTask(someTask);
|
|
||||||
return this;
|
|
||||||
} using Sprocket::activate;
|
|
||||||
void loop(){
|
|
||||||
//Sprocket::loop();
|
|
||||||
ts->execute();
|
|
||||||
yield();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
#include "config.h"
|
|
||||||
#include "WiFiNet.h"
|
|
||||||
#include "WiFiApp.h"
|
|
||||||
|
|
||||||
/* WiFiConfig wifiCfg = {
|
|
||||||
.stationMode=SPROCKET_MODE,
|
|
||||||
.stationSSID=STATION_SSID,
|
|
||||||
.stationPassword=STATION_PASSWORD,
|
|
||||||
.apSSID=AP_SSID,
|
|
||||||
.apPassword=AP_PASSWORD,
|
|
||||||
.hostname=HOSTNAME,
|
|
||||||
.connectTimeout=CONNECT_TIMEOUT
|
|
||||||
}; */
|
|
||||||
|
|
||||||
WiFiNet net(SPROCKET_MODE,STATION_SSID, STATION_PASSWORD,AP_SSID, AP_PASSWORD,HOSTNAME,CONNECT_TIMEOUT);
|
|
||||||
WiFiApp sprocket(
|
|
||||||
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
|
|
||||||
{ OTA_PORT, OTA_PASSWORD },
|
|
||||||
{ WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE }
|
|
||||||
);
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
delay(3000);
|
|
||||||
sprocket.join(net);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
sprocket.loop();
|
|
||||||
yield();
|
|
||||||
}
|
|
||||||
24
src/examples/wifi/WiFiApp.h
Normal file
24
src/examples/wifi/WiFiApp.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
#ifndef __WIFI_APP__
|
||||||
|
#define __WIFI_APP__
|
||||||
|
|
||||||
|
#include <TaskScheduler.h>
|
||||||
|
#include <Sprocket.h>
|
||||||
|
#include <plugins/WebServerConfig.h>
|
||||||
|
#include <plugins/WebServerPlugin.cpp>
|
||||||
|
#include <plugins/WebConfigPlugin.cpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace std::placeholders;
|
||||||
|
|
||||||
|
class WiFiApp : public Sprocket {
|
||||||
|
public:
|
||||||
|
AsyncWebServer* server;
|
||||||
|
WiFiApp(SprocketConfig cfg, WebServerConfig webCfg) : Sprocket(cfg) {
|
||||||
|
server = new AsyncWebServer(webCfg.port);
|
||||||
|
addPlugin(new WebServerPlugin(webCfg, server));
|
||||||
|
addPlugin(new WebConfigPlugin(server));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -27,5 +27,6 @@
|
|||||||
#define WEB_CONTEXT_PATH "/"
|
#define WEB_CONTEXT_PATH "/"
|
||||||
#define WEB_DOC_ROOT "/www"
|
#define WEB_DOC_ROOT "/www"
|
||||||
#define WEB_DEFAULT_FILE "index.html"
|
#define WEB_DEFAULT_FILE "index.html"
|
||||||
|
#define WEB_SERVER_PORT 80
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
28
src/examples/wifi/main.cpp
Normal file
28
src/examples/wifi/main.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include "config.h"
|
||||||
|
#include "WiFiNet.h"
|
||||||
|
#include "WiFiApp.h"
|
||||||
|
|
||||||
|
WiFiNet wifi(
|
||||||
|
SPROCKET_MODE,
|
||||||
|
STATION_SSID,
|
||||||
|
STATION_PASSWORD,
|
||||||
|
AP_SSID,
|
||||||
|
AP_PASSWORD,
|
||||||
|
HOSTNAME,
|
||||||
|
CONNECT_TIMEOUT);
|
||||||
|
WiFiApp sprocket(
|
||||||
|
{STARTUP_DELAY, SERIAL_BAUD_RATE},
|
||||||
|
{WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE, WEB_SERVER_PORT});
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
delay(3000);
|
||||||
|
wifi.connect();
|
||||||
|
sprocket.activate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
sprocket.loop();
|
||||||
|
yield();
|
||||||
|
}
|
||||||
50
src/examples/wifiMesh/MeshApp.h
Normal file
50
src/examples/wifiMesh/MeshApp.h
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#ifndef __MESH_APP__
|
||||||
|
#define __MESH_APP__
|
||||||
|
|
||||||
|
#include <Sprocket.h>
|
||||||
|
#include <MeshNet.h>
|
||||||
|
#include <plugins/MeshNetworkPlugin.cpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace std::placeholders;
|
||||||
|
|
||||||
|
class MeshApp : public Sprocket
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Task heartbeatTask;
|
||||||
|
|
||||||
|
MeshApp(SprocketConfig cfg, MeshConfig meshCfg) : Sprocket(cfg)
|
||||||
|
{
|
||||||
|
addPlugin(new MeshNetworkPlugin(meshCfg));
|
||||||
|
subscribe("device/heartbeat", bind(&MeshApp::messageHandler, this, _1));
|
||||||
|
}
|
||||||
|
|
||||||
|
Sprocket *activate(Scheduler *scheduler)
|
||||||
|
{
|
||||||
|
// add a task that sends stuff to the mesh
|
||||||
|
heartbeatTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this));
|
||||||
|
addTask(heartbeatTask);
|
||||||
|
Serial.println("MeshApp activated");
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
using Sprocket::activate;
|
||||||
|
|
||||||
|
void messageHandler(String msg)
|
||||||
|
{
|
||||||
|
Serial.println(String("MeshApp: ") + msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void heartbeat()
|
||||||
|
{
|
||||||
|
SprocketMessage msg;
|
||||||
|
msg.domain = "wirelos";
|
||||||
|
msg.to = "broadcast";
|
||||||
|
msg.payload = "alive";
|
||||||
|
msg.topic = "device/heartbeat";
|
||||||
|
msg.type = SprocketMessage::APP;
|
||||||
|
String msgStr = msg.toJsonString();
|
||||||
|
publish("mesh/broadcast", msgStr);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
// Scheduler config
|
// Scheduler config
|
||||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||||
#define _TASK_STD_FUNCTION
|
#define _TASK_STD_FUNCTION
|
||||||
|
#define _TASK_PRIORITY
|
||||||
|
|
||||||
// Chip config
|
// Chip config
|
||||||
#define SERIAL_BAUD_RATE 115200
|
#define SERIAL_BAUD_RATE 115200
|
||||||
21
src/examples/wifiMesh/main.cpp
Normal file
21
src/examples/wifiMesh/main.cpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include "config.h"
|
||||||
|
#include "MeshApp.h"
|
||||||
|
|
||||||
|
MeshApp sprocket(
|
||||||
|
{STARTUP_DELAY, SERIAL_BAUD_RATE},
|
||||||
|
{SPROCKET_MODE, WIFI_CHANNEL,
|
||||||
|
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
|
||||||
|
STATION_SSID, STATION_PASSWORD, HOSTNAME,
|
||||||
|
MESH_DEBUG_TYPES});
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
delay(3000);
|
||||||
|
sprocket.activate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
sprocket.loop();
|
||||||
|
yield();
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
#include "ArduinoOTA.h"
|
#include "ArduinoOTA.h"
|
||||||
#include "MeshNet.h"
|
#include "MeshNet.h"
|
||||||
#include "Plugin.h"
|
#include "Plugin.h"
|
||||||
#include <plugins/WebSO.h>
|
#include <plugins/WebServerConfig.h>
|
||||||
#include <base/MeshSprocketConfig.h>
|
#include <base/MeshSprocketConfig.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
52
src/plugins/MeshNetworkPlugin.cpp
Normal file
52
src/plugins/MeshNetworkPlugin.cpp
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#ifndef __MESH_NETWORK_PLUGIN__
|
||||||
|
#define __MESH_NETWORK_PLUGIN__
|
||||||
|
|
||||||
|
#include "Plugin.h"
|
||||||
|
#include "TaskSchedulerDeclarations.h"
|
||||||
|
#include <Network.h>
|
||||||
|
#include <MeshNet.h>
|
||||||
|
#include "plugins/NetworkPlugin.cpp"
|
||||||
|
#include <SprocketMessage.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace std::placeholders;
|
||||||
|
|
||||||
|
class MeshNetworkPlugin : public NetworkPlugin
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
Scheduler *meshScheduler;
|
||||||
|
|
||||||
|
public:
|
||||||
|
MeshNetworkPlugin(MeshConfig cfg)
|
||||||
|
{
|
||||||
|
network = new MeshNet(cfg);
|
||||||
|
meshScheduler = new Scheduler();
|
||||||
|
}
|
||||||
|
|
||||||
|
void activate(Scheduler *userScheduler)
|
||||||
|
{
|
||||||
|
userScheduler->setHighPriorityScheduler(meshScheduler);
|
||||||
|
network->onReceive(bind(&MeshNetworkPlugin::dispatch, this, _1, _2));
|
||||||
|
subscribe("mesh/broadcast", bind(&MeshNetworkPlugin::broadcast, this, _1));
|
||||||
|
// TODO mesh/sendTo
|
||||||
|
NetworkPlugin::activate(meshScheduler);
|
||||||
|
}
|
||||||
|
void broadcast(String msg)
|
||||||
|
{
|
||||||
|
network->broadcast(msg, true);
|
||||||
|
}
|
||||||
|
void dispatch(uint32_t from, String &msg)
|
||||||
|
{
|
||||||
|
SprocketMessage sMsg;
|
||||||
|
sMsg.fromJsonString(msg);
|
||||||
|
if (sMsg.valid)
|
||||||
|
{
|
||||||
|
sMsg.from = String(from);
|
||||||
|
publish(sMsg.topic, sMsg.payload);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
publish("mesh/message", msg);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
28
src/plugins/NetworkPlugin.cpp
Normal file
28
src/plugins/NetworkPlugin.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef __NETWORK_PLUGIN__
|
||||||
|
#define __NETWORK_PLUGIN__
|
||||||
|
|
||||||
|
#include "TaskSchedulerDeclarations.h"
|
||||||
|
#include "Plugin.h"
|
||||||
|
#include "Network.h"
|
||||||
|
|
||||||
|
class NetworkPlugin : public Plugin
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
Network *network;
|
||||||
|
|
||||||
|
public:
|
||||||
|
NetworkPlugin() {}
|
||||||
|
NetworkPlugin(Network *net)
|
||||||
|
{
|
||||||
|
network = net;
|
||||||
|
}
|
||||||
|
|
||||||
|
void activate(Scheduler *userScheduler)
|
||||||
|
{
|
||||||
|
Serial.println("join network");
|
||||||
|
network->init(userScheduler);
|
||||||
|
network->connect();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -26,7 +26,7 @@ class OtaTcpPlugin : public Plugin {
|
|||||||
}
|
}
|
||||||
void connectUpdateNetwork() {
|
void connectUpdateNetwork() {
|
||||||
Serial.println("OTA connect to update-network");
|
Serial.println("OTA connect to update-network");
|
||||||
net->connectStation(1);
|
net->connectStation();
|
||||||
}
|
}
|
||||||
void enable() {
|
void enable() {
|
||||||
Serial.println("OTA enable");
|
Serial.println("OTA enable");
|
||||||
@@ -5,7 +5,8 @@
|
|||||||
#include "TaskSchedulerDeclarations.h"
|
#include "TaskSchedulerDeclarations.h"
|
||||||
#include "ArduinoOTA.h"
|
#include "ArduinoOTA.h"
|
||||||
#include "Plugin.h"
|
#include "Plugin.h"
|
||||||
#include <plugins/WebSO.h>
|
#include <plugins/WebServerConfig.h>
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
@@ -19,7 +20,7 @@ class WebConfigPlugin : public Plugin {
|
|||||||
server = webServer;
|
server = webServer;
|
||||||
server->serveStatic("/config.json", SPIFFS, "config.json");
|
server->serveStatic("/config.json", SPIFFS, "config.json");
|
||||||
}
|
}
|
||||||
void activate(Scheduler* userScheduler, Network* network){
|
void activate(Scheduler* userScheduler){
|
||||||
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
|
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||||
Serial.println("GET /heap");
|
Serial.println("GET /heap");
|
||||||
request->send(200, "text/plain", String(ESP.getFreeHeap()));
|
request->send(200, "text/plain", String(ESP.getFreeHeap()));
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
#ifndef __SHARED_PLUGINS__
|
#ifndef __WEB_SERVER_CONFIG__
|
||||||
#define __SHARED_PLUGINS__
|
#define __WEB_SERVER_CONFIG__
|
||||||
|
|
||||||
#include <ESPAsyncWebServer.h>
|
|
||||||
extern AsyncWebServer WEBSERVER;
|
|
||||||
|
|
||||||
struct WebServerConfig {
|
struct WebServerConfig {
|
||||||
const char* contextPath;
|
const char* contextPath;
|
||||||
const char* docRoot;
|
const char* docRoot;
|
||||||
const char* defaultFile;
|
const char* defaultFile;
|
||||||
|
int port;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -2,9 +2,10 @@
|
|||||||
#define __WEB_SERVER_PLUGIN__
|
#define __WEB_SERVER_PLUGIN__
|
||||||
|
|
||||||
#include <FS.h>
|
#include <FS.h>
|
||||||
|
#include <ESPAsyncWebServer.h>
|
||||||
#include "TaskSchedulerDeclarations.h"
|
#include "TaskSchedulerDeclarations.h"
|
||||||
#include "Plugin.h"
|
#include "Plugin.h"
|
||||||
#include <plugins/WebSO.h>
|
#include <plugins/WebServerConfig.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
@@ -18,7 +19,7 @@ class WebServerPlugin : public Plugin {
|
|||||||
config = cfg;
|
config = cfg;
|
||||||
server = webServer;
|
server = webServer;
|
||||||
}
|
}
|
||||||
void activate(Scheduler* userScheduler, Network* network){
|
void activate(Scheduler* userScheduler){
|
||||||
server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile);
|
server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile);
|
||||||
// TODO add auth if configured
|
// TODO add auth if configured
|
||||||
// server->setAuthentication("user", "pass");
|
// server->setAuthentication("user", "pass");
|
||||||
|
|||||||
Reference in New Issue
Block a user