refactorz

This commit is contained in:
2018-06-12 01:25:59 +02:00
parent 0b941fee60
commit 8b04d8642a
6 changed files with 72 additions and 60 deletions

View File

@@ -8,54 +8,65 @@
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 MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define MESH_PORT 5555
#define STATION_SSID "Th1ngs4p"
#define STATION_PWD "th3r31sn0sp00n"
#define HOSTNAME "MeshNode"
class MeshNet : public Network {
public:
painlessMesh mesh;
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, 11 );
//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));
painlessMesh mesh;
return this;
}
Network* connect(){
Serial.println("connect station");
mesh.stationManual("tErAx1d", "ramalamadingdong");
mesh.setHostname("MeshNode");
return this;
}
void broadcast(String msg){
mesh.sendBroadcast(msg);
}
void update(){
mesh.update();
}
void receivedCallback( uint32_t from, String &msg ) {
Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
}
Network* init(){
void newConnectionCallback(uint32_t nodeId) {
id = nodeId;
Serial.printf("--> startHere: New Connection, nodeId = %u\n", nodeId);
}
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.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){
Serial.println("connect station");
mesh.stationManual(STATION_SSID, STATION_PWD);
mesh.setHostname(HOSTNAME);
}
void changedConnectionCallback() {
Serial.printf("Changed connections %s\n",mesh.subConnectionJson().c_str());
}
return this;
}
Network* connect(){
return this;
}
void broadcast(String msg){
mesh.sendBroadcast(msg);
}
void update(){
mesh.update();
}
void receivedCallback( uint32_t from, String &msg ) {
Serial.printf("startHere: Received from %u msg=%s\n", from, msg.c_str());
}
void nodeTimeAdjustedCallback(int32_t offset) {
Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(),offset);
}
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);
}
};
#endif