From 306b0b59d8545cfae90acaf5edc70f8ef491b614 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Tue, 12 Jun 2018 23:23:27 +0200 Subject: [PATCH] add sendTo method, cleanup --- library.json | 2 +- src/MeshNet.cpp | 56 ++++++++++++++++++++++++++++ src/MeshNet.h | 63 ++++++-------------------------- src/examples/mesh/MeshApp.h | 2 +- src/examples/mesh/config.h | 4 +- src/examples/mqttBridge/config.h | 8 ++-- 6 files changed, 76 insertions(+), 59 deletions(-) create mode 100644 src/MeshNet.cpp diff --git a/library.json b/library.json index fb64f8b..6ae6bb2 100644 --- a/library.json +++ b/library.json @@ -1,7 +1,7 @@ { "name": "sprocket-core", "keywords": "esp8266, sprocket, stack", - "description": "Core stack for sprockets", + "description": "Core stack for Sprockets", "authors": { "name": "Patrick Balsiger", diff --git a/src/MeshNet.cpp b/src/MeshNet.cpp new file mode 100644 index 0000000..36abada --- /dev/null +++ b/src/MeshNet.cpp @@ -0,0 +1,56 @@ +#include "MeshNet.h" + +MeshNet::MeshNet(MeshConfig cfg) : Network() { + config = cfg; +} + +Network* MeshNet::init(){ + + Serial.println("init mesh"); + //mesh.setDebugMsgTypes( ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE ); // all types on + 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(config.stationMode){ + Serial.println("connect station"); + mesh.stationManual(config.stationSSID, config.stationPassword); + mesh.setHostname(config.hostname); + } + + return this; +} +Network* MeshNet::connect(){ + return this; +} + +void MeshNet::sendTo(uint32_t target, String msg){ + mesh.sendSingle(target, msg); +} + +void MeshNet::broadcast(String msg){ + mesh.sendBroadcast(msg); +} +void MeshNet::update(){ + // only needed when no scheduler was passed to mesh.init + mesh.update(); +} +void MeshNet::receivedCallback( uint32_t from, String &msg ) { + Serial.printf("--> Received from %u msg=%s\n", from, msg.c_str()); +} + +void MeshNet::newConnectionCallback(uint32_t nodeId) { + Serial.printf("--> New Connection, nodeId = %u\n", nodeId); +} + +void MeshNet::changedConnectionCallback() { + Serial.printf("--> Changed connections %s\n",mesh.subConnectionJson().c_str()); +} + +void MeshNet::nodeTimeAdjustedCallback(int32_t offset) { + Serial.printf("--> Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset); +} \ No newline at end of file diff --git a/src/MeshNet.h b/src/MeshNet.h index 93ada93..04b8f6c 100644 --- a/src/MeshNet.h +++ b/src/MeshNet.h @@ -4,6 +4,7 @@ #include #include #include "Network.h" + using namespace std; using namespace std::placeholders; @@ -22,59 +23,19 @@ struct MeshConfig { 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( 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(config.stationMode){ - Serial.println("connect station"); - mesh.stationManual(config.stationSSID, config.stationPassword); - mesh.setHostname(config.hostname); - } - - return this; - } - Network* connect(){ - return this; - } - void broadcast(String msg){ - mesh.sendBroadcast(msg); - } - void update(){ - // only needed when no scheduler was passed to mesh.init - mesh.update(); - } - void receivedCallback( uint32_t from, String &msg ) { - Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str()); - } - - void newConnectionCallback(uint32_t nodeId) { - id = nodeId; - Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId); - } - - void changedConnectionCallback() { - Serial.printf("Changed connections %s\n",mesh.subConnectionJson().c_str()); - } - - void nodeTimeAdjustedCallback(int32_t offset) { - Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset); - } + MeshNet(MeshConfig cfg); + Network* init(); + Network* connect(); + + void broadcast(String msg); + void sendTo(uint32_t target, String msg); + void update(); // only needed when no scheduler was passed to mesh.init + void receivedCallback( uint32_t from, String &msg ); + void newConnectionCallback(uint32_t nodeId); + void changedConnectionCallback(); + void nodeTimeAdjustedCallback(int32_t offset); }; #endif \ No newline at end of file diff --git a/src/examples/mesh/MeshApp.h b/src/examples/mesh/MeshApp.h index b7930ed..cedab31 100644 --- a/src/examples/mesh/MeshApp.h +++ b/src/examples/mesh/MeshApp.h @@ -24,7 +24,7 @@ class MeshApp : public Sprocket { } using Sprocket::activate; void advertise(MeshNet* network){ - String msg = "Hi, my name is " + String(network->id); + String msg = "{ \"payload \": 1 }"; network->broadcast(msg); } diff --git a/src/examples/mesh/config.h b/src/examples/mesh/config.h index 45de96e..a9cd7eb 100644 --- a/src/examples/mesh/config.h +++ b/src/examples/mesh/config.h @@ -10,14 +10,14 @@ #define STARTUP_DELAY 3000 // Mesh config -#define STATION_MODE 1 +#define STATION_MODE 0 #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 HOSTNAME "mesh-node" #define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION #endif \ No newline at end of file diff --git a/src/examples/mqttBridge/config.h b/src/examples/mqttBridge/config.h index 05ede02..e01f91a 100644 --- a/src/examples/mqttBridge/config.h +++ b/src/examples/mqttBridge/config.h @@ -15,15 +15,15 @@ #define MESH_PORT 5555 #define MESH_PREFIX "whateverYouLike" #define MESH_PASSWORD "somethingSneaky" -#define STATION_SSID "Th1ngs4P" -#define STATION_PASSWORD "th3r31sn0sp00n" +#define STATION_SSID "tErAx1d" +#define STATION_PASSWORD "ramalamadingdong" #define HOSTNAME "mqtt-mesh-bridge" #define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION // Bridge config #define MQTT_CLIENT_NAME HOSTNAME -#define MQTT_BROKER "iot.eclipse.org" +#define MQTT_BROKER "citadel.lan" #define MQTT_PORT 1883 -#define MQTT_TOPIC_ROOT "mesh/" +#define MQTT_TOPIC_ROOT "mesh" #endif \ No newline at end of file