diff --git a/.vscode/settings.json b/.vscode/settings.json index 30a0ad1..acf27a5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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": { diff --git a/platformio.ini b/platformio.ini index 4402823..9b9a1c6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 = +<*> + - - diff --git a/src/App.h b/src/App.h deleted file mode 100644 index 2543143..0000000 --- a/src/App.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef _APP_H_ -#define _APP_H_ - -#include -#include -#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 \ No newline at end of file diff --git a/src/AppStack.cpp b/src/AppStack.cpp deleted file mode 100644 index 3ddc157..0000000 --- a/src/AppStack.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "AppStack.h" - -AppStack::AppStack(){ - -} - -void AppStack::begin(){ - -} - -void AppStack::loop(){ -} \ No newline at end of file diff --git a/src/AppStack.h b/src/AppStack.h deleted file mode 100644 index 9d697c1..0000000 --- a/src/AppStack.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef __APPSTACK_H_INCLUDED__ -#define __APPSTACK_H_INCLUDED__ - -#include -#include -//#include - -class AppStack { - public: - AppStack(); - void begin(); - void loop(); -}; - -#endif \ No newline at end of file diff --git a/src/MeshNet.h b/src/MeshNet.h index 067560c..93ada93 100644 --- a/src/MeshNet.h +++ b/src/MeshNet.h @@ -4,40 +4,47 @@ #include #include #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 ) { diff --git a/src/Network.h b/src/Network.h index 9bfc17c..e6af301 100644 --- a/src/Network.h +++ b/src/Network.h @@ -3,15 +3,14 @@ #include #include -#include 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){ diff --git a/src/Sprocket.cpp b/src/Sprocket.cpp index 46b9f54..23d180a 100644 --- a/src/Sprocket.cpp +++ b/src/Sprocket.cpp @@ -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(); } \ No newline at end of file diff --git a/src/Sprocket.h b/src/Sprocket.h index 7bc17c3..b8f9a76 100644 --- a/src/Sprocket.h +++ b/src/Sprocket.h @@ -1,12 +1,9 @@ #ifndef __SPROCKET_H__ #define __SPROCKET_H__ - #include - #include -#include "AppStack.h" -#include "App.h" +#include #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 \ No newline at end of file diff --git a/src/WebStack.h b/src/WebStack.h deleted file mode 100644 index e02fdc8..0000000 --- a/src/WebStack.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef __WEBSTACK_H_INCLUDED__ -#define __WEBSTACK_H_INCLUDED__ - -#include -#include -#include - -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 \ No newline at end of file diff --git a/src/WiFiNet.h b/src/WiFiNet.h deleted file mode 100644 index 1aaefb4..0000000 --- a/src/WiFiNet.h +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef __WIFI_NET_H__ -#define __WIFI_NET_H__ - -#if defined(ESP8266) -#include //https://github.com/esp8266/Arduino -#include -#else -#include -#endif -#include -#include -#include -#include //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 \ No newline at end of file diff --git a/src/examples/basic/ExampleApp.h b/src/examples/basic/ExampleApp.h index 17bb9b0..a27fdfe 100644 --- a/src/examples/basic/ExampleApp.h +++ b/src/examples/basic/ExampleApp.h @@ -4,25 +4,25 @@ //#include "Sprocket.h" #include #include -#include "App.h" +#include 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)); } diff --git a/src/examples/basic/basic.cpp b/src/examples/basic/basic.cpp index 1d30824..8b5f2b1 100644 --- a/src/examples/basic/basic.cpp +++ b/src/examples/basic/basic.cpp @@ -2,10 +2,6 @@ #define _TASK_STD_FUNCTION #include -#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(); } \ No newline at end of file diff --git a/src/examples/mesh/MeshApp.h b/src/examples/mesh/MeshApp.h index d3e926c..ecdc851 100644 --- a/src/examples/mesh/MeshApp.h +++ b/src/examples/mesh/MeshApp.h @@ -2,24 +2,18 @@ #define __MESH_APP__ #include -#include "App.h" -#include "MeshNet.h" - -#define MESH_PREFIX "whateverYouLike" -#define MESH_PASSWORD "somethingSneaky" -#define MESH_PORT 5555 +#include +#include 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(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 \ No newline at end of file diff --git a/src/examples/mesh/mesh.cpp b/src/examples/mesh/mesh.cpp index f1ab1db..05fa00e 100644 --- a/src/examples/mesh/mesh.cpp +++ b/src/examples/mesh/mesh.cpp @@ -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(); } \ No newline at end of file diff --git a/src/examples/mqttBridge/MqttMeshBridge.h b/src/examples/mqttBridge/MqttMeshBridge.h index a6d3e8b..12733d1 100644 --- a/src/examples/mqttBridge/MqttMeshBridge.h +++ b/src/examples/mqttBridge/MqttMeshBridge.h @@ -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(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"){ diff --git a/src/examples/mqttBridge/bridge.cpp b/src/examples/mqttBridge/bridge.cpp index caca954..8d6ea77 100644 --- a/src/examples/mqttBridge/bridge.cpp +++ b/src/examples/mqttBridge/bridge.cpp @@ -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 #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); }