From 0e6d7f7f3d6d46e0e22b22bb61db3156c505d06d Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Sun, 10 Jun 2018 17:56:28 +0200 Subject: [PATCH] basic mesh node example app --- platformio.ini | 2 +- src/App.h | 8 ++- src/MeshNet.h | 10 +++- src/Network.h | 8 ++- src/Sprocket.cpp | 15 +++-- src/Sprocket.h | 3 +- src/WiFiNet.h | 8 ++- src/examples/basic/ExampleApp.h | 12 ++++ src/examples/basic/basic.cpp | 5 +- src/examples/mesh/MeshApp.h | 58 +++++++------------ src/examples/mesh/mesh.cpp | 8 +-- .../mesh/mesh_example_non_sprocket.cpp_ | 4 +- 12 files changed, 78 insertions(+), 63 deletions(-) diff --git a/platformio.ini b/platformio.ini index 59f00d7..9b6076d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ ; http://docs.platformio.org/page/projectconf.html [platformio] -env_default = basic +env_default = mesh [common] framework = arduino diff --git a/src/App.h b/src/App.h index be4753a..2543143 100644 --- a/src/App.h +++ b/src/App.h @@ -1,6 +1,7 @@ #ifndef _APP_H_ #define _APP_H_ +#include #include #include "Sprocket.h" #include "AppStack.h" @@ -8,9 +9,10 @@ class App { public: - virtual void join(Network& network) {}; - virtual void activate(Scheduler* scheduler, Network* network) {}; - virtual void activate(Scheduler* scheduler) {}; + virtual void join(Network&) {}; + virtual void server(AsyncWebServer*) {}; + virtual void activate(Scheduler*, Network*) {}; + virtual void activate(Scheduler*) {}; }; #endif \ No newline at end of file diff --git a/src/MeshNet.h b/src/MeshNet.h index cde167c..b9e4ed0 100644 --- a/src/MeshNet.h +++ b/src/MeshNet.h @@ -20,9 +20,10 @@ class MeshNet : public Network { painlessMesh mesh; Network* init(){ Serial.println("init mesh"); - mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION ); // set before init() so that you can see startup messages + //mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on + mesh.setDebugMsgTypes( ERROR | STARTUP); mesh.init( MESH_PREFIX, MESH_PASSWORD, scheduler, MESH_PORT, WIFI_AP_STA, 11 ); - mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2)); + //mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2)); mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1)); mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this)); mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1)); @@ -32,8 +33,10 @@ class MeshNet : public Network { Network* connect(){ return this; } + void broadcast(String msg){ + mesh.sendBroadcast(msg); + } void update(){ - Serial.println("update mesh"); mesh.update(); } void receivedCallback( uint32_t from, String &msg ) { @@ -41,6 +44,7 @@ class MeshNet : public Network { } void newConnectionCallback(uint32_t nodeId) { + id = nodeId; Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId); } diff --git a/src/Network.h b/src/Network.h index 6504d1c..5aaed9e 100644 --- a/src/Network.h +++ b/src/Network.h @@ -1,16 +1,20 @@ #ifndef __NETWORK_H__ #define __NETWORK_H__ +#include #include class Network { - protected: - Scheduler* scheduler; public: + uint32_t id = 0; Network(){} + Scheduler* scheduler; virtual Network* init() { return this; }; virtual Network* connect() { return this; }; virtual void update() {}; + virtual void broadcast(String msg){ + Serial.println("no-broadcast"); + }; Network* setScheduler(Scheduler* s) { scheduler = s; return this; diff --git a/src/Sprocket.cpp b/src/Sprocket.cpp index 5d80298..11fd87b 100644 --- a/src/Sprocket.cpp +++ b/src/Sprocket.cpp @@ -9,13 +9,17 @@ Sprocket* Sprocket::init(SprocketConfig cfg){ SPIFFS.begin(); return this; } +Sprocket* Sprocket::join(Network& net, App& app){ + join(net); + app.activate(&scheduler, &net); +} + Sprocket* Sprocket::join(Network& net){ - //network = net; + network = net; Serial.println("join network"); net.setScheduler(&scheduler); net.init(); net.connect(); - Serial.println("connected"); return this; } Sprocket* Sprocket::use(AppStack* stk){ @@ -30,14 +34,15 @@ Sprocket* Sprocket::addTask(Task& tsk){ } Sprocket* Sprocket::app(App& app){ - //app.join(&network); - //app.activate(&scheduler, network); app.activate(&scheduler); + //app.join(network); + //app.activate(&scheduler, &network); + //app.activate(&scheduler, network); return this; } void Sprocket::loop(){ - //network->update(); + network.update(); scheduler.execute(); //stack->loop(); } \ No newline at end of file diff --git a/src/Sprocket.h b/src/Sprocket.h index 51cc223..d3f6abd 100644 --- a/src/Sprocket.h +++ b/src/Sprocket.h @@ -17,11 +17,12 @@ class Sprocket { private: AppStack* stack; Scheduler scheduler; - Network* network; + Network network; public: Sprocket(); Sprocket* init(SprocketConfig); Sprocket* join(Network&); + Sprocket* join(Network&, App&); Sprocket* use(AppStack*); Sprocket* addTask(Task&); Sprocket* app(App&); diff --git a/src/WiFiNet.h b/src/WiFiNet.h index 79a22b2..1aaefb4 100644 --- a/src/WiFiNet.h +++ b/src/WiFiNet.h @@ -22,16 +22,20 @@ class WiFiNet : public Network { const char* hostName = "foo"; public: WiFiNet() : Network() { - server = new AsyncWebServer(80); + //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); + //server = new AsyncWebServer(80); dns = new DNSServer(); wifiManager = new AsyncWiFiManager(server, dns); wifiManager->autoConnect(/* apName, apPassword */); diff --git a/src/examples/basic/ExampleApp.h b/src/examples/basic/ExampleApp.h index 30d6fbb..8bad49a 100644 --- a/src/examples/basic/ExampleApp.h +++ b/src/examples/basic/ExampleApp.h @@ -4,6 +4,11 @@ //#include "Sprocket.h" #include #include "App.h" +#include + + +using namespace std; +using namespace std::placeholders; class ExampleApp : public App { public: @@ -19,6 +24,13 @@ class ExampleApp : public App { scheduler->addTask(someTask); someTask.enable(); } + void server(AsyncWebServer* srv) { + srv->on("/ping", HTTP_POST, bind(&ExampleApp::handlePingRequest, this, _1)); + } + void handlePingRequest(AsyncWebServerRequest *request) { + Serial.println("pinged"); + request->send(200, "text/html", "pong"); + } }; #endif \ No newline at end of file diff --git a/src/examples/basic/basic.cpp b/src/examples/basic/basic.cpp index cccd622..29deb66 100644 --- a/src/examples/basic/basic.cpp +++ b/src/examples/basic/basic.cpp @@ -18,15 +18,18 @@ Sprocket sprocket; //AppStack stack; ExampleApp app; +AsyncWebServer server(80); void setup() { delay(STARTUP_DELAY); + net.use(&server); + //sprocket.use(&stack); sprocket.init(config); - sprocket.app(app); sprocket.join(net); + sprocket.app(app); //net.connect(); //stack.begin(); diff --git a/src/examples/mesh/MeshApp.h b/src/examples/mesh/MeshApp.h index 8859881..c8b823d 100644 --- a/src/examples/mesh/MeshApp.h +++ b/src/examples/mesh/MeshApp.h @@ -1,58 +1,40 @@ -#ifndef __MESH_APP__ + #ifndef __MESH_APP__ #define __MESH_APP__ -//#include "Sprocket.h" #include - #include "App.h" #define MESH_PREFIX "whateverYouLike" #define MESH_PASSWORD "somethingSneaky" #define MESH_PORT 5555 +using namespace std; +using namespace std::placeholders; + class MeshApp : public App { public: Task someTask; MeshNet* net; - MeshApp() /* App(sprkt) */ { - Serial.println("joo"); + MeshApp() /* : App(sprkt) */ { + } + void advertise(MeshNet* network){ + String msg = "Hi, my name is " + String(network->id); + network->broadcast(msg); + } + void activate(Scheduler* scheduler, Network* network) { + net = static_cast(network); + someTask.set(TASK_SECOND * 5, TASK_FOREVER, + bind(&MeshApp::advertise, this, net)); + net->mesh.onReceive(bind(&MeshApp::receivedCallback,this, _1, _2)); + scheduler->addTask(someTask); + someTask.enable(); } - void activate(Scheduler* scheduler, MeshNet* network) { - Serial.println("activate"); - net = network; - Serial.println("join mesh"); - /* net->mesh.onReceive(bind(&MeshApp::receivedCallback,this, _1, _2)); - net->mesh.onNewConnection(bind(&MeshApp::newConnectionCallback, this, _1)); - net->mesh.onChangedConnections(bind(&MeshApp::changedConnectionCallback, this)); - net->mesh.onNodeTimeAdjusted(bind(&MeshApp::nodeTimeAdjustedCallback, this, _1)); */ - join(); - /* someTask.set(TASK_SECOND * 5, TASK_FOREVER, [this, network](){ - Serial.println("task triggered"); - String msg = "Hello from node "; - msg += network.mesh.getNodeId(); - network.mesh.sendBroadcast( msg ); - }); - scheduler->addTask(someTask); - someTask.enable(); */ - } - void join(){ - - } void receivedCallback( uint32_t from, String &msg ) { Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str()); - } - - void newConnectionCallback(uint32_t nodeId) { - Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId); - } - - void changedConnectionCallback() { - Serial.printf("Changed connections %s\n",net->mesh.subConnectionJson().c_str()); - } - - void nodeTimeAdjustedCallback(int32_t offset) { - Serial.printf("Adjusted time %u. Offset = %d\n", net->mesh.getNodeTime(),offset); + // respond in receive callback can cause an endless loop when all nodes run the same firmware + //String foo = String("cheerz back to ") + String(from); + //net->broadcast(foo); } }; diff --git a/src/examples/mesh/mesh.cpp b/src/examples/mesh/mesh.cpp index 2b01a2f..addd086 100644 --- a/src/examples/mesh/mesh.cpp +++ b/src/examples/mesh/mesh.cpp @@ -13,7 +13,7 @@ SprocketConfig config = { SERIAL_BAUD_RATE }; Sprocket sprocket; -AppStack stack; +//AppStack stack; MeshNet net; MeshApp app; @@ -22,14 +22,12 @@ void setup() { delay(STARTUP_DELAY); sprocket.init(config); - sprocket.join(&net); - //sprocket.app(app); - //sprocket.use(&stack); - //stack.begin(); + sprocket.join(net, app); } void loop() { + net.update(); sprocket.loop(); yield(); } \ No newline at end of file diff --git a/src/examples/mesh/mesh_example_non_sprocket.cpp_ b/src/examples/mesh/mesh_example_non_sprocket.cpp_ index 828f94a..e6d484e 100644 --- a/src/examples/mesh/mesh_example_non_sprocket.cpp_ +++ b/src/examples/mesh/mesh_example_non_sprocket.cpp_ @@ -47,8 +47,8 @@ void nodeTimeAdjustedCallback(int32_t offset) { void setup() { Serial.begin(115200); -//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on - mesh.setDebugMsgTypes( ERROR | STARTUP ); // set before init() so that you can see startup messages +mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on + //mesh.setDebugMsgTypes( ERROR | STARTUP ); // set before init() so that you can see startup messages mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT, WIFI_AP_STA, 6 ); mesh.onReceive(&receivedCallback);