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
TaskScheduler
SPIFFS
;[env:build]
;src_filter = +<*> -<examples/>
;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

View File

@@ -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

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__
#include <painlessMesh.h>
#include <Sprocket.h>
#include <base/MeshSprocket.h>
#include <MeshNet.h>
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<MeshNet*>(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());
}
};

View File

@@ -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

View File

@@ -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);

View File

@@ -2,38 +2,26 @@
#define __MESH_APP__
#define DEBUG_ESP_OTA
#include <painlessMesh.h>
#include <Sprocket.h>
#include <MeshNet.h>
#include <OtaTcpPlugin.cpp>
#include <base/MeshSprocket.h>
#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<MeshNet*>(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());
}
};