mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-15 05:02:21 +01:00
Simplify
This commit is contained in:
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"terminal.integrated.env.linux": {
|
"terminal.integrated.env.linux": {
|
||||||
"PATH": "/home/master/.platformio/penv/bin:/home/master/.platformio/penv:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl",
|
"PATH": "/home/master/.platformio/penv/bin:/home/master/.platformio/penv:/home/master/bin:/home/master/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin",
|
||||||
"PLATFORMIO_CALLER": "vscode"
|
"PLATFORMIO_CALLER": "vscode"
|
||||||
},
|
},
|
||||||
"files.associations": {
|
"files.associations": {
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ board = esp12e
|
|||||||
upload_speed = 921600
|
upload_speed = 921600
|
||||||
monitor_baud = 115200
|
monitor_baud = 115200
|
||||||
lib_deps =
|
lib_deps =
|
||||||
|
Hash
|
||||||
ESP Async WebServer
|
ESP Async WebServer
|
||||||
ESPAsyncTCP
|
ESPAsyncTCP
|
||||||
TaskScheduler
|
TaskScheduler
|
||||||
@@ -45,8 +46,6 @@ 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}
|
||||||
ESPAsyncWifiManager
|
|
||||||
|
|
||||||
|
|
||||||
[env:mesh]
|
[env:mesh]
|
||||||
src_filter = +<*> +<examples/mesh/> -<examples/basic/> -<examples/mqttBridge/>
|
src_filter = +<*> +<examples/mesh/> -<examples/basic/> -<examples/mqttBridge/>
|
||||||
|
|||||||
18
src/App.h
18
src/App.h
@@ -1,18 +0,0 @@
|
|||||||
#ifndef _APP_H_
|
|
||||||
#define _APP_H_
|
|
||||||
|
|
||||||
#include <ESPAsyncWebServer.h>
|
|
||||||
#include <TaskSchedulerDeclarations.h>
|
|
||||||
#include "Sprocket.h"
|
|
||||||
#include "AppStack.h"
|
|
||||||
#include "Network.h"
|
|
||||||
|
|
||||||
class App {
|
|
||||||
public:
|
|
||||||
virtual void join(Network&) {};
|
|
||||||
virtual void server(AsyncWebServer*) {};
|
|
||||||
virtual void activate(Scheduler*, Network*) {};
|
|
||||||
virtual void activate(Scheduler*) {};
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
#include "AppStack.h"
|
|
||||||
|
|
||||||
AppStack::AppStack(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void AppStack::begin(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void AppStack::loop(){
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
#ifndef __APPSTACK_H_INCLUDED__
|
|
||||||
#define __APPSTACK_H_INCLUDED__
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <ESPAsyncWebServer.h>
|
|
||||||
//#include <TaskSchedulerDeclarations.h>
|
|
||||||
|
|
||||||
class AppStack {
|
|
||||||
public:
|
|
||||||
AppStack();
|
|
||||||
void begin();
|
|
||||||
void loop();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -4,40 +4,47 @@
|
|||||||
#include <painlessMesh.h>
|
#include <painlessMesh.h>
|
||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
|
|
||||||
#define STATION_MODE 1
|
struct MeshConfig {
|
||||||
#define WIFI_CHANNEL 11
|
int stationMode;
|
||||||
|
int channel;
|
||||||
#define MESH_PREFIX "whateverYouLike"
|
int meshPort;
|
||||||
#define MESH_PASSWORD "somethingSneaky"
|
const char* meshSSID;
|
||||||
#define MESH_PORT 5555
|
const char* meshPassword;
|
||||||
|
const char* stationSSID;
|
||||||
#define STATION_SSID "tErAx1d"
|
const char* stationPassword;
|
||||||
#define STATION_PWD "ramalamadingdong"
|
const char* hostname;
|
||||||
#define HOSTNAME "MeshNode"
|
uint16_t debugTypes;
|
||||||
|
};
|
||||||
|
|
||||||
class MeshNet : public Network {
|
class MeshNet : public Network {
|
||||||
public:
|
public:
|
||||||
painlessMesh mesh;
|
painlessMesh mesh;
|
||||||
|
|
||||||
|
MeshConfig config;
|
||||||
|
|
||||||
|
MeshNet(MeshConfig cfg) : Network() {
|
||||||
|
config = cfg;
|
||||||
|
}
|
||||||
|
|
||||||
Network* init(){
|
Network* init(){
|
||||||
|
|
||||||
Serial.println("init mesh");
|
Serial.println("init mesh");
|
||||||
//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( ERROR | STARTUP | CONNECTION);
|
mesh.setDebugMsgTypes( config.debugTypes );
|
||||||
mesh.init( MESH_PREFIX, MESH_PASSWORD, scheduler, MESH_PORT, WIFI_AP_STA, WIFI_CHANNEL );
|
mesh.init( config.meshSSID, config.meshPassword, scheduler, config.meshPort, WIFI_AP_STA, config.channel );
|
||||||
|
|
||||||
//mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2));
|
//mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2));
|
||||||
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(STATION_MODE){
|
if(config.stationMode){
|
||||||
Serial.println("connect station");
|
Serial.println("connect station");
|
||||||
mesh.stationManual(STATION_SSID, STATION_PWD);
|
mesh.stationManual(config.stationSSID, config.stationPassword);
|
||||||
mesh.setHostname(HOSTNAME);
|
mesh.setHostname(config.hostname);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
@@ -49,6 +56,7 @@ class MeshNet : public Network {
|
|||||||
mesh.sendBroadcast(msg);
|
mesh.sendBroadcast(msg);
|
||||||
}
|
}
|
||||||
void update(){
|
void update(){
|
||||||
|
// only needed when no scheduler was passed to mesh.init
|
||||||
mesh.update();
|
mesh.update();
|
||||||
}
|
}
|
||||||
void receivedCallback( uint32_t from, String &msg ) {
|
void receivedCallback( uint32_t from, String &msg ) {
|
||||||
|
|||||||
@@ -3,15 +3,14 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <TaskSchedulerDeclarations.h>
|
#include <TaskSchedulerDeclarations.h>
|
||||||
#include <WiFiClient.h>
|
|
||||||
|
|
||||||
class Network {
|
class Network {
|
||||||
public:
|
public:
|
||||||
uint32_t id = 0;
|
uint32_t id = 0;
|
||||||
Network(){}
|
Network(){}
|
||||||
Scheduler* scheduler;
|
Scheduler* scheduler;
|
||||||
//WiFiClient wifiClient;
|
|
||||||
virtual Network* init() { return this; };
|
virtual Network* init() { return this; };
|
||||||
|
virtual Network* init(Scheduler* s) { scheduler = s; return init(); };
|
||||||
virtual Network* connect() { return this; };
|
virtual Network* connect() { return this; };
|
||||||
virtual void update() {};
|
virtual void update() {};
|
||||||
virtual void broadcast(String msg){
|
virtual void broadcast(String msg){
|
||||||
|
|||||||
@@ -4,29 +4,28 @@ Sprocket::Sprocket(){
|
|||||||
Serial.println("init sprocket");
|
Serial.println("init sprocket");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Sprocket::Sprocket(SprocketConfig cfg){
|
||||||
|
config = cfg;
|
||||||
|
init(cfg);
|
||||||
|
}
|
||||||
|
|
||||||
Sprocket* Sprocket::init(SprocketConfig cfg){
|
Sprocket* Sprocket::init(SprocketConfig cfg){
|
||||||
delay(cfg.startupDelay);
|
delay(cfg.startupDelay);
|
||||||
Serial.begin(cfg.serialBaudRate);
|
Serial.begin(cfg.serialBaudRate);
|
||||||
SPIFFS.begin();
|
SPIFFS.begin();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
Sprocket* Sprocket::join(Network& net, App& app){
|
Sprocket* Sprocket::activate() {
|
||||||
join(net);
|
return activate(&scheduler);
|
||||||
app.activate(&scheduler, &net);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Sprocket* Sprocket::join(Network& net){
|
Sprocket* Sprocket::join(Network& net){
|
||||||
Serial.println("join network");
|
Serial.println("join network");
|
||||||
net.setScheduler(&scheduler);
|
net.init(&scheduler);
|
||||||
net.init();
|
|
||||||
net.connect();
|
net.connect();
|
||||||
activate(&scheduler, &net);
|
activate(&scheduler, &net);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
Sprocket* Sprocket::use(AppStack* stk){
|
|
||||||
stack = stk;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Sprocket* Sprocket::addTask(Task& tsk){
|
Sprocket* Sprocket::addTask(Task& tsk){
|
||||||
scheduler.addTask(tsk);
|
scheduler.addTask(tsk);
|
||||||
@@ -34,16 +33,6 @@ Sprocket* Sprocket::addTask(Task& tsk){
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sprocket* Sprocket::app(App& app){
|
|
||||||
app.activate(&scheduler);
|
|
||||||
//app.join(network);
|
|
||||||
//app.activate(&scheduler, &network);
|
|
||||||
//app.activate(&scheduler, network);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Sprocket::loop(){
|
void Sprocket::loop(){
|
||||||
//network.update();
|
|
||||||
scheduler.execute();
|
scheduler.execute();
|
||||||
//stack->loop();
|
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,9 @@
|
|||||||
#ifndef __SPROCKET_H__
|
#ifndef __SPROCKET_H__
|
||||||
#define __SPROCKET_H__
|
#define __SPROCKET_H__
|
||||||
|
|
||||||
|
|
||||||
#include <TaskSchedulerDeclarations.h>
|
#include <TaskSchedulerDeclarations.h>
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "AppStack.h"
|
#include <FS.h>
|
||||||
#include "App.h"
|
|
||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
|
|
||||||
struct SprocketConfig {
|
struct SprocketConfig {
|
||||||
@@ -17,18 +14,17 @@ struct SprocketConfig {
|
|||||||
class Sprocket {
|
class Sprocket {
|
||||||
protected:
|
protected:
|
||||||
Scheduler scheduler;
|
Scheduler scheduler;
|
||||||
private:
|
|
||||||
AppStack* stack; // REMOVE
|
|
||||||
public:
|
public:
|
||||||
|
SprocketConfig config;
|
||||||
Sprocket();
|
Sprocket();
|
||||||
|
Sprocket(SprocketConfig);
|
||||||
Sprocket* init(SprocketConfig);
|
Sprocket* init(SprocketConfig);
|
||||||
Sprocket* join(Network&);
|
Sprocket* join(Network&);
|
||||||
Sprocket* join(Network&, App&); // REMOVE
|
|
||||||
Sprocket* use(AppStack*); // REMOVE
|
|
||||||
Sprocket* addTask(Task&); // REMOVE ??
|
Sprocket* addTask(Task&); // REMOVE ??
|
||||||
Sprocket* app(App&); // REMOVE
|
|
||||||
virtual void loop();
|
virtual void loop();
|
||||||
virtual Sprocket* activate(Scheduler* scheduler, Network* network) {}
|
virtual Sprocket* activate();
|
||||||
|
virtual Sprocket* activate(Scheduler*) {}
|
||||||
|
virtual Sprocket* activate(Scheduler*, Network*) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
#ifndef __WEBSTACK_H_INCLUDED__
|
|
||||||
#define __WEBSTACK_H_INCLUDED__
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <ESPAsyncWebServer.h>
|
|
||||||
#include <AppStack.h>
|
|
||||||
|
|
||||||
class WebStack : public AppStack {
|
|
||||||
public:
|
|
||||||
AsyncWebServer* server;
|
|
||||||
AsyncWebSocket* socket;
|
|
||||||
WebStack() : AppStack(){
|
|
||||||
server = new AsyncWebServer(80);
|
|
||||||
initWebServer();
|
|
||||||
}
|
|
||||||
WebStack(AsyncWebServer* webServer) : AppStack(){
|
|
||||||
server = webServer;
|
|
||||||
initWebServer();
|
|
||||||
}
|
|
||||||
WebStack(AsyncWebServer* webServer, AsyncWebSocket* webSocket) : AppStack(){
|
|
||||||
server = webServer;
|
|
||||||
socket = webSocket;
|
|
||||||
initWebServer();
|
|
||||||
}
|
|
||||||
void initWebServer(){
|
|
||||||
//server->serveStatic(WEBSERVER_ROOT, SPIFFS, WEBSERVER_HTDOCS).setDefaultFile(WEBSERVER_DEFAULT_FILE);
|
|
||||||
//server->on(WEBCONFIG_GET_HEAP, HTTP_GET, HTTP_GET_HEAP);
|
|
||||||
}
|
|
||||||
void begin() {
|
|
||||||
server->begin();
|
|
||||||
AppStack::begin();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
#ifndef __WIFI_NET_H__
|
|
||||||
#define __WIFI_NET_H__
|
|
||||||
|
|
||||||
#if defined(ESP8266)
|
|
||||||
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
|
||||||
#include <ESP8266mDNS.h>
|
|
||||||
#else
|
|
||||||
#include <WiFi.h>
|
|
||||||
#endif
|
|
||||||
#include <WebStack.h>
|
|
||||||
#include <DNSServer.h>
|
|
||||||
#include <ESPAsyncWebServer.h>
|
|
||||||
#include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
|
|
||||||
|
|
||||||
#include "Network.h"
|
|
||||||
|
|
||||||
class WiFiNet : public Network {
|
|
||||||
private:
|
|
||||||
AsyncWiFiManager* wifiManager;
|
|
||||||
AsyncWebServer* server;
|
|
||||||
DNSServer* dns;
|
|
||||||
const char* hostName = "foo";
|
|
||||||
public:
|
|
||||||
WiFiNet() : Network() {
|
|
||||||
//server = new AsyncWebServer(80);
|
|
||||||
dns = new DNSServer();
|
|
||||||
}
|
|
||||||
WiFiNet* use(AsyncWebServer* srv) {
|
|
||||||
server = srv;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
WiFiNet* use(WebStack* stack) {
|
|
||||||
server = stack->server;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
WiFiNet* init(){}
|
|
||||||
WiFiNet* connect(){
|
|
||||||
//server = new AsyncWebServer(80);
|
|
||||||
dns = new DNSServer();
|
|
||||||
wifiManager = new AsyncWiFiManager(server, dns);
|
|
||||||
wifiManager->autoConnect(/* apName, apPassword */);
|
|
||||||
Serial.println("Hostname: " + String(hostName));
|
|
||||||
WiFi.hostname(hostName);
|
|
||||||
startMDNS();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
void startMDNS(){
|
|
||||||
if (!MDNS.begin(hostName)) {
|
|
||||||
Serial.println("Error setting up MDNS responder!");
|
|
||||||
} else {
|
|
||||||
Serial.println("mDNS responder started");
|
|
||||||
MDNS.addService("http", "tcp", 80);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -4,25 +4,25 @@
|
|||||||
//#include "Sprocket.h"
|
//#include "Sprocket.h"
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <TaskSchedulerDeclarations.h>
|
#include <TaskSchedulerDeclarations.h>
|
||||||
#include "App.h"
|
#include <Sprocket.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
|
|
||||||
class ExampleApp : public App {
|
class ExampleApp : public Sprocket {
|
||||||
public:
|
public:
|
||||||
Task someTask;
|
Task someTask;
|
||||||
ExampleApp() /* App(sprkt) */ {
|
ExampleApp() {
|
||||||
Serial.println("joo");
|
Serial.println("joo");
|
||||||
}
|
}
|
||||||
void activate(Scheduler* scheduler) {
|
Sprocket* activate(Scheduler* scheduler) {
|
||||||
Serial.println("activate");
|
Serial.println("activate");
|
||||||
someTask.set(TASK_SECOND, TASK_FOREVER, [this](){
|
someTask.set(TASK_SECOND, TASK_FOREVER, [this](){
|
||||||
Serial.println("do stuff in task");
|
Serial.println("do stuff in task");
|
||||||
});
|
});
|
||||||
scheduler->addTask(someTask);
|
scheduler->addTask(someTask);
|
||||||
someTask.enable();
|
someTask.enable();
|
||||||
}
|
} using Sprocket::activate;
|
||||||
void server(AsyncWebServer* srv) {
|
void server(AsyncWebServer* srv) {
|
||||||
srv->on("/ping", HTTP_POST, bind(&ExampleApp::handlePingRequest, this, _1));
|
srv->on("/ping", HTTP_POST, bind(&ExampleApp::handlePingRequest, this, _1));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,6 @@
|
|||||||
#define _TASK_STD_FUNCTION
|
#define _TASK_STD_FUNCTION
|
||||||
|
|
||||||
#include <TaskScheduler.h>
|
#include <TaskScheduler.h>
|
||||||
#include "Sprocket.h"
|
|
||||||
#include "AppStack.h"
|
|
||||||
#include "WiFiNet.h"
|
|
||||||
|
|
||||||
#include "ExampleApp.h"
|
#include "ExampleApp.h"
|
||||||
|
|
||||||
#define SERIAL_BAUD_RATE 115200
|
#define SERIAL_BAUD_RATE 115200
|
||||||
@@ -13,30 +9,17 @@
|
|||||||
|
|
||||||
SprocketConfig config = { STARTUP_DELAY, SERIAL_BAUD_RATE };
|
SprocketConfig config = { STARTUP_DELAY, SERIAL_BAUD_RATE };
|
||||||
|
|
||||||
WiFiNet net;
|
ExampleApp sprocket;
|
||||||
Sprocket sprocket;
|
|
||||||
//AppStack stack;
|
|
||||||
|
|
||||||
ExampleApp app;
|
|
||||||
AsyncWebServer server(80);
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
delay(STARTUP_DELAY);
|
delay(STARTUP_DELAY);
|
||||||
|
|
||||||
net.use(&server);
|
|
||||||
|
|
||||||
//sprocket.use(&stack);
|
|
||||||
sprocket.init(config);
|
sprocket.init(config);
|
||||||
sprocket.join(net);
|
sprocket.activate();
|
||||||
sprocket.app(app);
|
|
||||||
|
|
||||||
//net.connect();
|
|
||||||
//stack.begin();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
sprocket.loop();
|
sprocket.loop();
|
||||||
net.update();
|
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
@@ -2,24 +2,18 @@
|
|||||||
#define __MESH_APP__
|
#define __MESH_APP__
|
||||||
|
|
||||||
#include <painlessMesh.h>
|
#include <painlessMesh.h>
|
||||||
#include "App.h"
|
#include <Sprocket.h>
|
||||||
#include "MeshNet.h"
|
#include <MeshNet.h>
|
||||||
|
|
||||||
#define MESH_PREFIX "whateverYouLike"
|
|
||||||
#define MESH_PASSWORD "somethingSneaky"
|
|
||||||
#define MESH_PORT 5555
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
|
|
||||||
class MeshApp : public App {
|
class MeshApp : public Sprocket {
|
||||||
public:
|
public:
|
||||||
Task someTask;
|
Task someTask;
|
||||||
MeshNet* net;
|
MeshNet* net;
|
||||||
MeshApp() /* : App(sprkt) */ {
|
|
||||||
}
|
|
||||||
|
|
||||||
void activate(Scheduler* scheduler, Network* network) {
|
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
||||||
net = static_cast<MeshNet*>(network);
|
net = static_cast<MeshNet*>(network);
|
||||||
net->mesh.onReceive(bind(&MeshApp::receivedCallback,this, _1, _2));
|
net->mesh.onReceive(bind(&MeshApp::receivedCallback,this, _1, _2));
|
||||||
// add a task that sends stuff to the mesh
|
// add a task that sends stuff to the mesh
|
||||||
@@ -27,7 +21,7 @@ class MeshApp : public App {
|
|||||||
bind(&MeshApp::advertise, this, net));
|
bind(&MeshApp::advertise, this, net));
|
||||||
scheduler->addTask(someTask);
|
scheduler->addTask(someTask);
|
||||||
someTask.enable();
|
someTask.enable();
|
||||||
}
|
} using Sprocket::activate;
|
||||||
|
|
||||||
void advertise(MeshNet* network){
|
void advertise(MeshNet* network){
|
||||||
String msg = "Hi, my name is " + String(network->id);
|
String msg = "Hi, my name is " + String(network->id);
|
||||||
@@ -40,6 +34,10 @@ class MeshApp : public App {
|
|||||||
//String foo = String("cheerz back to ") + String(from);
|
//String foo = String("cheerz back to ") + String(from);
|
||||||
//net->broadcast(foo);
|
//net->broadcast(foo);
|
||||||
}
|
}
|
||||||
|
void loop() {
|
||||||
|
net->update();
|
||||||
|
scheduler.execute();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -4,7 +4,6 @@
|
|||||||
#include "Network.h"
|
#include "Network.h"
|
||||||
#include "MeshNet.h"
|
#include "MeshNet.h"
|
||||||
#include "Sprocket.h"
|
#include "Sprocket.h"
|
||||||
#include "AppStack.h"
|
|
||||||
#include "MeshApp.h"
|
#include "MeshApp.h"
|
||||||
|
|
||||||
#define SERIAL_BAUD_RATE 115200
|
#define SERIAL_BAUD_RATE 115200
|
||||||
@@ -12,22 +11,19 @@
|
|||||||
|
|
||||||
SprocketConfig config = { STARTUP_DELAY, SERIAL_BAUD_RATE };
|
SprocketConfig config = { STARTUP_DELAY, SERIAL_BAUD_RATE };
|
||||||
|
|
||||||
Sprocket sprocket;
|
|
||||||
//AppStack stack;
|
|
||||||
MeshNet net;
|
MeshNet net;
|
||||||
MeshApp app;
|
MeshApp sprocket;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
delay(STARTUP_DELAY);
|
delay(STARTUP_DELAY);
|
||||||
|
|
||||||
sprocket.init(config);
|
sprocket.init(config);
|
||||||
sprocket.join(net, app);
|
sprocket.join(net);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
net.update();
|
|
||||||
sprocket.loop();
|
sprocket.loop();
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
@@ -7,10 +7,6 @@
|
|||||||
#include "Sprocket.h"
|
#include "Sprocket.h"
|
||||||
#include "MeshNet.h"
|
#include "MeshNet.h"
|
||||||
|
|
||||||
#define MQTT_CLIENT_NAME "meshBridge"
|
|
||||||
#define MQTT_BROKER_HOST "citadel.lan"
|
|
||||||
#define MQTT_BROKER_PORT 1883
|
|
||||||
#define MQTT_TOPIC_ROOT "mesh"
|
|
||||||
#define MQTT_TOPIC_FROM "mesh/from/"
|
#define MQTT_TOPIC_FROM "mesh/from/"
|
||||||
#define MQTT_TOPIC_FROM_GATEWAY "mesh/from/gateway"
|
#define MQTT_TOPIC_FROM_GATEWAY "mesh/from/gateway"
|
||||||
#define MQTT_TOPIC_TO_ALL "mesh/to/#"
|
#define MQTT_TOPIC_TO_ALL "mesh/to/#"
|
||||||
@@ -22,35 +18,32 @@ struct MqttConfig {
|
|||||||
const char* clientName;
|
const char* clientName;
|
||||||
const char* brokerHost;
|
const char* brokerHost;
|
||||||
int brokerPort;
|
int brokerPort;
|
||||||
|
const char* topicRoot;
|
||||||
};
|
};
|
||||||
|
|
||||||
WiFiClient wifiClient;
|
|
||||||
|
|
||||||
class MqttMeshBridge : public Sprocket {
|
class MqttMeshBridge : public Sprocket {
|
||||||
public:
|
public:
|
||||||
MeshNet* net;
|
MeshNet* net;
|
||||||
PubSubClient* client;
|
PubSubClient* client;
|
||||||
|
WiFiClient wifiClient;
|
||||||
Task connectTask;
|
Task connectTask;
|
||||||
Task processTask;
|
Task processTask;
|
||||||
|
MqttConfig mqttConfig;
|
||||||
|
|
||||||
MqttMeshBridge(MqttConfig cfg) : Sprocket() {
|
MqttMeshBridge(SprocketConfig sprktCfg, MqttConfig cfg) : Sprocket(sprktCfg) {
|
||||||
|
mqttConfig = cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
Sprocket* activate(Scheduler* scheduler, Network* network) {
|
||||||
Serial.println("activate MQTT bridge");
|
Serial.println("activate MQTT bridge");
|
||||||
net = static_cast<MeshNet*>(network);
|
net = static_cast<MeshNet*>(network);
|
||||||
net->mesh.onReceive(bind(&MqttMeshBridge::receivedCallback,this, _1, _2));
|
net->mesh.onReceive(bind(&MqttMeshBridge::receivedCallback,this, _1, _2));
|
||||||
client = new PubSubClient(MQTT_BROKER_HOST, MQTT_BROKER_PORT, bind(&MqttMeshBridge::mqttCallback, this, _1, _2, _3), wifiClient);
|
client = new PubSubClient(mqttConfig.brokerHost, mqttConfig.brokerPort, bind(&MqttMeshBridge::mqttCallback, this, _1, _2, _3), wifiClient);
|
||||||
enableConnectTask(scheduler);
|
enableConnectTask(scheduler);
|
||||||
enableProcessTask(scheduler);
|
enableProcessTask(scheduler);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
|
||||||
net->update();
|
|
||||||
scheduler.execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
void enableConnectTask(Scheduler* scheduler) {
|
void enableConnectTask(Scheduler* scheduler) {
|
||||||
connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::connect, this));
|
connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::connect, this));
|
||||||
scheduler->addTask(connectTask);
|
scheduler->addTask(connectTask);
|
||||||
@@ -69,7 +62,7 @@ class MqttMeshBridge : public Sprocket {
|
|||||||
|
|
||||||
void connect() {
|
void connect() {
|
||||||
if (!client->connected()) {
|
if (!client->connected()) {
|
||||||
if (client->connect(MQTT_CLIENT_NAME)) {
|
if (client->connect(mqttConfig.clientName)) {
|
||||||
Serial.println("MQTT connected");
|
Serial.println("MQTT connected");
|
||||||
client->publish(MQTT_TOPIC_FROM_GATEWAY,"Ready!");
|
client->publish(MQTT_TOPIC_FROM_GATEWAY,"Ready!");
|
||||||
client->subscribe(MQTT_TOPIC_TO_ALL);
|
client->subscribe(MQTT_TOPIC_TO_ALL);
|
||||||
@@ -90,7 +83,7 @@ class MqttMeshBridge : public Sprocket {
|
|||||||
String msg = String(cleanPayload);
|
String msg = String(cleanPayload);
|
||||||
free(cleanPayload);
|
free(cleanPayload);
|
||||||
|
|
||||||
int topicRootLength = String(MQTT_TOPIC_ROOT).length();
|
int topicRootLength = String(mqttConfig.topicRoot).length();
|
||||||
String targetStr = String(topic).substring(topicRootLength + 4);
|
String targetStr = String(topic).substring(topicRootLength + 4);
|
||||||
|
|
||||||
if(targetStr == "gateway"){
|
if(targetStr == "gateway"){
|
||||||
|
|||||||
@@ -1,22 +1,43 @@
|
|||||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||||
#define _TASK_STD_FUNCTION
|
#define _TASK_STD_FUNCTION
|
||||||
|
|
||||||
#include "Network.h"
|
#include <MeshNet.h>
|
||||||
#include "MeshNet.h"
|
|
||||||
#include "Sprocket.h"
|
|
||||||
#include "AppStack.h"
|
|
||||||
#include "MqttMeshBridge.h"
|
#include "MqttMeshBridge.h"
|
||||||
|
|
||||||
#define SERIAL_BAUD_RATE 115200
|
// Chip config
|
||||||
#define STARTUP_DELAY 3000
|
#define SERIAL_BAUD_RATE 115200
|
||||||
|
#define STARTUP_DELAY 3000
|
||||||
|
|
||||||
SprocketConfig config = { STARTUP_DELAY, SERIAL_BAUD_RATE };
|
// 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-mesh-bridge"
|
||||||
|
#define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION
|
||||||
|
|
||||||
MeshNet net;
|
// Bridge config
|
||||||
MqttMeshBridge sprocket({"","",1883});
|
#define MQTT_CLIENT_NAME HOSTNAME
|
||||||
|
#define MQTT_BROKER "iot.eclipse.org"
|
||||||
|
#define MQTT_PORT 1883
|
||||||
|
#define MQTT_TOPIC_ROOT "mesh/"
|
||||||
|
|
||||||
|
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() {
|
void setup() {
|
||||||
sprocket.init(config);
|
|
||||||
sprocket.join(net);
|
sprocket.join(net);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user