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
[platformio]
env_default = basic
env_default = mesh
[common]
framework = arduino

View File

@@ -1,6 +1,7 @@
#ifndef _APP_H_
#define _APP_H_
#include <ESPAsyncWebServer.h>
#include <TaskSchedulerDeclarations.h>
#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

View File

@@ -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);
}

View File

@@ -1,16 +1,20 @@
#ifndef __NETWORK_H__
#define __NETWORK_H__
#include <Arduino.h>
#include <TaskSchedulerDeclarations.h>
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;

View File

@@ -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();
}

View File

@@ -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&);

View File

@@ -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 */);

View File

@@ -4,6 +4,11 @@
//#include "Sprocket.h"
#include <TaskSchedulerDeclarations.h>
#include "App.h"
#include <ESPAsyncWebServer.h>
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

View File

@@ -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();

View File

@@ -1,58 +1,40 @@
#ifndef __MESH_APP__
#ifndef __MESH_APP__
#define __MESH_APP__
//#include "Sprocket.h"
#include <painlessMesh.h>
#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<MeshNet*>(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);
}
};

View File

@@ -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();
}

View File

@@ -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);