basic mesh node example app

This commit is contained in:
2018-06-10 17:56:28 +02:00
parent e9a04ac827
commit 0e6d7f7f3d
12 changed files with 78 additions and 63 deletions

View File

@@ -9,7 +9,7 @@
; http://docs.platformio.org/page/projectconf.html ; http://docs.platformio.org/page/projectconf.html
[platformio] [platformio]
env_default = basic env_default = mesh
[common] [common]
framework = arduino framework = arduino

View File

@@ -1,6 +1,7 @@
#ifndef _APP_H_ #ifndef _APP_H_
#define _APP_H_ #define _APP_H_
#include <ESPAsyncWebServer.h>
#include <TaskSchedulerDeclarations.h> #include <TaskSchedulerDeclarations.h>
#include "Sprocket.h" #include "Sprocket.h"
#include "AppStack.h" #include "AppStack.h"
@@ -8,9 +9,10 @@
class App { class App {
public: public:
virtual void join(Network& network) {}; virtual void join(Network&) {};
virtual void activate(Scheduler* scheduler, Network* network) {}; virtual void server(AsyncWebServer*) {};
virtual void activate(Scheduler* scheduler) {}; virtual void activate(Scheduler*, Network*) {};
virtual void activate(Scheduler*) {};
}; };
#endif #endif

View File

@@ -20,9 +20,10 @@ class MeshNet : public Network {
painlessMesh mesh; painlessMesh mesh;
Network* init(){ Network* init(){
Serial.println("init mesh"); 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.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.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));
@@ -32,8 +33,10 @@ class MeshNet : public Network {
Network* connect(){ Network* connect(){
return this; return this;
} }
void broadcast(String msg){
mesh.sendBroadcast(msg);
}
void update(){ void update(){
Serial.println("update mesh");
mesh.update(); mesh.update();
} }
void receivedCallback( uint32_t from, String &msg ) { void receivedCallback( uint32_t from, String &msg ) {
@@ -41,6 +44,7 @@ class MeshNet : public Network {
} }
void newConnectionCallback(uint32_t nodeId) { void newConnectionCallback(uint32_t nodeId) {
id = nodeId;
Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId); Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
} }

View File

@@ -1,16 +1,20 @@
#ifndef __NETWORK_H__ #ifndef __NETWORK_H__
#define __NETWORK_H__ #define __NETWORK_H__
#include <Arduino.h>
#include <TaskSchedulerDeclarations.h> #include <TaskSchedulerDeclarations.h>
class Network { class Network {
protected:
Scheduler* scheduler;
public: public:
uint32_t id = 0;
Network(){} Network(){}
Scheduler* scheduler;
virtual Network* init() { return this; }; virtual Network* init() { return this; };
virtual Network* connect() { return this; }; virtual Network* connect() { return this; };
virtual void update() {}; virtual void update() {};
virtual void broadcast(String msg){
Serial.println("no-broadcast");
};
Network* setScheduler(Scheduler* s) { Network* setScheduler(Scheduler* s) {
scheduler = s; scheduler = s;
return this; return this;

View File

@@ -9,13 +9,17 @@ Sprocket* Sprocket::init(SprocketConfig cfg){
SPIFFS.begin(); SPIFFS.begin();
return this; return this;
} }
Sprocket* Sprocket::join(Network& net, App& app){
join(net);
app.activate(&scheduler, &net);
}
Sprocket* Sprocket::join(Network& net){ Sprocket* Sprocket::join(Network& net){
//network = net; network = net;
Serial.println("join network"); Serial.println("join network");
net.setScheduler(&scheduler); net.setScheduler(&scheduler);
net.init(); net.init();
net.connect(); net.connect();
Serial.println("connected");
return this; return this;
} }
Sprocket* Sprocket::use(AppStack* stk){ Sprocket* Sprocket::use(AppStack* stk){
@@ -30,14 +34,15 @@ Sprocket* Sprocket::addTask(Task& tsk){
} }
Sprocket* Sprocket::app(App& app){ Sprocket* Sprocket::app(App& app){
//app.join(&network);
//app.activate(&scheduler, network);
app.activate(&scheduler); app.activate(&scheduler);
//app.join(network);
//app.activate(&scheduler, &network);
//app.activate(&scheduler, network);
return this; return this;
} }
void Sprocket::loop(){ void Sprocket::loop(){
//network->update(); network.update();
scheduler.execute(); scheduler.execute();
//stack->loop(); //stack->loop();
} }

View File

@@ -17,11 +17,12 @@ class Sprocket {
private: private:
AppStack* stack; AppStack* stack;
Scheduler scheduler; Scheduler scheduler;
Network* network; Network network;
public: public:
Sprocket(); Sprocket();
Sprocket* init(SprocketConfig); Sprocket* init(SprocketConfig);
Sprocket* join(Network&); Sprocket* join(Network&);
Sprocket* join(Network&, App&);
Sprocket* use(AppStack*); Sprocket* use(AppStack*);
Sprocket* addTask(Task&); Sprocket* addTask(Task&);
Sprocket* app(App&); Sprocket* app(App&);

View File

@@ -22,16 +22,20 @@ class WiFiNet : public Network {
const char* hostName = "foo"; const char* hostName = "foo";
public: public:
WiFiNet() : Network() { WiFiNet() : Network() {
server = new AsyncWebServer(80); //server = new AsyncWebServer(80);
dns = new DNSServer(); dns = new DNSServer();
} }
WiFiNet* use(AsyncWebServer* srv) {
server = srv;
return this;
}
WiFiNet* use(WebStack* stack) { WiFiNet* use(WebStack* stack) {
server = stack->server; server = stack->server;
return this; return this;
} }
WiFiNet* init(){} WiFiNet* init(){}
WiFiNet* connect(){ WiFiNet* connect(){
server = new AsyncWebServer(80); //server = new AsyncWebServer(80);
dns = new DNSServer(); dns = new DNSServer();
wifiManager = new AsyncWiFiManager(server, dns); wifiManager = new AsyncWiFiManager(server, dns);
wifiManager->autoConnect(/* apName, apPassword */); wifiManager->autoConnect(/* apName, apPassword */);

View File

@@ -4,6 +4,11 @@
//#include "Sprocket.h" //#include "Sprocket.h"
#include <TaskSchedulerDeclarations.h> #include <TaskSchedulerDeclarations.h>
#include "App.h" #include "App.h"
#include <ESPAsyncWebServer.h>
using namespace std;
using namespace std::placeholders;
class ExampleApp : public App { class ExampleApp : public App {
public: public:
@@ -19,6 +24,13 @@ class ExampleApp : public App {
scheduler->addTask(someTask); scheduler->addTask(someTask);
someTask.enable(); 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 #endif

View File

@@ -18,15 +18,18 @@ Sprocket sprocket;
//AppStack stack; //AppStack stack;
ExampleApp app; ExampleApp app;
AsyncWebServer server(80);
void setup() { void setup() {
delay(STARTUP_DELAY); delay(STARTUP_DELAY);
net.use(&server);
//sprocket.use(&stack); //sprocket.use(&stack);
sprocket.init(config); sprocket.init(config);
sprocket.app(app);
sprocket.join(net); sprocket.join(net);
sprocket.app(app);
//net.connect(); //net.connect();
//stack.begin(); //stack.begin();

View File

@@ -1,58 +1,40 @@
#ifndef __MESH_APP__ #ifndef __MESH_APP__
#define __MESH_APP__ #define __MESH_APP__
//#include "Sprocket.h"
#include <painlessMesh.h> #include <painlessMesh.h>
#include "App.h" #include "App.h"
#define MESH_PREFIX "whateverYouLike" #define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky" #define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555 #define MESH_PORT 5555
using namespace std;
using namespace std::placeholders;
class MeshApp : public App { class MeshApp : public App {
public: public:
Task someTask; Task someTask;
MeshNet* net; MeshNet* net;
MeshApp() /* App(sprkt) */ { MeshApp() /* : App(sprkt) */ {
Serial.println("joo");
} }
void advertise(MeshNet* network){
void activate(Scheduler* scheduler, MeshNet* network) { String msg = "Hi, my name is " + String(network->id);
Serial.println("activate"); network->broadcast(msg);
net = network; }
Serial.println("join mesh"); void activate(Scheduler* scheduler, Network* network) {
/* net->mesh.onReceive(bind(&MeshApp::receivedCallback,this, _1, _2)); net = static_cast<MeshNet*>(network);
net->mesh.onNewConnection(bind(&MeshApp::newConnectionCallback, this, _1)); someTask.set(TASK_SECOND * 5, TASK_FOREVER,
net->mesh.onChangedConnections(bind(&MeshApp::changedConnectionCallback, this)); bind(&MeshApp::advertise, this, net));
net->mesh.onNodeTimeAdjusted(bind(&MeshApp::nodeTimeAdjustedCallback, this, _1)); */ net->mesh.onReceive(bind(&MeshApp::receivedCallback,this, _1, _2));
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); scheduler->addTask(someTask);
someTask.enable(); */ someTask.enable();
} }
void join(){
}
void receivedCallback( uint32_t from, String &msg ) { void receivedCallback( uint32_t from, String &msg ) {
Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str()); Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
} // respond in receive callback can cause an endless loop when all nodes run the same firmware
//String foo = String("cheerz back to ") + String(from);
void newConnectionCallback(uint32_t nodeId) { //net->broadcast(foo);
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);
} }
}; };

View File

@@ -13,7 +13,7 @@
SprocketConfig config = { SERIAL_BAUD_RATE }; SprocketConfig config = { SERIAL_BAUD_RATE };
Sprocket sprocket; Sprocket sprocket;
AppStack stack; //AppStack stack;
MeshNet net; MeshNet net;
MeshApp app; MeshApp app;
@@ -22,14 +22,12 @@ void setup() {
delay(STARTUP_DELAY); delay(STARTUP_DELAY);
sprocket.init(config); sprocket.init(config);
sprocket.join(&net); sprocket.join(net, app);
//sprocket.app(app);
//sprocket.use(&stack);
//stack.begin();
} }
void loop() { void loop() {
net.update();
sprocket.loop(); sprocket.loop();
yield(); yield();
} }

View File

@@ -47,8 +47,8 @@ void nodeTimeAdjustedCallback(int32_t offset) {
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
//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 ); // set before init() so that you can see startup messages //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.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT, WIFI_AP_STA, 6 );
mesh.onReceive(&receivedCallback); mesh.onReceive(&receivedCallback);