diff --git a/platformio.ini b/platformio.ini index fd3924d..7a71f43 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ ; http://docs.platformio.org/page/projectconf.html [platformio] -env_default = build +env_default = build-mesh [common] framework = arduino diff --git a/src/IlluCat.h b/src/IlluCat.h index 5a9dfa0..7e00875 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -42,12 +42,11 @@ class IlluCat : public Sprocket { sprocketConfig = cfg; otaConfig = otaCfg; webConfig = webCfg; - // defaults - //pixelConfig.pin = 4; - //pixelConfig.length = 8; - //pixelConfig.brightness = 32; - //pixelConfig.updateInterval = 100; - //pixelConfig.defaultColor = 100; + pixelConfig.pin = 4; + pixelConfig.length = 8; + pixelConfig.brightness = 32; + pixelConfig.updateInterval = 100; + pixelConfig.defaultColor = 100; } virtual void scanningAnimation() { @@ -61,19 +60,81 @@ class IlluCat : public Sprocket { Sprocket* activate(Scheduler* scheduler, Network* network) { // load config files from SPIFFS - pixelConfig.fromFile("/pixelConfig.json"); - defaultState.fromFile("/pixelState.json"); - state = defaultState; + if(SPIFFS.begin()){ + pixelConfig.fromFile("/pixelConfig.json"); + defaultState.fromFile("/pixelState.json"); + state = defaultState; + } // initialize services pixels = new NeoPattern(pixelConfig.length, pixelConfig.pin, NEO_GRB + NEO_KHZ800); + server = new AsyncWebServer(80); + ws = new AsyncWebSocket("/pixel"); + //wsStream = new AsyncWebSocket("/stream"); + defaultAnimation(); // add plugins + // TODO add HTTP OTA instead of TCP + //addPlugin(new OtaTcpPlugin(otaConfig)); + addPlugin(new WebServerPlugin(webConfig, server)); + addPlugin(new WebConfigPlugin(server)); addPlugin(new PixelPlugin(pixelConfig, pixels)); - defaultAnimation(); + + + // FIXME move to networking + //String softApPrt = "SoftAP IP: " + WiFi.softAPIP().toString(); + //PRINT_MSG(Serial, SPROCKET_TYPE, softApPrt.c_str()); + + // TODO move to plugin + // setup web stuff + server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json"); + server->on("/pixel/api", HTTP_POST, bind(&IlluCat::patternWebRequestHandler, this, _1)); + ws->onEvent(bind(&IlluCat::onWsEvent, this, _1, _2, _3, _4, _5, _6)); + server->addHandler(ws); + //wsStream->onEvent(bind(&IlluCat::onStream, this, _1, _2, _3, _4, _5, _6)); + //server->addHandler(wsStream); + return Sprocket::activate(scheduler, network); } using Sprocket::activate; + // TODO move to utils + String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true){ + if(request->hasParam(param, isPost)) { + return request->getParam(param, isPost)->value(); + } + return defaultValue; + } + + + void onStream(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) { + if(type == WS_EVT_DATA){ + PRINT_MSG(Serial, SPROCKET_TYPE, WsUtils::parseFrameAsString(type, arg, data, len, 0).c_str()); + pixels->ActivePattern = NONE; + pixels->handleStream(data, len); + } + } + + void patternWebRequestHandler(AsyncWebServerRequest *request) { + PRINT_MSG(Serial, SPROCKET_TYPE, "POST /pixel/api"); + currentMessage.topic = getRequestParameterOrDefault(request, "topic", ""); + currentMessage.payload = getRequestParameterOrDefault(request, "payload", ""); + currentMessage.broadcast = atoi(getRequestParameterOrDefault(request, "broadcast", "0").c_str()); + String msg = currentMessage.toJsonString(); + publish(currentMessage.topic, currentMessage.payload); + if(currentMessage.broadcast){ + network.broadcast(msg); + } + request->send(200, "text/plain", msg); + } + + 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); + dispatch(0, frame); + network.broadcast(frame); + } + } + virtual void dispatch( uint32_t from, String &msg ) { currentMessage.fromJsonString(msg); if(currentMessage.valid){