mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-15 21:18:21 +01:00
mesh example
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
; http://docs.platformio.org/page/projectconf.html
|
; http://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[platformio]
|
[platformio]
|
||||||
env_default = build
|
env_default = mesh
|
||||||
|
|
||||||
[common]
|
[common]
|
||||||
framework = arduino
|
framework = arduino
|
||||||
@@ -18,9 +18,9 @@ lib_deps =
|
|||||||
ESP Async WebServer
|
ESP Async WebServer
|
||||||
ESPAsyncTCP
|
ESPAsyncTCP
|
||||||
TaskScheduler
|
TaskScheduler
|
||||||
; painlessMesh
|
|
||||||
|
|
||||||
[env:build]
|
[env:build]
|
||||||
|
src_filter = +<*> -<examples/>
|
||||||
#platform = https://github.com/platformio/platform-espressif8266.git#feature/stage
|
#platform = https://github.com/platformio/platform-espressif8266.git#feature/stage
|
||||||
#platform = https://github.com/platformio/platform-espressif8266.git
|
#platform = https://github.com/platformio/platform-espressif8266.git
|
||||||
#platform = espressif8266@~1.6.0
|
#platform = espressif8266@~1.6.0
|
||||||
@@ -32,4 +32,14 @@ framework = ${common.framework}
|
|||||||
lib_deps = ${common.lib_deps}
|
lib_deps = ${common.lib_deps}
|
||||||
#build_flags = -DLED_PIN=2 -g
|
#build_flags = -DLED_PIN=2 -g
|
||||||
;upload_port = /dev/ttyUSB0
|
;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");
|
Serial.println("init sprocket");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Sprocket* Sprocket::init(SprocketConfig cfg){
|
||||||
|
Serial.begin(cfg.serialBaudRate);
|
||||||
|
SPIFFS.begin();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
Sprocket* Sprocket::use(AppStack* stk){
|
Sprocket* Sprocket::use(AppStack* stk){
|
||||||
stack = stk;
|
stack = stk;
|
||||||
return this;
|
return this;
|
||||||
@@ -12,10 +18,12 @@ Sprocket* Sprocket::use(AppStack* stk){
|
|||||||
Sprocket* Sprocket::addTask(Task& tsk){
|
Sprocket* Sprocket::addTask(Task& tsk){
|
||||||
scheduler.addTask(tsk);
|
scheduler.addTask(tsk);
|
||||||
tsk.enable();
|
tsk.enable();
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Sprocket* Sprocket::registerApp(App& app){
|
Sprocket* Sprocket::registerApp(App& app){
|
||||||
app.activate(&scheduler);
|
app.activate(&scheduler);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sprocket::loop(){
|
void Sprocket::loop(){
|
||||||
|
|||||||
@@ -9,6 +9,9 @@
|
|||||||
#include "AppStack.h"
|
#include "AppStack.h"
|
||||||
#include "App.h"
|
#include "App.h"
|
||||||
|
|
||||||
|
struct SprocketConfig {
|
||||||
|
int serialBaudRate;
|
||||||
|
};
|
||||||
|
|
||||||
class Sprocket {
|
class Sprocket {
|
||||||
private:
|
private:
|
||||||
@@ -16,9 +19,10 @@ class Sprocket {
|
|||||||
Scheduler scheduler;
|
Scheduler scheduler;
|
||||||
public:
|
public:
|
||||||
Sprocket();
|
Sprocket();
|
||||||
Sprocket* use(AppStack* stack);
|
Sprocket* init(SprocketConfig);
|
||||||
Sprocket* addTask(Task& tsk);
|
Sprocket* use(AppStack*);
|
||||||
Sprocket* registerApp(App& app);
|
Sprocket* addTask(Task&);
|
||||||
|
Sprocket* registerApp(App&);
|
||||||
void loop();
|
void loop();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -7,33 +7,37 @@
|
|||||||
#else
|
#else
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <WebStack.h>
|
#include <WebStack.h>
|
||||||
#include <DNSServer.h>
|
#include <DNSServer.h>
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
#include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
|
#include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
|
||||||
|
|
||||||
class WiFiNet {
|
#include "Network.h"
|
||||||
|
|
||||||
|
class WiFiNet : public Network {
|
||||||
private:
|
private:
|
||||||
AsyncWiFiManager* wifiManager;
|
AsyncWiFiManager* wifiManager;
|
||||||
AsyncWebServer* server;
|
AsyncWebServer* server;
|
||||||
DNSServer* dns;
|
DNSServer* dns;
|
||||||
const char* hostName = "foo";
|
const char* hostName = "foo";
|
||||||
public:
|
public:
|
||||||
WiFiNet(){
|
WiFiNet() : Network() {
|
||||||
//server = new AsyncWebServer(80);
|
server = new AsyncWebServer(80);
|
||||||
dns = new DNSServer();
|
dns = new DNSServer();
|
||||||
}
|
}
|
||||||
WiFiNet* use(WebStack* stack) {
|
WiFiNet* use(WebStack* stack) {
|
||||||
server = stack->server;
|
server = stack->server;
|
||||||
return this;
|
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 = new AsyncWiFiManager(server, dns);
|
||||||
wifiManager->autoConnect(/* apName, apPassword */);
|
wifiManager->autoConnect(/* apName, apPassword */);
|
||||||
Serial.println("Hostname: " + String(hostName));
|
Serial.println("Hostname: " + String(hostName));
|
||||||
WiFi.hostname(hostName);
|
WiFi.hostname(hostName);
|
||||||
startMDNS();
|
startMDNS();
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
void startMDNS(){
|
void startMDNS(){
|
||||||
if (!MDNS.begin(hostName)) {
|
if (!MDNS.begin(hostName)) {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
#define _TASK_SLEEP_ON_IDLE_RUN
|
#define _TASK_SLEEP_ON_IDLE_RUN
|
||||||
#define _TASK_STD_FUNCTION
|
#define _TASK_STD_FUNCTION
|
||||||
|
|
||||||
@@ -12,23 +11,26 @@
|
|||||||
#define SERIAL_BAUD_RATE 115200
|
#define SERIAL_BAUD_RATE 115200
|
||||||
#define STARTUP_DELAY 3000
|
#define STARTUP_DELAY 3000
|
||||||
|
|
||||||
|
SprocketConfig config = { SERIAL_BAUD_RATE };
|
||||||
|
|
||||||
Sprocket sprocket;
|
Sprocket sprocket;
|
||||||
AppStack stack;
|
AppStack stack;
|
||||||
WiFiNet net;
|
WiFiNet net;
|
||||||
|
|
||||||
|
|
||||||
ExampleApp app;
|
ExampleApp app;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(SERIAL_BAUD_RATE);
|
|
||||||
SPIFFS.begin();
|
|
||||||
delay(STARTUP_DELAY);
|
delay(STARTUP_DELAY);
|
||||||
|
|
||||||
Serial.println("setup");
|
Serial.println("setup");
|
||||||
//net.use(&stack)->connect();
|
|
||||||
|
|
||||||
//sprocket.use(&stack);
|
//sprocket.use(&stack);
|
||||||
|
sprocket.init(config);
|
||||||
sprocket.registerApp(app);
|
sprocket.registerApp(app);
|
||||||
|
|
||||||
|
net.connect();
|
||||||
|
|
||||||
|
|
||||||
//stack.begin();
|
//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