move webserver stuff to plugin

This commit is contained in:
2018-08-29 09:03:38 +02:00
parent 4c32af3e1b
commit 52fdf0e5e0
15 changed files with 156 additions and 24 deletions

View File

@@ -1,8 +1,12 @@
# Sprocket-Core # Sprocket-Lib
Library to build a mesh network of single purpose nodes. Library to build a mesh network of Sprockets.
## Lifecycle
TODO docs
sprocket.init -> Serial.begin & SPIFFS.begin
sprocket.join(network) -> network.init & sprocket.activate -> plugins.activate
network->onReceive -> sprocket.dispatch -> plugins.onMessage &sprocket.onMessage
## WTF is a sprocket?
A sprocket is a device that has a single purpose, for example a PIR sensor node that notifies other nodes if there is motion or an LED node that lights up when a message is received.
# Useful commands # Useful commands
```sh ```sh

7
data/www/index.html Normal file
View File

@@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
<h1>Sprocket</h1>
</body>
</html>

View File

@@ -55,7 +55,9 @@ lib_deps = ${common.lib_deps}
painlessMesh painlessMesh
ESP8266mDNS ESP8266mDNS
ArduinoOTA ArduinoOTA
;upload_port = 192.168.1.247 ESP Async WebServer
ESPAsyncTCP
upload_port = 192.168.1.247
[env:meshMqttBridge] [env:meshMqttBridge]
src_filter = +<*> -<examples/> +<examples/meshMqttBridge/> src_filter = +<*> -<examples/> +<examples/meshMqttBridge/>

View File

@@ -43,6 +43,7 @@ void MeshNet::sendTo(uint32_t target, String msg){
void MeshNet::broadcast(String msg){ void MeshNet::broadcast(String msg){
mesh.sendBroadcast(msg); mesh.sendBroadcast(msg);
} }
void MeshNet::update(){ void MeshNet::update(){
// only needed when no scheduler was passed to mesh.init // only needed when no scheduler was passed to mesh.init

View File

@@ -9,10 +9,10 @@ class Plugin {
protected: protected:
Scheduler* scheduler; Scheduler* scheduler;
public: public:
virtual void setup(Scheduler*, Network*); virtual void activate(Scheduler*, Network*);
virtual void enable(); virtual void enable(){};
virtual void disable(); virtual void disable(){};
virtual void onMessage(MeshMessage msg); virtual void onMessage(MeshMessage msg){};
}; };
#endif #endif

View File

@@ -20,7 +20,7 @@ Sprocket* Sprocket::activate() {
} }
Sprocket* Sprocket::activate(Scheduler* scheduler, Network* network) { Sprocket* Sprocket::activate(Scheduler* scheduler, Network* network) {
// setup plugins // setup plugins
setupPlugins(scheduler, network); activatePlugins(scheduler, network);
return this; return this;
} }
@@ -54,9 +54,9 @@ void Sprocket::addPlugin(Plugin* p){
plugins.push_back(p); plugins.push_back(p);
} }
void Sprocket::setupPlugins(Scheduler* scheduler, Network* network){ void Sprocket::activatePlugins(Scheduler* scheduler, Network* network){
for(Plugin* p : plugins){ for(Plugin* p : plugins){
p->setup(scheduler, network); p->activate(scheduler, network);
} }
} }

View File

@@ -36,7 +36,7 @@ class Sprocket {
virtual void dispatch( uint32_t from, String &msg ); virtual void dispatch( uint32_t from, String &msg );
void addPlugin(Plugin* p); void addPlugin(Plugin* p);
void setupPlugins(Scheduler* scheduler, Network* network); void activatePlugins(Scheduler* scheduler, Network* network);
void dispatchMessageToPlugins(MeshMessage msg); void dispatchMessageToPlugins(MeshMessage msg);
}; };

View File

@@ -8,18 +8,25 @@
#include <MeshNet.h> #include <MeshNet.h>
#include <base/MeshMessage.h> #include <base/MeshMessage.h>
#include <base/MeshSprocketConfig.h> #include <base/MeshSprocketConfig.h>
#include <plugins/WebSO.h>
#include <plugins/OtaTcpPlugin.cpp> #include <plugins/OtaTcpPlugin.cpp>
#include <plugins/WebServerPlugin.cpp>
#include <plugins/WebConfigPlugin.cpp>
#include "config.h" #include "config.h"
using namespace std; using namespace std;
using namespace std::placeholders; using namespace std::placeholders;
AsyncWebServer WEBSERVER(80);
class MeshSprocket : public Sprocket { class MeshSprocket : public Sprocket {
public: public:
MeshNet* net; MeshNet* net;
MeshSprocket(SprocketConfig cfg, OtaConfig otaCfg) : Sprocket(cfg) { MeshSprocket(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) {
addPlugin(new OtaTcpPlugin(otaCfg)); addPlugin(new OtaTcpPlugin(otaCfg));
addPlugin(new WebServerPlugin(webCfg, &WEBSERVER));
addPlugin(new WebConfigPlugin(&WEBSERVER));
} }
Sprocket* activate(Scheduler* scheduler, Network* network) { Sprocket* activate(Scheduler* scheduler, Network* network) {

View File

@@ -8,12 +8,13 @@
using namespace std; using namespace std;
using namespace std::placeholders; using namespace std::placeholders;
class MeshApp : public MeshSprocket { class MeshApp : public MeshSprocket {
public: public:
Task heartbeatTask; Task heartbeatTask;
MeshApp(SprocketConfig cfg, OtaConfig otaCfg) : MeshSprocket(cfg, otaCfg) { MeshApp(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : MeshSprocket(cfg, otaCfg, webCfg) {
} }
Sprocket* activate(Scheduler* scheduler, Network* network) { Sprocket* activate(Scheduler* scheduler, Network* network) {
@@ -23,7 +24,7 @@ class MeshApp : public MeshSprocket {
// add a task that sends stuff to the mesh // add a task that sends stuff to the mesh
heartbeatTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this, net)); heartbeatTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this, net));
addTask(heartbeatTask); addTask(heartbeatTask);
return this; return this;
} using MeshSprocket::activate; } using MeshSprocket::activate;

View File

@@ -25,4 +25,9 @@
#define OTA_PORT 8266 #define OTA_PORT 8266
#define OTA_PASSWORD "" #define OTA_PASSWORD ""
// WebServer
#define WEB_CONTEXT_PATH "/"
#define WEB_DOC_ROOT "/www"
#define WEB_DEFAULT_FILE "index.html"
#endif #endif

View File

@@ -1,11 +1,6 @@
#include "config.h" #include "config.h"
#include "MeshNet.h" #include "MeshNet.h"
#include "MeshApp.cpp" #include "MeshApp.h"
MeshApp sprocket(
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
{ OTA_PORT, OTA_PASSWORD }
);
MeshNet net({ MeshNet net({
SPROCKET_MODE, WIFI_CHANNEL, SPROCKET_MODE, WIFI_CHANNEL,
@@ -13,6 +8,11 @@ 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 },
{ OTA_PORT, OTA_PASSWORD },
{ WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE }
);
void setup() { void setup() {
sprocket.join(net); sprocket.join(net);

View File

@@ -44,7 +44,7 @@ class OtaTcpPlugin : public Plugin {
WiFi.gatewayIP().printTo(Serial); WiFi.gatewayIP().printTo(Serial);
} }
} }
void setup(Scheduler* userScheduler, Network* network){ void activate(Scheduler* userScheduler, Network* network){
// connect done in network class? // connect done in network class?
//connectUpdateNetwork(network); //connectUpdateNetwork(network);
net = static_cast<MeshNet*>(network); net = static_cast<MeshNet*>(network);
@@ -79,6 +79,8 @@ class OtaTcpPlugin : public Plugin {
else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("OTA: Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed"); else if (error == OTA_END_ERROR) Serial.println("OTA: End Failed");
}); });
enable();
} }
void disable(){ void disable(){
otaTask.disable(); otaTask.disable();

View File

@@ -0,0 +1,57 @@
#ifndef __WEB_CONFIG_PLUGIN_H__
#define __WEB_CONFIG_PLUGIN_H__
#include "TaskSchedulerDeclarations.h"
#include "ArduinoOTA.h"
#include "MeshNet.h"
#include "Plugin.h"
#include <plugins/WebSO.h>
#include <base/MeshSprocketConfig.h>
using namespace std;
using namespace std::placeholders;
class WebConfigPlugin : public Plugin {
private:
MeshNet* net;
AsyncWebServer* server;
public:
WebConfigPlugin(AsyncWebServer* webServer){
server = webServer;
}
void activate(Scheduler* userScheduler, Network* network){
net = static_cast<MeshNet*>(network);
server->on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("GET /heap");
request->send(200, "text/plain", String(ESP.getFreeHeap()));
});
server->on("/config", HTTP_GET, [](AsyncWebServerRequest *request){
Serial.println("GET /config");
MeshSprocketConfig config;
config.fromFile("/config.json");
config.meshPassword = "";
config.stationPassword = "";
request->send(200, "text/plain", config.toJsonString());
});
// TODO needs testing
server->on("/config", HTTP_POST, [](AsyncWebServerRequest *request){
Serial.println("POST /config");
// read existing config
MeshSprocketConfig config;
config.fromFile("/config.json");
if(request->hasParam("mesh", true)) {
String inStr = request->getParam("mesh", true)->value();
config.fromJsonString(inStr);
Serial.println(config.toJsonString());
// config.saveFile("/config.json");
}
request->send(200, "text/plain", String(ESP.getFreeHeap()));
});
}
};
#endif

13
src/plugins/WebSO.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef __SHARED_PLUGINS__
#define __SHARED_PLUGINS__
#include <ESPAsyncWebServer.h>
extern AsyncWebServer WEBSERVER;
struct WebServerConfig {
const char* contextPath;
const char* docRoot;
const char* defaultFile;
};
#endif

View File

@@ -0,0 +1,33 @@
#ifndef __WEB_SERVER_PLUGIN__
#define __WEB_SERVER_PLUGIN__
#include <FS.h>
#include "TaskSchedulerDeclarations.h"
#include "ArduinoOTA.h"
#include "MeshNet.h"
#include "Plugin.h"
#include <plugins/WebSO.h>
using namespace std;
using namespace std::placeholders;
class WebServerPlugin : public Plugin {
private:
WebServerConfig config;
MeshNet* net;
AsyncWebServer* server;
public:
WebServerPlugin(WebServerConfig cfg, AsyncWebServer* webServer){
config = cfg;
server = webServer;
}
void activate(Scheduler* userScheduler, Network* network){
//connectUpdateNetwork(network);
net = static_cast<MeshNet*>(network);
server->serveStatic(config.contextPath, SPIFFS, config.docRoot).setDefaultFile(config.defaultFile);
server->begin();
Serial.println("WebServer activated");
}
};
#endif