add base MeshSprocket with OTA enabled

This commit is contained in:
2018-08-17 14:29:18 +02:00
parent a94aaa6804
commit c127346394
8 changed files with 85 additions and 49 deletions

View File

@@ -22,7 +22,7 @@ lib_deps =
ESPAsyncTCP ESPAsyncTCP
TaskScheduler TaskScheduler
SPIFFS SPIFFS
;[env:build] ;[env:build]
;src_filter = +<*> -<examples/> ;src_filter = +<*> -<examples/>
;platform = ${common.platform} ;platform = ${common.platform}
@@ -74,5 +74,6 @@ upload_flags = --auth=f4ncy
monitor_baud = ${common.monitor_baud} monitor_baud = ${common.monitor_baud}
framework = ${common.framework} framework = ${common.framework}
lib_deps = ${common.lib_deps} lib_deps = ${common.lib_deps}
ESP8266mDNS
painlessMesh painlessMesh
ArduinoOTA ArduinoOTA

View File

@@ -34,8 +34,7 @@ class Sprocket {
virtual Sprocket* activate(); virtual Sprocket* activate();
virtual Sprocket* activate(Scheduler*) { return this; } virtual Sprocket* activate(Scheduler*) { return this; }
virtual Sprocket* activate(Scheduler*, Network*) { return this; } virtual Sprocket* activate(Scheduler*, Network*) { return this; }
// TODO bind onMessage to network->onReceive virtual void dispatch( uint32_t from, String &msg ) {}
//virtual void onMessage(uint32_t from, String &msg) {};
}; };
#endif #endif

46
src/base/MeshSprocket.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef __MESH_SPROCKET__
#define __MESH_SPROCKET__
#define DEBUG_ESP_OTA
#include <painlessMesh.h>
#include <Sprocket.h>
#include <MeshNet.h>
#include <plugins/OtaTcpPlugin.cpp>
#include "config.h"
using namespace std;
using namespace std::placeholders;
class MeshSprocket : public Sprocket {
public:
MeshNet* net;
OtaTcpPlugin* ota;
MeshSprocket(SprocketConfig cfg, OtaConfig otaCfg) : Sprocket(cfg) {
ota = new OtaTcpPlugin(otaCfg);
}
Sprocket* activate(Scheduler* scheduler, Network* network) {
net = static_cast<MeshNet*>(network);
net->onReceive(bind(&MeshSprocket::dispatch,this, _1, _2));
// enable plugins
ota->enable(scheduler, network);
return this;
} using Sprocket::activate;
virtual void onMessage(uint32_t from, String &msg) {
Serial.printf("MeshSprocket onMessage: received from %u msg=%s\n", from, msg.c_str());
};
void dispatch( uint32_t from, String &msg ) {
// TODO handle OTA before passing to onMessage
onMessage(from, msg);
}
void loop() {
net->update();
scheduler.execute();
}
};
#endif

View File

@@ -2,42 +2,37 @@
#define __MESH_APP__ #define __MESH_APP__
#include <painlessMesh.h> #include <painlessMesh.h>
#include <Sprocket.h> #include <base/MeshSprocket.h>
#include <MeshNet.h> #include <MeshNet.h>
using namespace std; using namespace std;
using namespace std::placeholders; using namespace std::placeholders;
class MeshApp : public Sprocket { class MeshApp : public MeshSprocket {
public: public:
Task someTask; Task heartbeatTask;
MeshNet* net;
MeshApp(SprocketConfig cfg) : Sprocket(cfg) {} MeshApp(SprocketConfig cfg, OtaConfig otaCfg) : MeshSprocket(cfg, otaCfg) {
}
Sprocket* activate(Scheduler* scheduler, Network* network) { Sprocket* activate(Scheduler* scheduler, Network* network) {
net = static_cast<MeshNet*>(network); // call parent method that enables dispatching and plugins
net->onReceive(bind(&MeshApp::dispatch,this, _1, _2)); MeshSprocket::activate(scheduler, network);
// add a task that sends stuff to the mesh // add a task that sends stuff to the mesh
someTask.set(TASK_SECOND * 5, TASK_FOREVER, heartbeatTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this, net));
bind(&MeshApp::heartbeat, this, net)); addTask(heartbeatTask);
scheduler->addTask(someTask);
someTask.enable();
return this; return this;
} using Sprocket::activate; } using MeshSprocket::activate;
void heartbeat(MeshNet* network){ void heartbeat(MeshNet* network){
String msg = "{ \"payload \": 1 }"; String msg = "{ \"payload \": 1 }";
network->broadcast(msg); network->mesh.sendBroadcast(msg, true);
} }
void onMessage( uint32_t from, String &msg ) {
void dispatch( uint32_t from, String &msg ) { Serial.printf("MeshApp onMessage: received from %u msg=%s\n", from, msg.c_str());
Serial.printf("MeshApp: 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);
//net->broadcast(foo);
}
void loop() {
net->update();
scheduler.execute();
} }
}; };

View File

@@ -18,7 +18,11 @@
#define STATION_SSID "Th1ngs4P" #define STATION_SSID "Th1ngs4P"
#define STATION_PASSWORD "th3r31sn0sp00n" #define STATION_PASSWORD "th3r31sn0sp00n"
#define HOSTNAME "mesh-node" #define HOSTNAME "mesh-node"
#define MESH_DEBUG_TYPES ERROR | CONNECTION | COMMUNICATION #define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION
//ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE //ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE
// OTA config
#define OTA_PORT 8266
#define OTA_PASSWORD ""
#endif #endif

View File

@@ -8,7 +8,10 @@ MeshNet net({
STATION_SSID, STATION_PASSWORD, HOSTNAME, STATION_SSID, STATION_PASSWORD, HOSTNAME,
MESH_DEBUG_TYPES MESH_DEBUG_TYPES
}); });
MeshApp sprocket({ STARTUP_DELAY, SERIAL_BAUD_RATE }); MeshApp sprocket(
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
{ OTA_PORT, OTA_PASSWORD }
);
void setup() { void setup() {
sprocket.join(net); sprocket.join(net);

View File

@@ -2,38 +2,26 @@
#define __MESH_APP__ #define __MESH_APP__
#define DEBUG_ESP_OTA #define DEBUG_ESP_OTA
#include <painlessMesh.h>
#include <Sprocket.h>
#include <MeshNet.h> #include <MeshNet.h>
#include <OtaTcpPlugin.cpp> #include <base/MeshSprocket.h>
#include "config.h" #include "config.h"
using namespace std; using namespace std;
using namespace std::placeholders; using namespace std::placeholders;
class SprocketOTA : public Sprocket { // MeshSprocket base class integrates OTA plugin by default
class SprocketOTA : public MeshSprocket {
public: public:
MeshNet* net; SprocketOTA(SprocketConfig cfg, OtaConfig otaCfg) : MeshSprocket(cfg, otaCfg) {}
OtaTcpPlugin* ota;
SprocketOTA(SprocketConfig cfg, OtaConfig otaCfg) : Sprocket(cfg) {
ota = new OtaTcpPlugin(otaCfg);
}
Sprocket* activate(Scheduler* scheduler, Network* network) { Sprocket* activate(Scheduler* scheduler, Network* network) {
net = static_cast<MeshNet*>(network); // call parent method that enables dispatching and plugins
net->onReceive(bind(&SprocketOTA::dispatch,this, _1, _2)); MeshSprocket::activate(scheduler, network);
ota->enable(scheduler, network);
return this; return this;
} using Sprocket::activate; } using MeshSprocket::activate;
void dispatch( uint32_t from, String &msg ) { void onMessage( uint32_t from, String &msg ) {
Serial.printf("Sprocket: received from %u msg=%s\n", from, msg.c_str()); Serial.printf("SprocketOTA onMessage: received from %u msg=%s\n", from, msg.c_str());
}
void loop() {
net->update();
scheduler.execute();
} }
}; };