mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-16 13:25:03 +01:00
fix basic example
This commit is contained in:
22
src/App.h
22
src/App.h
@@ -2,27 +2,15 @@
|
||||
#define _APP_H_
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
//#include "Sprocket.h"
|
||||
#include "Sprocket.h"
|
||||
#include "AppStack.h"
|
||||
|
||||
/* template <class T>
|
||||
class App {
|
||||
private:
|
||||
T* stack;
|
||||
public:
|
||||
App();
|
||||
App(T* stack);
|
||||
};
|
||||
*/
|
||||
#include "Network.h"
|
||||
|
||||
class App {
|
||||
protected:
|
||||
//Sprocket* sprocket;
|
||||
public:
|
||||
/* App(Sprocket* sprkt){
|
||||
sprocket = sprkt;
|
||||
} */
|
||||
virtual void activate(Scheduler* scheduler);
|
||||
virtual void join(Network& network) {};
|
||||
virtual void activate(Scheduler* scheduler, Network* network) {};
|
||||
virtual void activate(Scheduler* scheduler) {};
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,10 +1,56 @@
|
||||
#ifndef __MESHNET_H__
|
||||
#define __MESHNET_H__
|
||||
|
||||
#include <painlessMesh.h>
|
||||
|
||||
#include "Network.h"
|
||||
|
||||
class MeshNet : public Network {
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define MESH_PORT 5555
|
||||
|
||||
|
||||
|
||||
class MeshNet : public Network {
|
||||
public:
|
||||
painlessMesh mesh;
|
||||
Network* init(){
|
||||
Serial.println("init mesh");
|
||||
mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION ); // set before init() so that you can see startup messages
|
||||
mesh.init( MESH_PREFIX, MESH_PASSWORD, scheduler, MESH_PORT, WIFI_AP_STA, 11 );
|
||||
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));
|
||||
|
||||
return this;
|
||||
}
|
||||
Network* connect(){
|
||||
return this;
|
||||
}
|
||||
void update(){
|
||||
Serial.println("update mesh");
|
||||
mesh.update();
|
||||
}
|
||||
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",mesh.subConnectionJson().c_str());
|
||||
}
|
||||
|
||||
void nodeTimeAdjustedCallback(int32_t offset) {
|
||||
Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,10 +1,20 @@
|
||||
#ifndef __NETWORK_H__
|
||||
#define __NETWORK_H__
|
||||
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
|
||||
class Network {
|
||||
protected:
|
||||
Scheduler* scheduler;
|
||||
public:
|
||||
Network(){}
|
||||
virtual Network* connect();
|
||||
virtual Network* init() { return this; };
|
||||
virtual Network* connect() { return this; };
|
||||
virtual void update() {};
|
||||
Network* setScheduler(Scheduler* s) {
|
||||
scheduler = s;
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -9,7 +9,15 @@ Sprocket* Sprocket::init(SprocketConfig cfg){
|
||||
SPIFFS.begin();
|
||||
return this;
|
||||
}
|
||||
|
||||
Sprocket* Sprocket::join(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){
|
||||
stack = stk;
|
||||
return this;
|
||||
@@ -21,12 +29,15 @@ Sprocket* Sprocket::addTask(Task& tsk){
|
||||
return this;
|
||||
}
|
||||
|
||||
Sprocket* Sprocket::registerApp(App& app){
|
||||
Sprocket* Sprocket::app(App& app){
|
||||
//app.join(&network);
|
||||
//app.activate(&scheduler, network);
|
||||
app.activate(&scheduler);
|
||||
return this;
|
||||
}
|
||||
|
||||
void Sprocket::loop(){
|
||||
//network->update();
|
||||
scheduler.execute();
|
||||
//stack->loop();
|
||||
}
|
||||
@@ -5,9 +5,9 @@
|
||||
#include <TaskSchedulerDeclarations.h>
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "WiFiNet.h"
|
||||
#include "AppStack.h"
|
||||
#include "App.h"
|
||||
#include "Network.h"
|
||||
|
||||
struct SprocketConfig {
|
||||
int serialBaudRate;
|
||||
@@ -17,12 +17,14 @@ class Sprocket {
|
||||
private:
|
||||
AppStack* stack;
|
||||
Scheduler scheduler;
|
||||
Network* network;
|
||||
public:
|
||||
Sprocket();
|
||||
Sprocket* init(SprocketConfig);
|
||||
Sprocket* join(Network&);
|
||||
Sprocket* use(AppStack*);
|
||||
Sprocket* addTask(Task&);
|
||||
Sprocket* registerApp(App&);
|
||||
Sprocket* app(App&);
|
||||
void loop();
|
||||
};
|
||||
|
||||
|
||||
@@ -29,7 +29,8 @@ class WiFiNet : public Network {
|
||||
server = stack->server;
|
||||
return this;
|
||||
}
|
||||
WiFiNet* connect(){
|
||||
WiFiNet* init(){}
|
||||
WiFiNet* connect(){
|
||||
server = new AsyncWebServer(80);
|
||||
dns = new DNSServer();
|
||||
wifiManager = new AsyncWiFiManager(server, dns);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <TaskScheduler.h>
|
||||
#include "Sprocket.h"
|
||||
#include "AppStack.h"
|
||||
//#include "WiFiNet.h"
|
||||
#include "WiFiNet.h"
|
||||
|
||||
#include "ExampleApp.h"
|
||||
|
||||
@@ -13,9 +13,9 @@
|
||||
|
||||
SprocketConfig config = { SERIAL_BAUD_RATE };
|
||||
|
||||
Sprocket sprocket;
|
||||
AppStack stack;
|
||||
WiFiNet net;
|
||||
Sprocket sprocket;
|
||||
//AppStack stack;
|
||||
|
||||
ExampleApp app;
|
||||
|
||||
@@ -23,20 +23,17 @@ void setup() {
|
||||
|
||||
delay(STARTUP_DELAY);
|
||||
|
||||
Serial.println("setup");
|
||||
|
||||
//sprocket.use(&stack);
|
||||
sprocket.init(config);
|
||||
sprocket.registerApp(app);
|
||||
sprocket.app(app);
|
||||
sprocket.join(net);
|
||||
|
||||
net.connect();
|
||||
|
||||
|
||||
//net.connect();
|
||||
//stack.begin();
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
sprocket.loop();
|
||||
net.update();
|
||||
yield();
|
||||
}
|
||||
59
src/examples/mesh/MeshApp.h
Normal file
59
src/examples/mesh/MeshApp.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#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
|
||||
|
||||
class MeshApp : public App {
|
||||
public:
|
||||
Task someTask;
|
||||
MeshNet* net;
|
||||
MeshApp() /* App(sprkt) */ {
|
||||
Serial.println("joo");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
35
src/examples/mesh/mesh.cpp
Normal file
35
src/examples/mesh/mesh.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||
#define _TASK_STD_FUNCTION
|
||||
|
||||
#include "Network.h"
|
||||
#include "MeshNet.h"
|
||||
#include "Sprocket.h"
|
||||
#include "AppStack.h"
|
||||
#include "MeshApp.h"
|
||||
|
||||
#define SERIAL_BAUD_RATE 115200
|
||||
#define STARTUP_DELAY 3000
|
||||
|
||||
SprocketConfig config = { SERIAL_BAUD_RATE };
|
||||
|
||||
Sprocket sprocket;
|
||||
AppStack stack;
|
||||
MeshNet net;
|
||||
MeshApp app;
|
||||
|
||||
void setup() {
|
||||
|
||||
delay(STARTUP_DELAY);
|
||||
|
||||
sprocket.init(config);
|
||||
sprocket.join(&net);
|
||||
//sprocket.app(app);
|
||||
//sprocket.use(&stack);
|
||||
//stack.begin();
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
sprocket.loop();
|
||||
yield();
|
||||
}
|
||||
@@ -50,7 +50,7 @@ void setup() {
|
||||
//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 );
|
||||
mesh.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT, WIFI_AP_STA, 6 );
|
||||
mesh.onReceive(&receivedCallback);
|
||||
mesh.onNewConnection(&newConnectionCallback);
|
||||
mesh.onChangedConnections(&changedConnectionCallback);
|
||||
@@ -63,4 +63,4 @@ void setup() {
|
||||
void loop() {
|
||||
userScheduler.execute(); // it will run mesh scheduler as well
|
||||
mesh.update();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user