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

View File

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

View File

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

View File

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