remove dns server and newConn cb

This commit is contained in:
2018-09-18 15:29:48 +02:00
parent 235fa06d27
commit 35dd5d828f
5 changed files with 65 additions and 55 deletions

View File

@@ -23,4 +23,21 @@ TBD
1. change the NeoPixel settings according to your hardware. The pin needs to be specified as the pin id of your board, e.g. 4 = D2 on a Wemos D1 Mini. 1. change the NeoPixel settings according to your hardware. The pin needs to be specified as the pin id of your board, e.g. 4 = D2 on a Wemos D1 Mini.
1. submit all changes 1. submit all changes
1. hit restart under the System section 1. hit restart under the System section
1. illucat connects to your network and can be reached with http://illucat (or any other configured hostname) 1. illucat connects to your network and can be reached with http://illucat (or any other configured hostname)
## API
### WebSocket
Endpoint: /pixel
#### Topics
pixels/color
pixels/color2
pixels/pattern
pixels/totalSteps
pixels/brightness
### REST
#### Endpoints
POST /pixel/state
POST /config
### Mesh

View File

@@ -32,20 +32,20 @@ struct NeoPixelConfig : public JsonStruct {
} }
}; };
struct NeoPatternMsg : public JsonStruct { struct NeoPatternState : public JsonStruct {
String pattern; uint pattern = 0;
String payload; uint color= 0;
String topic; uint color2= 0;
uint color; uint totalSteps = 255;
uint color2; uint brightness = 100;
uint totalSteps;
void mapJsonObject(JsonObject& root) { void mapJsonObject(JsonObject& root) {
root["topic"] = topic;
root["pattern"] = pattern; root["pattern"] = pattern;
root["payload"] = payload;
root["color"] = color; root["color"] = color;
root["color2"] = color2; root["color2"] = color2;
root["totalSteps"] = totalSteps; root["totalSteps"] = totalSteps;
root["brightness"] = brightness;
} }
// Map a json object to this struct. // Map a json object to this struct.
void fromJsonObject(JsonObject& json){ void fromJsonObject(JsonObject& json){
@@ -54,12 +54,11 @@ struct NeoPatternMsg : public JsonStruct {
valid = 0; valid = 0;
return; return;
} }
topic = getAttrFromJson(json, "topic"); color = getIntAttrFromJson(json, "color", color);
color = getIntAttrFromJson(json, "color"); color2 = getIntAttrFromJson(json, "color2", color2);
color2 = getIntAttrFromJson(json, "color2"); pattern = getIntAttrFromJson(json, "pattern", pattern);
pattern = getAttrFromJson(json, "pattern"); brightness = getIntAttrFromJson(json, "brightness", brightness);
payload = getAttrFromJson(json, "payload"); totalSteps = getIntAttrFromJson(json, "totalSteps", totalSteps);
totalSteps = getIntAttrFromJson(json, "totalSteps", 255);
valid = 1; valid = 1;
}; };
}; };

View File

@@ -38,5 +38,4 @@ lib_deps = ${common.lib_deps}
ESP Async WebServer ESP Async WebServer
ESPAsyncTCP ESPAsyncTCP
Adafruit NeoPixel Adafruit NeoPixel
DNSServer
https://gitlab.com/wirelos/sprocket-core.git#develop https://gitlab.com/wirelos/sprocket-core.git#develop

View File

@@ -4,7 +4,6 @@
#include <painlessMesh.h> #include <painlessMesh.h>
#include <base/MeshSprocket.h> #include <base/MeshSprocket.h>
#include <MeshNet.h> #include <MeshNet.h>
#include <DNSServer.h>
#include "config.h" #include "config.h"
@@ -23,7 +22,6 @@
using namespace std; using namespace std;
using namespace std::placeholders; using namespace std::placeholders;
const byte DNS_PORT = 53;
class IlluCat : public MeshSprocket { class IlluCat : public MeshSprocket {
public: public:
@@ -32,13 +30,14 @@ class IlluCat : public MeshSprocket {
NeoPatternDto state; NeoPatternDto state;
AsyncWebServer* server; AsyncWebServer* server;
AsyncWebSocket* ws; AsyncWebSocket* ws;
DNSServer* dnsServer;
NeoPixelConfig pixelConfig; NeoPixelConfig pixelConfig;
SprocketConfig sprocketConfig; SprocketConfig sprocketConfig;
OtaConfig otaConfig; OtaConfig otaConfig;
WebServerConfig webConfig; WebServerConfig webConfig;
SprocketMessage currentMessage;
IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg/* , NeoPixelConfig pixelCfg */) : MeshSprocket(cfg) { IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg/* , NeoPixelConfig pixelCfg */) : MeshSprocket(cfg) {
//pixelConfig = pixelCfg; //pixelConfig = pixelCfg;
@@ -67,7 +66,7 @@ class IlluCat : public MeshSprocket {
net = static_cast<MeshNet*>(network); net = static_cast<MeshNet*>(network);
net->mesh.onNewConnection(bind(&IlluCat::onNewConnection,this, _1)); net->mesh.onNewConnection(bind(&IlluCat::onNewConnection,this, _1));
net->mesh.onChangedConnections(bind(&IlluCat::onConnectionChanged,this)); //net->mesh.onChangedConnections(bind(&IlluCat::onConnectionChanged,this));
if(SPIFFS.begin()){ if(SPIFFS.begin()){
pixelConfig.fromFile("/pixelConfig.json"); pixelConfig.fromFile("/pixelConfig.json");
@@ -77,18 +76,12 @@ class IlluCat : public MeshSprocket {
pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800); pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800);
server = new AsyncWebServer(80); server = new AsyncWebServer(80);
ws = new AsyncWebSocket("/pixel"); ws = new AsyncWebSocket("/pixel");
dnsServer = new DNSServer();
addPlugin(new OtaTcpPlugin(otaConfig)); addPlugin(new OtaTcpPlugin(otaConfig));
addPlugin(new WebServerPlugin(webConfig, server)); addPlugin(new WebServerPlugin(webConfig, server));
addPlugin(new WebConfigPlugin(server)); addPlugin(new WebConfigPlugin(server));
addPlugin(new PixelPlugin(pixelConfig, pixels)); addPlugin(new PixelPlugin(pixelConfig, pixels));
dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
dnsServer->start(DNS_PORT, "*", WiFi.softAPIP());
//scanningAnimation();
defaultAnimation(); defaultAnimation();
// setup web stuff // setup web stuff
@@ -104,10 +97,10 @@ class IlluCat : public MeshSprocket {
} using MeshSprocket::activate; } using MeshSprocket::activate;
void patternWebRequestHandler(AsyncWebServerRequest *request) { void patternWebRequestHandler(AsyncWebServerRequest *request) {
Serial.println("POST /pixel/pattern"); Serial.println("POST /pixel/state");
if(request->hasParam("state", true)) { if(request->hasParam("msg", true)) {
String inStr = request->getParam("state", true)->value(); String inStr = request->getParam("msg", true)->value();
onMessage(0, inStr); dispatch(0, inStr);
} }
request->send(200, "text/plain", "OK"); request->send(200, "text/plain", "OK");
} }
@@ -115,41 +108,37 @@ class IlluCat : public MeshSprocket {
virtual void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) { virtual void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
if(type == WS_EVT_DATA){ if(type == WS_EVT_DATA){
String frame = WsUtils::parseFrameAsString(type, arg, data, len, 0); String frame = WsUtils::parseFrameAsString(type, arg, data, len, 0);
//onMessage(0, frame); dispatch(0, frame);
NeoPatternMsg msg; // TODO broadcast enable/disable flag
msg.fromJsonString(frame); net->mesh.sendBroadcast(frame);
if(msg.valid){ client->text(String(millis()));
// TODO convert message to send to mesh
//net->mesh.sendBroadcast(frame);
publish(msg.topic, msg.payload);
client->text(String(millis()));
}
} }
} }
virtual void onMessage( uint32_t from, String &msg ) { void dispatch( uint32_t from, String &msg ) {
PRINT_MSG(Serial, SPROCKET_TYPE, "msg from %u = %s\n", from, msg.c_str()); currentMessage.fromJsonString(msg);
state.fromJsonString(msg); if(currentMessage.valid){
PIXEL_FNCS[state.mode](pixels, state.valueStr); currentMessage.from = from;
publish(currentMessage.topic, currentMessage.payload);
}
} }
virtual void onNewConnection(uint32_t nodeId){ virtual void onNewConnection(uint32_t nodeId){
PRINT_MSG(Serial, SPROCKET_TYPE, "connected to %u", nodeId); PRINT_MSG(Serial, SPROCKET_TYPE, "connected to %u", nodeId);
//defaultAnimation(); // publish current state to new node
String stateJson = state.toJsonString(); // FIXME we got a memory leak here
net->mesh.sendSingle(nodeId, stateJson); //String stateJson = currentMessage.toJsonString();
// TODO publish current state to pixel/color on new node //net->mesh.sendSingle(nodeId, stateJson);
}
virtual void onConnectionChanged(){
PRINT_MSG(Serial, SPROCKET_TYPE, "connection changed");
//if(!net->mesh.getNodeList().size()){
// defaultAnimation();
//}
} }
//virtual void onConnectionChanged(){
// PRINT_MSG(Serial, SPROCKET_TYPE, "connection changed");
// //if(!net->mesh.getNodeList().size()){
// // defaultAnimation();
// //}
//}
void loop(){ void loop(){
MeshSprocket::loop(); MeshSprocket::loop();
dnsServer->processNextRequest();
} }
}; };

View File

@@ -17,6 +17,7 @@ class PixelPlugin : public Plugin {
private: private:
NeoPixelConfig pixelConfig; NeoPixelConfig pixelConfig;
NeoPattern* pixels; NeoPattern* pixels;
NeoPatternState state;
public: public:
Task animation; Task animation;
PixelPlugin(NeoPixelConfig cfg, NeoPattern* neoPattern){ PixelPlugin(NeoPixelConfig cfg, NeoPattern* neoPattern){
@@ -31,12 +32,17 @@ class PixelPlugin : public Plugin {
subscribe("pixels/pattern", bind(&PixelPlugin::setPattern, this, _1)); subscribe("pixels/pattern", bind(&PixelPlugin::setPattern, this, _1));
subscribe("pixels/totalSteps", bind(&PixelPlugin::setTotalSteps, this, _1)); subscribe("pixels/totalSteps", bind(&PixelPlugin::setTotalSteps, this, _1));
subscribe("pixels/brightness", bind(&PixelPlugin::setBrightness, this, _1)); subscribe("pixels/brightness", bind(&PixelPlugin::setBrightness, this, _1));
subscribe("pixels/state", bind(&PixelPlugin::setState, this, _1));
animation.set(TASK_MILLISECOND * pixelConfig.updateInterval, TASK_FOREVER, bind(&PixelPlugin::animate, this)); animation.set(TASK_MILLISECOND * pixelConfig.updateInterval, TASK_FOREVER, bind(&PixelPlugin::animate, this));
userScheduler->addTask(animation); userScheduler->addTask(animation);
animation.enable(); animation.enable();
Serial.println("NeoPixels activated"); Serial.println("NeoPixels activated");
} }
// TODO set the whole pixel state
void setState(String msg){
state.fromJsonString(msg);
}
void setTotalSteps(String msg){ void setTotalSteps(String msg){
pixels->TotalSteps = atoi(msg.c_str()); pixels->TotalSteps = atoi(msg.c_str());
} }