add sendTo method, cleanup

This commit is contained in:
2018-06-12 23:23:27 +02:00
parent 03bb24d6c7
commit 306b0b59d8
6 changed files with 76 additions and 59 deletions

View File

@@ -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",

56
src/MeshNet.cpp Normal file
View File

@@ -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);
}

View File

@@ -4,6 +4,7 @@
#include <painlessMesh.h>
#include <WiFiClient.h>
#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

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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