This commit is contained in:
2018-06-12 19:44:22 +00:00
parent a6d17ba6a3
commit 16b4f254e5
17 changed files with 98 additions and 254 deletions

View File

@@ -1,6 +1,6 @@
{
"terminal.integrated.env.linux": {
"PATH": "/home/master/.platformio/penv/bin:/home/master/.platformio/penv:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl",
"PATH": "/home/master/.platformio/penv/bin:/home/master/.platformio/penv:/home/master/bin:/home/master/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin",
"PLATFORMIO_CALLER": "vscode"
},
"files.associations": {

View File

@@ -18,6 +18,7 @@ board = esp12e
upload_speed = 921600
monitor_baud = 115200
lib_deps =
Hash
ESP Async WebServer
ESPAsyncTCP
TaskScheduler
@@ -45,8 +46,6 @@ upload_speed = ${common.upload_speed}
monitor_baud = ${common.monitor_baud}
framework = ${common.framework}
lib_deps = ${common.lib_deps}
ESPAsyncWifiManager
[env:mesh]
src_filter = +<*> +<examples/mesh/> -<examples/basic/> -<examples/mqttBridge/>

View File

@@ -1,18 +0,0 @@
#ifndef _APP_H_
#define _APP_H_
#include <ESPAsyncWebServer.h>
#include <TaskSchedulerDeclarations.h>
#include "Sprocket.h"
#include "AppStack.h"
#include "Network.h"
class App {
public:
virtual void join(Network&) {};
virtual void server(AsyncWebServer*) {};
virtual void activate(Scheduler*, Network*) {};
virtual void activate(Scheduler*) {};
};
#endif

View File

@@ -1,12 +0,0 @@
#include "AppStack.h"
AppStack::AppStack(){
}
void AppStack::begin(){
}
void AppStack::loop(){
}

View File

@@ -1,15 +0,0 @@
#ifndef __APPSTACK_H_INCLUDED__
#define __APPSTACK_H_INCLUDED__
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
//#include <TaskSchedulerDeclarations.h>
class AppStack {
public:
AppStack();
void begin();
void loop();
};
#endif

View File

@@ -4,40 +4,47 @@
#include <painlessMesh.h>
#include <WiFiClient.h>
#include "Network.h"
using namespace std;
using namespace std::placeholders;
#define STATION_MODE 1
#define WIFI_CHANNEL 11
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
#define STATION_SSID "tErAx1d"
#define STATION_PWD "ramalamadingdong"
#define HOSTNAME "MeshNode"
struct MeshConfig {
int stationMode;
int channel;
int meshPort;
const char* meshSSID;
const char* meshPassword;
const char* stationSSID;
const char* stationPassword;
const char* hostname;
uint16_t debugTypes;
};
class MeshNet : public Network {
public:
painlessMesh mesh;
MeshConfig config;
MeshNet(MeshConfig cfg) : Network() {
config = cfg;
}
Network* init(){
Serial.println("init mesh");
//mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on
mesh.setDebugMsgTypes( ERROR | STARTUP | CONNECTION);
mesh.init( MESH_PREFIX, MESH_PASSWORD, scheduler, MESH_PORT, WIFI_AP_STA, WIFI_CHANNEL );
mesh.setDebugMsgTypes( config.debugTypes );
mesh.init( config.meshSSID, config.meshPassword, scheduler, config.meshPort, WIFI_AP_STA, config.channel );
//mesh.onReceive(bind(&MeshNet::receivedCallback,this, _1, _2));
mesh.onNewConnection(bind(&MeshNet::newConnectionCallback, this, _1));
mesh.onChangedConnections(bind(&MeshNet::changedConnectionCallback, this));
mesh.onNodeTimeAdjusted(bind(&MeshNet::nodeTimeAdjustedCallback, this, _1));
if(STATION_MODE){
if(config.stationMode){
Serial.println("connect station");
mesh.stationManual(STATION_SSID, STATION_PWD);
mesh.setHostname(HOSTNAME);
mesh.stationManual(config.stationSSID, config.stationPassword);
mesh.setHostname(config.hostname);
}
return this;
@@ -49,6 +56,7 @@ class MeshNet : public Network {
mesh.sendBroadcast(msg);
}
void update(){
// only needed when no scheduler was passed to mesh.init
mesh.update();
}
void receivedCallback( uint32_t from, String &msg ) {

View File

@@ -3,15 +3,14 @@
#include <Arduino.h>
#include <TaskSchedulerDeclarations.h>
#include <WiFiClient.h>
class Network {
public:
uint32_t id = 0;
Network(){}
Scheduler* scheduler;
//WiFiClient wifiClient;
virtual Network* init() { return this; };
virtual Network* init(Scheduler* s) { scheduler = s; return init(); };
virtual Network* connect() { return this; };
virtual void update() {};
virtual void broadcast(String msg){

View File

@@ -4,29 +4,28 @@ Sprocket::Sprocket(){
Serial.println("init sprocket");
}
Sprocket::Sprocket(SprocketConfig cfg){
config = cfg;
init(cfg);
}
Sprocket* Sprocket::init(SprocketConfig cfg){
delay(cfg.startupDelay);
Serial.begin(cfg.serialBaudRate);
SPIFFS.begin();
return this;
}
Sprocket* Sprocket::join(Network& net, App& app){
join(net);
app.activate(&scheduler, &net);
Sprocket* Sprocket::activate() {
return activate(&scheduler);
}
Sprocket* Sprocket::join(Network& net){
Serial.println("join network");
net.setScheduler(&scheduler);
net.init();
net.init(&scheduler);
net.connect();
activate(&scheduler, &net);
return this;
}
Sprocket* Sprocket::use(AppStack* stk){
stack = stk;
return this;
}
Sprocket* Sprocket::addTask(Task& tsk){
scheduler.addTask(tsk);
@@ -34,16 +33,6 @@ Sprocket* Sprocket::addTask(Task& tsk){
return this;
}
Sprocket* Sprocket::app(App& app){
app.activate(&scheduler);
//app.join(network);
//app.activate(&scheduler, &network);
//app.activate(&scheduler, network);
return this;
}
void Sprocket::loop(){
//network.update();
scheduler.execute();
//stack->loop();
}

View File

@@ -1,12 +1,9 @@
#ifndef __SPROCKET_H__
#define __SPROCKET_H__
#include <TaskSchedulerDeclarations.h>
#include <Arduino.h>
#include "AppStack.h"
#include "App.h"
#include <FS.h>
#include "Network.h"
struct SprocketConfig {
@@ -17,18 +14,17 @@ struct SprocketConfig {
class Sprocket {
protected:
Scheduler scheduler;
private:
AppStack* stack; // REMOVE
public:
SprocketConfig config;
Sprocket();
Sprocket(SprocketConfig);
Sprocket* init(SprocketConfig);
Sprocket* join(Network&);
Sprocket* join(Network&, App&); // REMOVE
Sprocket* use(AppStack*); // REMOVE
Sprocket* addTask(Task&); // REMOVE ??
Sprocket* app(App&); // REMOVE
virtual void loop();
virtual Sprocket* activate(Scheduler* scheduler, Network* network) {}
virtual Sprocket* activate();
virtual Sprocket* activate(Scheduler*) {}
virtual Sprocket* activate(Scheduler*, Network*) {}
};
#endif

View File

@@ -1,35 +0,0 @@
#ifndef __WEBSTACK_H_INCLUDED__
#define __WEBSTACK_H_INCLUDED__
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
#include <AppStack.h>
class WebStack : public AppStack {
public:
AsyncWebServer* server;
AsyncWebSocket* socket;
WebStack() : AppStack(){
server = new AsyncWebServer(80);
initWebServer();
}
WebStack(AsyncWebServer* webServer) : AppStack(){
server = webServer;
initWebServer();
}
WebStack(AsyncWebServer* webServer, AsyncWebSocket* webSocket) : AppStack(){
server = webServer;
socket = webSocket;
initWebServer();
}
void initWebServer(){
//server->serveStatic(WEBSERVER_ROOT, SPIFFS, WEBSERVER_HTDOCS).setDefaultFile(WEBSERVER_DEFAULT_FILE);
//server->on(WEBCONFIG_GET_HEAP, HTTP_GET, HTTP_GET_HEAP);
}
void begin() {
server->begin();
AppStack::begin();
}
};
#endif

View File

@@ -1,58 +0,0 @@
#ifndef __WIFI_NET_H__
#define __WIFI_NET_H__
#if defined(ESP8266)
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <ESP8266mDNS.h>
#else
#include <WiFi.h>
#endif
#include <WebStack.h>
#include <DNSServer.h>
#include <ESPAsyncWebServer.h>
#include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager
#include "Network.h"
class WiFiNet : public Network {
private:
AsyncWiFiManager* wifiManager;
AsyncWebServer* server;
DNSServer* dns;
const char* hostName = "foo";
public:
WiFiNet() : Network() {
//server = new AsyncWebServer(80);
dns = new DNSServer();
}
WiFiNet* use(AsyncWebServer* srv) {
server = srv;
return this;
}
WiFiNet* use(WebStack* stack) {
server = stack->server;
return this;
}
WiFiNet* init(){}
WiFiNet* connect(){
//server = new AsyncWebServer(80);
dns = new DNSServer();
wifiManager = new AsyncWiFiManager(server, dns);
wifiManager->autoConnect(/* apName, apPassword */);
Serial.println("Hostname: " + String(hostName));
WiFi.hostname(hostName);
startMDNS();
return this;
}
void startMDNS(){
if (!MDNS.begin(hostName)) {
Serial.println("Error setting up MDNS responder!");
} else {
Serial.println("mDNS responder started");
MDNS.addService("http", "tcp", 80);
}
}
};
#endif

View File

@@ -4,25 +4,25 @@
//#include "Sprocket.h"
#include <ESPAsyncWebServer.h>
#include <TaskSchedulerDeclarations.h>
#include "App.h"
#include <Sprocket.h>
using namespace std;
using namespace std::placeholders;
class ExampleApp : public App {
class ExampleApp : public Sprocket {
public:
Task someTask;
ExampleApp() /* App(sprkt) */ {
ExampleApp() {
Serial.println("joo");
}
void activate(Scheduler* scheduler) {
Sprocket* activate(Scheduler* scheduler) {
Serial.println("activate");
someTask.set(TASK_SECOND, TASK_FOREVER, [this](){
Serial.println("do stuff in task");
});
scheduler->addTask(someTask);
someTask.enable();
}
} using Sprocket::activate;
void server(AsyncWebServer* srv) {
srv->on("/ping", HTTP_POST, bind(&ExampleApp::handlePingRequest, this, _1));
}

View File

@@ -2,10 +2,6 @@
#define _TASK_STD_FUNCTION
#include <TaskScheduler.h>
#include "Sprocket.h"
#include "AppStack.h"
#include "WiFiNet.h"
#include "ExampleApp.h"
#define SERIAL_BAUD_RATE 115200
@@ -13,30 +9,17 @@
SprocketConfig config = { STARTUP_DELAY, SERIAL_BAUD_RATE };
WiFiNet net;
Sprocket sprocket;
//AppStack stack;
ExampleApp app;
AsyncWebServer server(80);
ExampleApp sprocket;
void setup() {
delay(STARTUP_DELAY);
net.use(&server);
//sprocket.use(&stack);
sprocket.init(config);
sprocket.join(net);
sprocket.app(app);
//net.connect();
//stack.begin();
sprocket.activate();
}
void loop() {
sprocket.loop();
net.update();
yield();
}

View File

@@ -2,24 +2,18 @@
#define __MESH_APP__
#include <painlessMesh.h>
#include "App.h"
#include "MeshNet.h"
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
#include <Sprocket.h>
#include <MeshNet.h>
using namespace std;
using namespace std::placeholders;
class MeshApp : public App {
class MeshApp : public Sprocket {
public:
Task someTask;
MeshNet* net;
MeshApp() /* : App(sprkt) */ {
}
void activate(Scheduler* scheduler, Network* network) {
Sprocket* activate(Scheduler* scheduler, Network* network) {
net = static_cast<MeshNet*>(network);
net->mesh.onReceive(bind(&MeshApp::receivedCallback,this, _1, _2));
// add a task that sends stuff to the mesh
@@ -27,7 +21,7 @@ class MeshApp : public App {
bind(&MeshApp::advertise, this, net));
scheduler->addTask(someTask);
someTask.enable();
}
} using Sprocket::activate;
void advertise(MeshNet* network){
String msg = "Hi, my name is " + String(network->id);
@@ -40,6 +34,10 @@ class MeshApp : public App {
//String foo = String("cheerz back to ") + String(from);
//net->broadcast(foo);
}
void loop() {
net->update();
scheduler.execute();
}
};
#endif

View File

@@ -4,7 +4,6 @@
#include "Network.h"
#include "MeshNet.h"
#include "Sprocket.h"
#include "AppStack.h"
#include "MeshApp.h"
#define SERIAL_BAUD_RATE 115200
@@ -12,22 +11,19 @@
SprocketConfig config = { STARTUP_DELAY, SERIAL_BAUD_RATE };
Sprocket sprocket;
//AppStack stack;
MeshNet net;
MeshApp app;
MeshApp sprocket;
void setup() {
delay(STARTUP_DELAY);
sprocket.init(config);
sprocket.join(net, app);
sprocket.join(net);
}
void loop() {
net.update();
sprocket.loop();
yield();
}

View File

@@ -7,10 +7,6 @@
#include "Sprocket.h"
#include "MeshNet.h"
#define MQTT_CLIENT_NAME "meshBridge"
#define MQTT_BROKER_HOST "citadel.lan"
#define MQTT_BROKER_PORT 1883
#define MQTT_TOPIC_ROOT "mesh"
#define MQTT_TOPIC_FROM "mesh/from/"
#define MQTT_TOPIC_FROM_GATEWAY "mesh/from/gateway"
#define MQTT_TOPIC_TO_ALL "mesh/to/#"
@@ -22,35 +18,32 @@ struct MqttConfig {
const char* clientName;
const char* brokerHost;
int brokerPort;
const char* topicRoot;
};
WiFiClient wifiClient;
class MqttMeshBridge : public Sprocket {
public:
MeshNet* net;
PubSubClient* client;
WiFiClient wifiClient;
Task connectTask;
Task processTask;
MqttConfig mqttConfig;
MqttMeshBridge(MqttConfig cfg) : Sprocket() {
MqttMeshBridge(SprocketConfig sprktCfg, MqttConfig cfg) : Sprocket(sprktCfg) {
mqttConfig = cfg;
}
Sprocket* activate(Scheduler* scheduler, Network* network) {
Serial.println("activate MQTT bridge");
net = static_cast<MeshNet*>(network);
net->mesh.onReceive(bind(&MqttMeshBridge::receivedCallback,this, _1, _2));
client = new PubSubClient(MQTT_BROKER_HOST, MQTT_BROKER_PORT, bind(&MqttMeshBridge::mqttCallback, this, _1, _2, _3), wifiClient);
client = new PubSubClient(mqttConfig.brokerHost, mqttConfig.brokerPort, bind(&MqttMeshBridge::mqttCallback, this, _1, _2, _3), wifiClient);
enableConnectTask(scheduler);
enableProcessTask(scheduler);
return this;
}
void loop() {
net->update();
scheduler.execute();
}
void enableConnectTask(Scheduler* scheduler) {
connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::connect, this));
scheduler->addTask(connectTask);
@@ -69,7 +62,7 @@ class MqttMeshBridge : public Sprocket {
void connect() {
if (!client->connected()) {
if (client->connect(MQTT_CLIENT_NAME)) {
if (client->connect(mqttConfig.clientName)) {
Serial.println("MQTT connected");
client->publish(MQTT_TOPIC_FROM_GATEWAY,"Ready!");
client->subscribe(MQTT_TOPIC_TO_ALL);
@@ -90,7 +83,7 @@ class MqttMeshBridge : public Sprocket {
String msg = String(cleanPayload);
free(cleanPayload);
int topicRootLength = String(MQTT_TOPIC_ROOT).length();
int topicRootLength = String(mqttConfig.topicRoot).length();
String targetStr = String(topic).substring(topicRootLength + 4);
if(targetStr == "gateway"){

View File

@@ -1,22 +1,43 @@
#define _TASK_SLEEP_ON_IDLE_RUN
#define _TASK_STD_FUNCTION
#include "Network.h"
#include "MeshNet.h"
#include "Sprocket.h"
#include "AppStack.h"
#include <MeshNet.h>
#include "MqttMeshBridge.h"
#define SERIAL_BAUD_RATE 115200
#define STARTUP_DELAY 3000
// Chip config
#define SERIAL_BAUD_RATE 115200
#define STARTUP_DELAY 3000
SprocketConfig config = { STARTUP_DELAY, SERIAL_BAUD_RATE };
// Mesh config
#define STATION_MODE 1
#define WIFI_CHANNEL 11
#define MESH_PORT 5555
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define STATION_SSID "Th1ngs4P"
#define STATION_PASSWORD "th3r31sn0sp00n"
#define HOSTNAME "mqtt-mesh-bridge"
#define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION
MeshNet net;
MqttMeshBridge sprocket({"","",1883});
// Bridge config
#define MQTT_CLIENT_NAME HOSTNAME
#define MQTT_BROKER "iot.eclipse.org"
#define MQTT_PORT 1883
#define MQTT_TOPIC_ROOT "mesh/"
MeshNet net({
STATION_MODE, WIFI_CHANNEL,
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
STATION_SSID, STATION_PASSWORD, HOSTNAME,
MESH_DEBUG_TYPES
});
MqttMeshBridge sprocket(
{ STARTUP_DELAY, SERIAL_BAUD_RATE },
{ MQTT_CLIENT_NAME, MQTT_BROKER, MQTT_PORT, MQTT_TOPIC_ROOT }
);
void setup() {
sprocket.init(config);
sprocket.join(net);
}