From 35dd5d828fd14881bf20d224545650c2f6927f3b Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Tue, 18 Sep 2018 15:29:48 +0200 Subject: [PATCH] remove dns server and newConn cb --- README.md | 19 +++++++++- lib/NeoPattern/NeoPatternDto.h | 29 ++++++++------- platformio.ini | 1 - src/IlluCat.h | 65 ++++++++++++++-------------------- src/PixelPlugin.h | 6 ++++ 5 files changed, 65 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 4ffd7d9..531b0c7 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file +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 \ No newline at end of file diff --git a/lib/NeoPattern/NeoPatternDto.h b/lib/NeoPattern/NeoPatternDto.h index 15897ef..ca76462 100644 --- a/lib/NeoPattern/NeoPatternDto.h +++ b/lib/NeoPattern/NeoPatternDto.h @@ -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; }; }; diff --git a/platformio.ini b/platformio.ini index 8d5acb5..09cc2af 100644 --- a/platformio.ini +++ b/platformio.ini @@ -38,5 +38,4 @@ lib_deps = ${common.lib_deps} ESP Async WebServer ESPAsyncTCP Adafruit NeoPixel - DNSServer https://gitlab.com/wirelos/sprocket-core.git#develop \ No newline at end of file diff --git a/src/IlluCat.h b/src/IlluCat.h index dd59af8..360b37c 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -4,7 +4,6 @@ #include #include #include -#include #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(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(); } }; diff --git a/src/PixelPlugin.h b/src/PixelPlugin.h index da92f5a..b7fab64 100644 --- a/src/PixelPlugin.h +++ b/src/PixelPlugin.h @@ -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()); }