From c127346394cec1a8c26d786e83a1f2c649c52b77 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Fri, 17 Aug 2018 14:29:18 +0200 Subject: [PATCH] add base MeshSprocket with OTA enabled --- platformio.ini | 3 +- src/Sprocket.h | 3 +- src/base/MeshSprocket.h | 46 ++++++++++++++++++++++++++++++ src/examples/mesh/MeshApp.cpp | 41 ++++++++++++-------------- src/examples/mesh/config.h | 6 +++- src/examples/mesh/main.cpp | 5 +++- src/examples/ota/SprocketOTA.cpp | 30 ++++++------------- src/{ => plugins}/OtaTcpPlugin.cpp | 0 8 files changed, 85 insertions(+), 49 deletions(-) create mode 100644 src/base/MeshSprocket.h rename src/{ => plugins}/OtaTcpPlugin.cpp (100%) diff --git a/platformio.ini b/platformio.ini index 6bb5b8c..11ad2ab 100644 --- a/platformio.ini +++ b/platformio.ini @@ -22,7 +22,7 @@ lib_deps = ESPAsyncTCP TaskScheduler SPIFFS - + ;[env:build] ;src_filter = +<*> - ;platform = ${common.platform} @@ -74,5 +74,6 @@ upload_flags = --auth=f4ncy monitor_baud = ${common.monitor_baud} framework = ${common.framework} lib_deps = ${common.lib_deps} + ESP8266mDNS painlessMesh ArduinoOTA \ No newline at end of file diff --git a/src/Sprocket.h b/src/Sprocket.h index de48cf2..f9a2532 100644 --- a/src/Sprocket.h +++ b/src/Sprocket.h @@ -34,8 +34,7 @@ class Sprocket { virtual Sprocket* activate(); virtual Sprocket* activate(Scheduler*) { return this; } virtual Sprocket* activate(Scheduler*, Network*) { return this; } - // TODO bind onMessage to network->onReceive - //virtual void onMessage(uint32_t from, String &msg) {}; + virtual void dispatch( uint32_t from, String &msg ) {} }; #endif \ No newline at end of file diff --git a/src/base/MeshSprocket.h b/src/base/MeshSprocket.h new file mode 100644 index 0000000..085bd06 --- /dev/null +++ b/src/base/MeshSprocket.h @@ -0,0 +1,46 @@ +#ifndef __MESH_SPROCKET__ +#define __MESH_SPROCKET__ + +#define DEBUG_ESP_OTA +#include +#include +#include +#include +#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(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 \ No newline at end of file diff --git a/src/examples/mesh/MeshApp.cpp b/src/examples/mesh/MeshApp.cpp index b41f39f..81150a6 100644 --- a/src/examples/mesh/MeshApp.cpp +++ b/src/examples/mesh/MeshApp.cpp @@ -2,42 +2,37 @@ #define __MESH_APP__ #include -#include +#include #include using namespace std; using namespace std::placeholders; -class MeshApp : public Sprocket { +class MeshApp : public MeshSprocket { public: - Task someTask; - MeshNet* net; - MeshApp(SprocketConfig cfg) : Sprocket(cfg) {} + Task heartbeatTask; + + MeshApp(SprocketConfig cfg, OtaConfig otaCfg) : MeshSprocket(cfg, otaCfg) { + + } + Sprocket* activate(Scheduler* scheduler, Network* network) { - net = static_cast(network); - net->onReceive(bind(&MeshApp::dispatch,this, _1, _2)); + // call parent method that enables dispatching and plugins + MeshSprocket::activate(scheduler, network); + // add a task that sends stuff to the mesh - someTask.set(TASK_SECOND * 5, TASK_FOREVER, - bind(&MeshApp::heartbeat, this, net)); - scheduler->addTask(someTask); - someTask.enable(); + heartbeatTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this, net)); + addTask(heartbeatTask); + return this; - } using Sprocket::activate; + } using MeshSprocket::activate; void heartbeat(MeshNet* network){ String msg = "{ \"payload \": 1 }"; - network->broadcast(msg); + network->mesh.sendBroadcast(msg, true); } - - void dispatch( uint32_t from, String &msg ) { - 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(); + void onMessage( uint32_t from, String &msg ) { + Serial.printf("MeshApp onMessage: received from %u msg=%s\n", from, msg.c_str()); } }; diff --git a/src/examples/mesh/config.h b/src/examples/mesh/config.h index 6b44a23..ab23017 100644 --- a/src/examples/mesh/config.h +++ b/src/examples/mesh/config.h @@ -18,7 +18,11 @@ #define STATION_SSID "Th1ngs4P" #define STATION_PASSWORD "th3r31sn0sp00n" #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 +// OTA config +#define OTA_PORT 8266 +#define OTA_PASSWORD "" + #endif \ No newline at end of file diff --git a/src/examples/mesh/main.cpp b/src/examples/mesh/main.cpp index bd04b7c..4361b38 100644 --- a/src/examples/mesh/main.cpp +++ b/src/examples/mesh/main.cpp @@ -8,7 +8,10 @@ MeshNet net({ STATION_SSID, STATION_PASSWORD, HOSTNAME, MESH_DEBUG_TYPES }); -MeshApp sprocket({ STARTUP_DELAY, SERIAL_BAUD_RATE }); +MeshApp sprocket( + { STARTUP_DELAY, SERIAL_BAUD_RATE }, + { OTA_PORT, OTA_PASSWORD } +); void setup() { sprocket.join(net); diff --git a/src/examples/ota/SprocketOTA.cpp b/src/examples/ota/SprocketOTA.cpp index ebe4776..8d110ab 100644 --- a/src/examples/ota/SprocketOTA.cpp +++ b/src/examples/ota/SprocketOTA.cpp @@ -2,38 +2,26 @@ #define __MESH_APP__ #define DEBUG_ESP_OTA -#include -#include #include -#include +#include #include "config.h" using namespace std; using namespace std::placeholders; -class SprocketOTA : public Sprocket { +// MeshSprocket base class integrates OTA plugin by default +class SprocketOTA : public MeshSprocket { public: - MeshNet* net; - OtaTcpPlugin* ota; + SprocketOTA(SprocketConfig cfg, OtaConfig otaCfg) : MeshSprocket(cfg, otaCfg) {} - SprocketOTA(SprocketConfig cfg, OtaConfig otaCfg) : Sprocket(cfg) { - ota = new OtaTcpPlugin(otaCfg); - } - Sprocket* activate(Scheduler* scheduler, Network* network) { - net = static_cast(network); - net->onReceive(bind(&SprocketOTA::dispatch,this, _1, _2)); - ota->enable(scheduler, network); + // call parent method that enables dispatching and plugins + MeshSprocket::activate(scheduler, network); return this; - } using Sprocket::activate; + } using MeshSprocket::activate; - void dispatch( uint32_t from, String &msg ) { - Serial.printf("Sprocket: received from %u msg=%s\n", from, msg.c_str()); - } - - void loop() { - net->update(); - scheduler.execute(); + void onMessage( uint32_t from, String &msg ) { + Serial.printf("SprocketOTA onMessage: received from %u msg=%s\n", from, msg.c_str()); } }; diff --git a/src/OtaTcpPlugin.cpp b/src/plugins/OtaTcpPlugin.cpp similarity index 100% rename from src/OtaTcpPlugin.cpp rename to src/plugins/OtaTcpPlugin.cpp