diff --git a/platformio.ini b/platformio.ini index b14fc3e..ab16b02 100644 --- a/platformio.ini +++ b/platformio.ini @@ -61,18 +61,6 @@ lib_deps = ${common.lib_deps} ESPAsyncTCP ;upload_port = 192.168.1.247 -[env:meshMqttBridge] -src_filter = +<*> - + -platform = espressif8266 -board = esp12e -upload_speed = ${common.upload_speed} -monitor_baud = ${common.monitor_baud} -framework = ${common.framework} -lib_deps = ${common.lib_deps} - painlessMesh - PubSubClient - - [env:standalone] src_filter = +<*> - + platform = ${common.platform} diff --git a/src/Sprocket.cpp b/src/Sprocket.cpp index db94b88..619781f 100644 --- a/src/Sprocket.cpp +++ b/src/Sprocket.cpp @@ -13,15 +13,14 @@ Sprocket* Sprocket::init(SprocketConfig cfg){ delay(cfg.startupDelay); Serial.begin(cfg.serialBaudRate); SPIFFS.begin(); - scheduler = new Scheduler(); return this; } Sprocket* Sprocket::activate() { + scheduler = new Scheduler(); + activatePlugins(scheduler); return activate(scheduler); } -Sprocket* Sprocket::activate(Scheduler* scheduler) { - // setup plugins - activatePlugins(scheduler); +Sprocket* Sprocket::activate(Scheduler* s) { return this; } diff --git a/src/examples/mesh/MeshApp.h b/src/examples/mesh/MeshApp.h index 575228a..5305b14 100644 --- a/src/examples/mesh/MeshApp.h +++ b/src/examples/mesh/MeshApp.h @@ -14,41 +14,45 @@ using namespace std::placeholders; AsyncWebServer WEBSERVER(80); -class MeshApp : public Sprocket { - public: - Task heartbeatTask; +class MeshApp : public Sprocket +{ + public: + Task heartbeatTask; - MeshApp(SprocketConfig cfg, MeshConfig meshCfg, WebServerConfig webCfg) : Sprocket(cfg) { - addPlugin(new MeshNetworkPlugin(meshCfg)); - addPlugin(new WebServerPlugin(webCfg, &WEBSERVER)); - addPlugin(new WebConfigPlugin(&WEBSERVER)); - subscribe("device/heartbeat", bind(&MeshApp::messageHandler, this, _1)); - } + MeshApp(SprocketConfig cfg, MeshConfig meshCfg, WebServerConfig webCfg) : Sprocket(cfg) + { + addPlugin(new MeshNetworkPlugin(meshCfg)); + addPlugin(new WebServerPlugin(webCfg, &WEBSERVER)); + addPlugin(new WebConfigPlugin(&WEBSERVER)); + subscribe("device/heartbeat", bind(&MeshApp::messageHandler, this, _1)); + } - Sprocket* activate(Scheduler* scheduler) { - Serial.println("activate MeshApp"); - Sprocket::activate(scheduler); - // add a task that sends stuff to the mesh - heartbeatTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this)); - addTask(heartbeatTask); - - return this; - } using Sprocket::activate; + Sprocket *activate(Scheduler *scheduler) + { + // add a task that sends stuff to the mesh + heartbeatTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MeshApp::heartbeat, this)); + addTask(heartbeatTask); + Serial.println("MeshApp activated"); + return this; + } + using Sprocket::activate; - void messageHandler(String msg){ - Serial.println(String("MeshApp: ") + msg); - } + void messageHandler(String msg) + { + Serial.println(String("MeshApp: ") + msg); + } - void heartbeat(){ - SprocketMessage msg; - msg.domain = "wirelos"; - msg.to = "broadcast"; - msg.payload = "alive"; - msg.topic = "device/heartbeat"; - msg.type = SprocketMessage::APP; - String msgStr = msg.toJsonString(); - publish("mesh/broadcast", msgStr); - } + void heartbeat() + { + SprocketMessage msg; + msg.domain = "wirelos"; + msg.to = "broadcast"; + msg.payload = "alive"; + msg.topic = "device/heartbeat"; + msg.type = SprocketMessage::APP; + String msgStr = msg.toJsonString(); + publish("mesh/broadcast", msgStr); + } }; #endif \ No newline at end of file diff --git a/src/examples/meshMqttBridge/MqttMeshBridge.cpp b/src/examples/meshMqttBridge/MqttMeshBridge.cpp deleted file mode 100644 index 9c775e8..0000000 --- a/src/examples/meshMqttBridge/MqttMeshBridge.cpp +++ /dev/null @@ -1,112 +0,0 @@ -#ifndef __MESH_MQTT_BRIDGE_APP__ -#define __MESH_MQTT_BRIDGE_APP__ - -#include -#include -#include -#include "Sprocket.h" -#include "MeshNet.h" - -#define MQTT_TOPIC_FROM "mesh/from/" -#define MQTT_TOPIC_FROM_GATEWAY "mesh/from/gateway" -#define MQTT_TOPIC_TO_ALL "mesh/to/#" - -using namespace std; -using namespace std::placeholders; - -struct MqttConfig { - const char* clientName; - const char* brokerHost; - int brokerPort; - const char* topicRoot; -}; - -class MqttMeshBridge : public Sprocket { - public: - MeshNet* net; - PubSubClient* client; - WiFiClient wifiClient; - Task connectTask; - Task processTask; - MqttConfig mqttConfig; - - MqttMeshBridge(SprocketConfig sprktCfg, MqttConfig cfg) : Sprocket(sprktCfg) { - mqttConfig = cfg; - } - - Sprocket* activate(Scheduler* scheduler){ - enableConnectTask(scheduler); - enableProcessTask(scheduler); - return this; - } - - 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(mqttConfig.brokerHost, mqttConfig.brokerPort, bind(&MqttMeshBridge::mqttCallback, this, _1, _2, _3), wifiClient); - return activate(scheduler); - } - - private: - void enableConnectTask(Scheduler* scheduler) { - connectTask.set(TASK_SECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::connect, this)); - scheduler->addTask(connectTask); - connectTask.enable(); - } - - void enableProcessTask(Scheduler* scheduler) { - processTask.set(TASK_MILLISECOND * 5, TASK_FOREVER, bind(&MqttMeshBridge::process, this)); - scheduler->addTask(processTask); - processTask.enable(); - } - - void process(){ - client->loop(); - } - - void connect() { - if (!client->connected()) { - if (client->connect(mqttConfig.clientName)) { - Serial.println("MQTT connected"); - client->publish(MQTT_TOPIC_FROM_GATEWAY,"Ready!"); - client->subscribe(MQTT_TOPIC_TO_ALL); - } - } - } - - void receivedCallback( uint32_t from, String &msg ) { - Serial.printf("bridge: Received from %u msg=%s\n", from, msg.c_str()); - String topic = MQTT_TOPIC_FROM + String(from); - client->publish(topic.c_str(), msg.c_str()); - } - - void mqttCallback(char* topic, uint8_t* payload, unsigned int length) { - char* cleanPayload = (char*)malloc(length+1); - payload[length] = '\0'; - memcpy(cleanPayload, payload, length+1); - String msg = String(cleanPayload); - free(cleanPayload); - - int topicRootLength = String(mqttConfig.topicRoot).length(); - String targetStr = String(topic).substring(topicRootLength + 4); - - if(targetStr == "gateway"){ - if(msg == "getNodes") { - client->publish(MQTT_TOPIC_FROM_GATEWAY, net->mesh.subConnectionJson().c_str()); - } - } else if(targetStr == "broadcast") { - net->mesh.sendBroadcast(msg); - } else { - uint32_t target = strtoul(targetStr.c_str(), NULL, 10); - if(net->mesh.isConnected(target)){ - net->mesh.sendSingle(target, msg); - } else { - client->publish(MQTT_TOPIC_FROM_GATEWAY, "Client not connected!"); - } - } - } - -}; - -#endif \ No newline at end of file diff --git a/src/examples/meshMqttBridge/config.h b/src/examples/meshMqttBridge/config.h deleted file mode 100644 index 5a10475..0000000 --- a/src/examples/meshMqttBridge/config.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef __BRIDGE_CONFIG__ -#define __BRIDGE_CONFIG__ - -// Scheduler config -#define _TASK_SLEEP_ON_IDLE_RUN -#define _TASK_STD_FUNCTION - -// Chip config -#define SERIAL_BAUD_RATE 115200 -#define STARTUP_DELAY 3000 - -// 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-bridge" -#define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION - -// Bridge config -#define MQTT_CLIENT_NAME HOSTNAME -#define MQTT_BROKER "citadel.lan" -#define MQTT_PORT 1883 -#define MQTT_TOPIC_ROOT "mesh" - -#endif \ No newline at end of file diff --git a/src/examples/meshMqttBridge/main.cpp b/src/examples/meshMqttBridge/main.cpp deleted file mode 100644 index 46f0fce..0000000 --- a/src/examples/meshMqttBridge/main.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "config.h" -#include "MeshNet.h" -#include "MqttMeshBridge.cpp" - -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.join(net); -} - -void loop() { - sprocket.loop(); - yield(); -} \ No newline at end of file