cleanup build, examples and library

This commit is contained in:
2018-06-19 19:09:27 +02:00
parent 514f0fa8b8
commit 8d2342b60b
13 changed files with 124 additions and 23 deletions

View File

@@ -24,9 +24,6 @@ Network* MeshNet::init(){
return this;
}
Network* MeshNet::connect(){
return this;
}
void MeshNet::sendTo(uint32_t target, String msg){
mesh.sendSingle(target, msg);

View File

@@ -27,7 +27,6 @@ class MeshNet : public Network {
MeshNet(MeshConfig cfg);
Network* init();
Network* connect();
void broadcast(String msg);
void sendTo(uint32_t target, String msg);

View File

@@ -20,7 +20,7 @@ class Sprocket {
Sprocket(SprocketConfig);
Sprocket* init(SprocketConfig);
Sprocket* join(Network&);
Sprocket* addTask(Task&); // REMOVE ??
Sprocket* addTask(Task&);
virtual void loop();
virtual Sprocket* activate();
virtual Sprocket* activate(Scheduler*) { return this; }

View File

@@ -18,12 +18,12 @@ class MeshApp : public Sprocket {
net->mesh.onReceive(bind(&MeshApp::receivedCallback,this, _1, _2));
// add a task that sends stuff to the mesh
someTask.set(TASK_SECOND * 5, TASK_FOREVER,
bind(&MeshApp::advertise, this, net));
bind(&MeshApp::heartbeat, this, net));
scheduler->addTask(someTask);
someTask.enable();
} using Sprocket::activate;
void advertise(MeshNet* network){
void heartbeat(MeshNet* network){
String msg = "{ \"payload \": 1 }";
network->broadcast(msg);
}

View File

@@ -34,16 +34,21 @@ class MqttMeshBridge : public Sprocket {
mqttConfig = cfg;
}
Sprocket* activate(Scheduler* scheduler, Network* network) {
Serial.println("activate MQTT bridge");
net = static_cast<MeshNet*>(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);
Sprocket* activate(Scheduler* scheduler){
enableConnectTask(scheduler);
enableProcessTask(scheduler);
return this;
}
Sprocket* activate(Scheduler* scheduler, Network* network) {
Serial.println("activate MQTT bridge");
net = static_cast<MeshNet*>(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);

View File

@@ -13,11 +13,11 @@
#define STATION_MODE 1
#define WIFI_CHANNEL 11
#define MESH_PORT 5555
#define MESH_PREFIX "whateverYouLike"
#define MESH_PASSWORD "somethingSneaky"
#define STATION_SSID "tErAx1d"
#define STATION_PASSWORD "ramalamadingdong"
#define HOSTNAME "mqtt-mesh-bridge"
#define MESH_PREFIX "wirelos_contraption"
#define MESH_PASSWORD "th3r31sn0sp00n"
#define STATION_SSID "Th1ngs4P"
#define STATION_PASSWORD "th3r31sn0sp00n"
#define HOSTNAME "sprocket"
#define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION
// Bridge config

View File

@@ -0,0 +1,38 @@
#ifndef __BASE_SPROCKET__
#define __BASE_SPROCKET__
#include <ESPAsyncWebServer.h>
#include <Sprocket.h>
using namespace std;
using namespace std::placeholders;
// TODO remove someTask and replace with OTA stuff
class BaseSprocket : public Sprocket {
public:
Task someTask;
MeshNet* net;
BaseSprocket(SprocketConfig cfg) : Sprocket(cfg) {
}
Sprocket* activate(Scheduler* scheduler, Network* network) {
net = static_cast<MeshNet*>(network);
net->mesh.onReceive(bind(&BaseSprocket::receivedCallback,this, _1, _2));
// add a task that sends stuff to the mesh
someTask.set(TASK_SECOND * 5, TASK_FOREVER,
bind(&BaseSprocket::heartbeat, this, net));
scheduler->addTask(someTask);
someTask.enable();
} using Sprocket::activate;
void heartbeat(MeshNet* network){
String msg = "{ \"alive \": 1 }";
network->broadcast(msg);
}
virtual void receivedCallback( uint32_t from, String &msg ) {
Serial.printf("RECV %u = %s\n", from, msg.c_str());
}
};
#endif

28
src/firmware/config.h Normal file
View File

@@ -0,0 +1,28 @@
#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 0 // 1 = connect to AP using STATION params
#define WIFI_CHANNEL 11
#define MESH_PORT 5555
#define MESH_PREFIX "wirelos_contraption"
#define MESH_PASSWORD "th3r31sn0sp00n"
#define STATION_SSID "Th1ngs4P"
#define STATION_PASSWORD "th3r31sn0sp00n"
#define HOSTNAME "sprocket"
#define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION
#define MQTT_CLIENT_NAME HOSTNAME
#define MQTT_BROKER "citadel.lan"
#define MQTT_PORT 1883
#define MQTT_TOPIC_ROOT "mesh"
#endif

23
src/firmware/main.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "config.h"
#include "MeshNet.h"
#include "BaseSprocket.h"
MeshNet net({
STATION_MODE, WIFI_CHANNEL,
MESH_PORT, MESH_PREFIX, MESH_PASSWORD,
STATION_SSID, STATION_PASSWORD, HOSTNAME,
MESH_DEBUG_TYPES
});
BaseSprocket sprocket(
{ STARTUP_DELAY, SERIAL_BAUD_RATE }
);
void setup() {
sprocket.join(net);
}
void loop() {
sprocket.loop();
yield();
}