mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-15 05:02:21 +01:00
mesh example
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
; http://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[platformio]
|
||||
env_default = build
|
||||
env_default = mesh
|
||||
|
||||
[common]
|
||||
framework = arduino
|
||||
@@ -18,9 +18,9 @@ lib_deps =
|
||||
ESP Async WebServer
|
||||
ESPAsyncTCP
|
||||
TaskScheduler
|
||||
; painlessMesh
|
||||
|
||||
[env:build]
|
||||
src_filter = +<*> -<examples/>
|
||||
#platform = https://github.com/platformio/platform-espressif8266.git#feature/stage
|
||||
#platform = https://github.com/platformio/platform-espressif8266.git
|
||||
#platform = espressif8266@~1.6.0
|
||||
@@ -32,4 +32,14 @@ framework = ${common.framework}
|
||||
lib_deps = ${common.lib_deps}
|
||||
#build_flags = -DLED_PIN=2 -g
|
||||
;upload_port = /dev/ttyUSB0
|
||||
;upload_port = 192.168.1.168
|
||||
;upload_port = 192.168.1.168
|
||||
|
||||
[env:mesh]
|
||||
src_filter = +<*> +<examples/mesh/> -<examples/basic/>
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
upload_speed = 921600
|
||||
monitor_baud = 115200
|
||||
framework = ${common.framework}
|
||||
lib_deps = ${common.lib_deps}
|
||||
painlessMesh
|
||||
10
src/MeshNet.h
Normal file
10
src/MeshNet.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef __MESHNET_H__
|
||||
#define __MESHNET_H__
|
||||
|
||||
#include "Network.h"
|
||||
|
||||
class MeshNet : public Network {
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
10
src/Network.h
Normal file
10
src/Network.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef __NETWORK_H__
|
||||
#define __NETWORK_H__
|
||||
|
||||
class Network {
|
||||
public:
|
||||
Network(){}
|
||||
virtual Network* connect();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -4,6 +4,12 @@ Sprocket::Sprocket(){
|
||||
Serial.println("init sprocket");
|
||||
}
|
||||
|
||||
Sprocket* Sprocket::init(SprocketConfig cfg){
|
||||
Serial.begin(cfg.serialBaudRate);
|
||||
SPIFFS.begin();
|
||||
return this;
|
||||
}
|
||||
|
||||
Sprocket* Sprocket::use(AppStack* stk){
|
||||
stack = stk;
|
||||
return this;
|
||||
@@ -12,10 +18,12 @@ Sprocket* Sprocket::use(AppStack* stk){
|
||||
Sprocket* Sprocket::addTask(Task& tsk){
|
||||
scheduler.addTask(tsk);
|
||||
tsk.enable();
|
||||
return this;
|
||||
}
|
||||
|
||||
Sprocket* Sprocket::registerApp(App& app){
|
||||
app.activate(&scheduler);
|
||||
return this;
|
||||
}
|
||||
|
||||
void Sprocket::loop(){
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
#include "AppStack.h"
|
||||
#include "App.h"
|
||||
|
||||
struct SprocketConfig {
|
||||
int serialBaudRate;
|
||||
};
|
||||
|
||||
class Sprocket {
|
||||
private:
|
||||
@@ -16,9 +19,10 @@ class Sprocket {
|
||||
Scheduler scheduler;
|
||||
public:
|
||||
Sprocket();
|
||||
Sprocket* use(AppStack* stack);
|
||||
Sprocket* addTask(Task& tsk);
|
||||
Sprocket* registerApp(App& app);
|
||||
Sprocket* init(SprocketConfig);
|
||||
Sprocket* use(AppStack*);
|
||||
Sprocket* addTask(Task&);
|
||||
Sprocket* registerApp(App&);
|
||||
void loop();
|
||||
};
|
||||
|
||||
|
||||
@@ -7,33 +7,37 @@
|
||||
#else
|
||||
#include <WiFi.h>
|
||||
#endif
|
||||
|
||||
#include <WebStack.h>
|
||||
#include <DNSServer.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
|
||||
|
||||
class WiFiNet {
|
||||
#include "Network.h"
|
||||
|
||||
class WiFiNet : public Network {
|
||||
private:
|
||||
AsyncWiFiManager* wifiManager;
|
||||
AsyncWebServer* server;
|
||||
DNSServer* dns;
|
||||
const char* hostName = "foo";
|
||||
public:
|
||||
WiFiNet(){
|
||||
//server = new AsyncWebServer(80);
|
||||
WiFiNet() : Network() {
|
||||
server = new AsyncWebServer(80);
|
||||
dns = new DNSServer();
|
||||
}
|
||||
WiFiNet* use(WebStack* stack) {
|
||||
server = stack->server;
|
||||
return this;
|
||||
}
|
||||
void connect(/* char const *apName, char const *apPassword */){
|
||||
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)) {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||
#define _TASK_STD_FUNCTION
|
||||
|
||||
@@ -12,23 +11,26 @@
|
||||
#define SERIAL_BAUD_RATE 115200
|
||||
#define STARTUP_DELAY 3000
|
||||
|
||||
SprocketConfig config = { SERIAL_BAUD_RATE };
|
||||
|
||||
Sprocket sprocket;
|
||||
AppStack stack;
|
||||
WiFiNet net;
|
||||
|
||||
|
||||
ExampleApp app;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(SERIAL_BAUD_RATE);
|
||||
SPIFFS.begin();
|
||||
|
||||
delay(STARTUP_DELAY);
|
||||
|
||||
Serial.println("setup");
|
||||
//net.use(&stack)->connect();
|
||||
|
||||
|
||||
//sprocket.use(&stack);
|
||||
sprocket.init(config);
|
||||
sprocket.registerApp(app);
|
||||
|
||||
net.connect();
|
||||
|
||||
|
||||
//stack.begin();
|
||||
|
||||
66
src/examples/mesh/mesh_example.cpp
Normal file
66
src/examples/mesh/mesh_example.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
//************************************************************
|
||||
// this is a simple example that uses the painlessMesh library
|
||||
//
|
||||
// 1. sends a silly message to every node on the mesh at a random time between 1 and 5 seconds
|
||||
// 2. prints anything it receives to Serial.print
|
||||
//
|
||||
//
|
||||
//************************************************************
|
||||
#include "painlessMesh.h"
|
||||
|
||||
#define MESH_PREFIX "whateverYouLike"
|
||||
#define MESH_PASSWORD "somethingSneaky"
|
||||
#define MESH_PORT 5555
|
||||
|
||||
Scheduler userScheduler; // to control your personal task
|
||||
painlessMesh mesh;
|
||||
|
||||
// User stub
|
||||
void sendMessage() ; // Prototype so PlatformIO doesn't complain
|
||||
|
||||
Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
|
||||
|
||||
void sendMessage() {
|
||||
String msg = "Hello from node ";
|
||||
msg += mesh.getNodeId();
|
||||
mesh.sendBroadcast( msg );
|
||||
taskSendMessage.setInterval( random( TASK_SECOND * 1, TASK_SECOND * 5 ));
|
||||
}
|
||||
|
||||
// Needed for painless library
|
||||
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);
|
||||
}
|
||||
|
||||
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.init( MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT );
|
||||
mesh.onReceive(&receivedCallback);
|
||||
mesh.onNewConnection(&newConnectionCallback);
|
||||
mesh.onChangedConnections(&changedConnectionCallback);
|
||||
mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
|
||||
|
||||
userScheduler.addTask( taskSendMessage );
|
||||
taskSendMessage.enable();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
userScheduler.execute(); // it will run mesh scheduler as well
|
||||
mesh.update();
|
||||
}
|
||||
Reference in New Issue
Block a user