From 2ba0684982a279e50257c23c7bceafe440015f5d Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Fri, 28 Sep 2018 11:09:39 +0200 Subject: [PATCH 1/8] comment out all mesh stuff --- src/IlluCat.h | 46 +++++++++++++++++++++++++++------------------- src/config.h | 3 +++ src/main.cpp | 8 +++++--- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/IlluCat.h b/src/IlluCat.h index b1b010a..9e68357 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -1,10 +1,11 @@ #ifndef __MESH_APP__ #define __MESH_APP__ -#include -#include -#include -#include +#include +//#include +//#include +#include +//#include #include "config.h" @@ -23,16 +24,17 @@ using namespace std; using namespace std::placeholders; -const byte DNS_PORT = 53; +//const byte DNS_PORT = 53; -class IlluCat : public MeshSprocket { +class IlluCat : public Sprocket { public: + Scheduler* ts; NeoPattern* pixels; NeoPatternDto defaultState; NeoPatternDto state; AsyncWebServer* server; AsyncWebSocket* ws; - DNSServer* dnsServer; + //DNSServer* dnsServer; NeoPixelConfig pixelConfig; SprocketConfig sprocketConfig; @@ -41,7 +43,7 @@ class IlluCat : public MeshSprocket { SprocketMessage currentMessage; - IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg/* , NeoPixelConfig pixelCfg */) : MeshSprocket(cfg) { + IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg/* , NeoPixelConfig pixelCfg */) : Sprocket(cfg) { //pixelConfig = pixelCfg; sprocketConfig = cfg; @@ -53,6 +55,8 @@ class IlluCat : public MeshSprocket { pixelConfig.brightness = 32; pixelConfig.updateInterval = 100; pixelConfig.defaultColor = 100; + + ts = new Scheduler(); } virtual void scanningAnimation() { @@ -64,7 +68,7 @@ class IlluCat : public MeshSprocket { } Sprocket* activate(Scheduler* scheduler, Network* network) { - net = static_cast(network); + //net = static_cast(network); // load config files from SPIFFS if(SPIFFS.begin()){ @@ -77,10 +81,10 @@ 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(); + //dnsServer = new DNSServer(); // add plugins - addPlugin(new OtaTcpPlugin(otaConfig)); + //addPlugin(new OtaTcpPlugin(otaConfig)); addPlugin(new WebServerPlugin(webConfig, server)); addPlugin(new WebConfigPlugin(server)); addPlugin(new PixelPlugin(pixelConfig, pixels)); @@ -88,8 +92,8 @@ class IlluCat : public MeshSprocket { // configure DNS // plugin? - dnsServer->setErrorReplyCode(DNSReplyCode::NoError); - dnsServer->start(DNS_PORT, "*", WiFi.softAPIP()); + //dnsServer->setErrorReplyCode(DNSReplyCode::NoError); + //dnsServer->start(DNS_PORT, "*", WiFi.softAPIP()); String softApPrt = "SoftAP IP: " + WiFi.softAPIP().toString(); PRINT_MSG(Serial, SPROCKET_TYPE, softApPrt.c_str()); @@ -100,8 +104,8 @@ class IlluCat : public MeshSprocket { ws->onEvent(bind(&IlluCat::onWsEvent, this, _1, _2, _3, _4, _5, _6)); server->addHandler(ws); - return MeshSprocket::activate(scheduler, network); - } using MeshSprocket::activate; + return Sprocket::activate(scheduler, network); + } using Sprocket::activate; // TODO move to utils String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true){ @@ -111,6 +115,8 @@ class IlluCat : public MeshSprocket { return defaultValue; } + + void patternWebRequestHandler(AsyncWebServerRequest *request) { PRINT_MSG(Serial, SPROCKET_TYPE, "POST /pixel/api"); currentMessage.topic = getRequestParameterOrDefault(request, "topic", ""); @@ -119,7 +125,7 @@ class IlluCat : public MeshSprocket { String msg = currentMessage.toJsonString(); publish(currentMessage.topic, currentMessage.payload); if(currentMessage.broadcast){ - net->mesh.sendBroadcast(msg); + network.broadcast(msg); } request->send(200, "text/plain", msg); } @@ -137,14 +143,16 @@ class IlluCat : public MeshSprocket { currentMessage.from = from; publish(currentMessage.topic, currentMessage.payload); if(currentMessage.broadcast){ - net->mesh.sendBroadcast(msg); + network.broadcast(msg); } } } void loop(){ - MeshSprocket::loop(); - dnsServer->processNextRequest(); + Sprocket::loop(); + yield(); + //ts->execute(); + //dnsServer->processNextRequest(); } }; diff --git a/src/config.h b/src/config.h index ec43677..7108353 100644 --- a/src/config.h +++ b/src/config.h @@ -14,11 +14,14 @@ #define SPROCKET_MODE 0 #define WIFI_CHANNEL 11 #define MESH_PORT 5555 +#define AP_SSID "illucat" +#define AP_PASSWORD "illumination" #define MESH_PREFIX "illucat-mesh" #define MESH_PASSWORD "th3r31sn0sp00n" #define STATION_SSID "MyAP" #define STATION_PASSWORD "th3r31sn0sp00n" #define HOSTNAME "illucat" +#define CONNECT_TIMEOUT 10000 #define MESH_DEBUG_TYPES ERROR | STARTUP | CONNECTION //ERROR | MESH_STATUS | CONNECTION | SYNC | COMMUNICATION | GENERAL | MSG_TYPES | REMOTE diff --git a/src/main.cpp b/src/main.cpp index 440847f..72f3185 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,15 @@ #include "config.h" -#include "MeshNet.h" +//#include "MeshNet.h" +#include "WiFiNet.h" #include "IlluCat.h" -MeshNet net({ +/* MeshNet net({ SPROCKET_MODE, WIFI_CHANNEL, MESH_PORT, MESH_PREFIX, MESH_PASSWORD, STATION_SSID, STATION_PASSWORD, HOSTNAME, MESH_DEBUG_TYPES -}); +}); */ +WiFiNet net(SPROCKET_MODE,STATION_SSID, STATION_PASSWORD,AP_SSID, AP_PASSWORD,HOSTNAME,CONNECT_TIMEOUT); IlluCat sprocket( { STARTUP_DELAY, SERIAL_BAUD_RATE }, { OTA_PORT, OTA_PASSWORD }, From 77d92155df31d706a54077c67cee50aa3ff314ca Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Wed, 3 Oct 2018 21:24:47 +0200 Subject: [PATCH 2/8] add ap infos to example conf --- data/example.config.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data/example.config.json b/data/example.config.json index 7777ec7..f9326fd 100644 --- a/data/example.config.json +++ b/data/example.config.json @@ -1,6 +1,8 @@ { "stationMode": 0, "hostname": "illucat", + "apSSID": "illucat", + "apPassword": "illucat", "stationSSID": "MyWifi", "stationPassword": "myWifiPassword", "meshSSID": "illucat", From 0686895f333ebc66fee45c91a251c6f71efa7f65 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Wed, 3 Oct 2018 22:07:20 +0200 Subject: [PATCH 3/8] add conn timeout to example conf --- data/example.config.json | 3 ++- src/IlluCat.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/data/example.config.json b/data/example.config.json index f9326fd..b441a17 100644 --- a/data/example.config.json +++ b/data/example.config.json @@ -2,7 +2,8 @@ "stationMode": 0, "hostname": "illucat", "apSSID": "illucat", - "apPassword": "illucat", + "apPassword": "illumination", + "connectTimeout": 20000, "stationSSID": "MyWifi", "stationPassword": "myWifiPassword", "meshSSID": "illucat", diff --git a/src/IlluCat.h b/src/IlluCat.h index c106d65..985c41a 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -143,7 +143,7 @@ class IlluCat : public Sprocket { if(type == WS_EVT_DATA){ String frame = WsUtils::parseFrameAsString(type, arg, data, len, 0); dispatch(0, frame); - net->mesh.sendBroadcast(frame); + network.broadcast(frame); } } From 207f90d60afbb1875af9d1f9d40d84d5361e2cc7 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Thu, 4 Oct 2018 02:34:37 +0200 Subject: [PATCH 4/8] separate concerns --- .vscode/settings.json | 23 +++++++- data/pixelConfig.json | 4 +- platformio.ini | 14 +++++ src/IlluCat.h | 101 +++++------------------------------ src/main.cpp | 27 ---------- src/wifi/WebCat.h | 118 +++++++++++++++++++++++++++++++++++++++++ src/wifi/main.cpp | 28 ++++++++++ src/wifiMesh/MeshCat.h | 39 ++++++++++++++ src/wifiMesh/main.cpp | 25 +++++++++ 9 files changed, 262 insertions(+), 117 deletions(-) delete mode 100644 src/main.cpp create mode 100644 src/wifi/WebCat.h create mode 100644 src/wifi/main.cpp create mode 100644 src/wifiMesh/MeshCat.h create mode 100644 src/wifiMesh/main.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 8d30d4d..5b1aa28 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,6 +15,27 @@ "unordered_map": "cpp", "vector": "cpp", "tuple": "cpp", - "utility": "cpp" + "utility": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "exception": "cpp", + "fstream": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "numeric": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "typeinfo": "cpp" } } \ No newline at end of file diff --git a/data/pixelConfig.json b/data/pixelConfig.json index 4a0d066..0bbd7c5 100644 --- a/data/pixelConfig.json +++ b/data/pixelConfig.json @@ -1,7 +1,7 @@ { "pin": 4, - "length": 300, + "length": 32, "brightness": 64, - "updateInterval": 50, + "updateInterval": 100, "defaultColor": 100 } \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index d08fb07..fd3924d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,6 +31,20 @@ lib_deps = Adafruit NeoPixel [env:build] +src_filter = +<*> - + +platform = ${common.platform} +board = ${common.board} +upload_speed = ${common.upload_speed} +monitor_baud = ${common.monitor_baud} +framework = ${common.framework} +build_flags = -Wl,-Teagle.flash.4m1m.ld + -DSPROCKET_PRINT=1 +lib_deps = ${common.lib_deps} + https://gitlab.com/wirelos/sprocket-core.git#develop + + +[env:build-mesh] +src_filter = +<*> + - platform = ${common.platform} board = ${common.board} upload_speed = ${common.upload_speed} diff --git a/src/IlluCat.h b/src/IlluCat.h index 985c41a..5a9dfa0 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -2,11 +2,8 @@ #define __MESH_APP__ #include -//#include -//#include +#include #include -//#include - #include "config.h" #include "NeoPattern.cpp" @@ -24,17 +21,15 @@ using namespace std; using namespace std::placeholders; -//const byte DNS_PORT = 53; class IlluCat : public Sprocket { public: - Scheduler* ts; NeoPattern* pixels; NeoPatternDto defaultState; NeoPatternDto state; AsyncWebServer* server; AsyncWebSocket* ws; - AsyncWebSocket* wsStream; + //AsyncWebSocket* wsStream; NeoPixelConfig pixelConfig; SprocketConfig sprocketConfig; @@ -43,110 +38,42 @@ class IlluCat : public Sprocket { SprocketMessage currentMessage; - IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg/* , NeoPixelConfig pixelCfg */) : Sprocket(cfg) { - //pixelConfig = pixelCfg; - + IlluCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : Sprocket(cfg) { sprocketConfig = cfg; otaConfig = otaCfg; webConfig = webCfg; - - pixelConfig.pin = 4; - pixelConfig.length = 8; - pixelConfig.brightness = 32; - pixelConfig.updateInterval = 100; - pixelConfig.defaultColor = 100; - - ts = new Scheduler(); - + // defaults + //pixelConfig.pin = 4; + //pixelConfig.length = 8; + //pixelConfig.brightness = 32; + //pixelConfig.updateInterval = 100; + //pixelConfig.defaultColor = 100; } + virtual void scanningAnimation() { pixels->Scanner(pixels->Wheel(COLOR_NOT_CONNECTED), pixelConfig.updateInterval); } virtual void defaultAnimation() { + // remove DEPRECATED USAGE String defaultStr = String(defaultState.value); PIXEL_FNCS[defaultState.mode](pixels, defaultStr.c_str()); } Sprocket* activate(Scheduler* scheduler, Network* network) { - //net = static_cast(network); - // load config files from SPIFFS - if(SPIFFS.begin()){ - pixelConfig.fromFile("/pixelConfig.json"); - defaultState.fromFile("/pixelState.json"); - state = defaultState; - } + 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"); // add plugins - //addPlugin(new OtaTcpPlugin(otaConfig)); - addPlugin(new WebServerPlugin(webConfig, server)); - addPlugin(new WebConfigPlugin(server)); addPlugin(new PixelPlugin(pixelConfig, pixels)); defaultAnimation(); - - // configure DNS - // plugin? - //dnsServer->setErrorReplyCode(DNSReplyCode::NoError); - //dnsServer->start(DNS_PORT, "*", WiFi.softAPIP()); - 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){ diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index 72f3185..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "config.h" -//#include "MeshNet.h" -#include "WiFiNet.h" -#include "IlluCat.h" - -/* MeshNet net({ - SPROCKET_MODE, WIFI_CHANNEL, - MESH_PORT, MESH_PREFIX, MESH_PASSWORD, - STATION_SSID, STATION_PASSWORD, HOSTNAME, - MESH_DEBUG_TYPES -}); */ -WiFiNet net(SPROCKET_MODE,STATION_SSID, STATION_PASSWORD,AP_SSID, AP_PASSWORD,HOSTNAME,CONNECT_TIMEOUT); -IlluCat sprocket( - { STARTUP_DELAY, SERIAL_BAUD_RATE }, - { OTA_PORT, OTA_PASSWORD }, - { WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE }/* , - { LED_STRIP_PIN, LED_STRIP_LENGTH, LED_STRIP_BRIGHTNESS, LED_STRIP_UPDATE_INTERVAL, LED_STRIP_DEFAULT_COLOR } */ -); - -void setup() { - sprocket.join(net); -} - -void loop() { - sprocket.loop(); - yield(); -} \ No newline at end of file diff --git a/src/wifi/WebCat.h b/src/wifi/WebCat.h new file mode 100644 index 0000000..54fb09d --- /dev/null +++ b/src/wifi/WebCat.h @@ -0,0 +1,118 @@ +#ifndef __WEB_CAT__ +#define __WEB_CAT__ + +#include +#include +#include + +#include "config.h" +#include "NeoPattern.cpp" +#include "NeoPatternDto.h" +#include "NeoPattern_api_json.h" +#include "NeoPattern_api_modes.cpp" +#include "utils_print.h" +#include "utils_ws.h" +#include +#include +#include +#include +#include "PixelPlugin.h" +#include "IlluCat.h" + +using namespace std; +using namespace std::placeholders; + + +class WebCat : public IlluCat { + public: + AsyncWebServer* server; + AsyncWebSocket* ws; + //AsyncWebSocket* wsStream; + WebServerConfig webConfig; + + SprocketMessage currentMessage; + + WebCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : IlluCat(cfg, otaCfg, webCfg) { + webConfig = webCfg; + } + + Sprocket* activate(Scheduler* scheduler, Network* network) { + + Serial.println("SETUP SERVER"); + // initialize services + server = new AsyncWebServer(80); + ws = new AsyncWebSocket("/pixel"); + //wsStream = new AsyncWebSocket("/stream"); + Serial.println("init handlers"); + // TODO move to plugin + // setup web stuff + server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json"); + server->on("/pixel/api", HTTP_POST, bind(&WebCat::patternWebRequestHandler, this, _1)); + ws->onEvent(bind(&WebCat::onWsEvent, this, _1, _2, _3, _4, _5, _6)); + server->addHandler(ws); + //wsStream->onEvent(bind(&WebCat::onStream, this, _1, _2, _3, _4, _5, _6)); + //server->addHandler(wsStream); + Serial.println("add plugins"); + // add plugins + // TODO add HTTP OTA instead of TCP + //addPlugin(new OtaTcpPlugin(otaConfig)); + addPlugin(new WebServerPlugin(webConfig, server)); + addPlugin(new WebConfigPlugin(server)); + + return IlluCat::activate(scheduler, network);; + }; + + + // 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){ + currentMessage.from = from; + publish(currentMessage.topic, currentMessage.payload); + } + } + + void loop(){ + Sprocket::loop(); + yield(); + } +}; + +#endif \ No newline at end of file diff --git a/src/wifi/main.cpp b/src/wifi/main.cpp new file mode 100644 index 0000000..57f92bd --- /dev/null +++ b/src/wifi/main.cpp @@ -0,0 +1,28 @@ +#include "config.h" +#include "WiFiNet.h" +#include "WebCat.h" + +WiFiNet net( + SPROCKET_MODE, + STATION_SSID, + STATION_PASSWORD, + AP_SSID, + AP_PASSWORD, + HOSTNAME, + CONNECT_TIMEOUT +); +WebCat sprocket( + { STARTUP_DELAY, SERIAL_BAUD_RATE }, + { OTA_PORT, OTA_PASSWORD }, + { WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE } +); + +void setup() { + delay(STARTUP_DELAY); + sprocket.join(net); +} + +void loop() { + sprocket.loop(); + yield(); +} \ No newline at end of file diff --git a/src/wifiMesh/MeshCat.h b/src/wifiMesh/MeshCat.h new file mode 100644 index 0000000..083acf4 --- /dev/null +++ b/src/wifiMesh/MeshCat.h @@ -0,0 +1,39 @@ +#ifndef __MESHCAT__ +#define __MESHCAT__ + +#include +#include +#include +#include "utils_print.h" +#include "IlluCat.h" +#include "config.h" + +using namespace std; +using namespace std::placeholders; + +class MeshCat : public IlluCat { + public: + Scheduler* meshScheduler; + + MeshCat(SprocketConfig cfg, OtaConfig otaCfg, WebServerConfig webCfg) : IlluCat(cfg, otaCfg, webCfg) { + meshScheduler = new Scheduler(); + } + + Sprocket* join(Network& net){ + PRINT_MSG(Serial, SPROCKET_TYPE, "join mesh network"); + net.init(meshScheduler); + net.onReceive(bind(&IlluCat::dispatch,this, _1, _2)); + net.connect(); + network = net; + return activate(&scheduler, &net); + } + + void loop() { + meshScheduler->execute(); + yield(); + Sprocket::loop(); + yield(); + } +}; + +#endif \ No newline at end of file diff --git a/src/wifiMesh/main.cpp b/src/wifiMesh/main.cpp new file mode 100644 index 0000000..7656152 --- /dev/null +++ b/src/wifiMesh/main.cpp @@ -0,0 +1,25 @@ +#include "config.h" +#include "MeshNet.h" +#include "MeshCat.h" + +MeshNet net({ + SPROCKET_MODE, WIFI_CHANNEL, + MESH_PORT, MESH_PREFIX, MESH_PASSWORD, + STATION_SSID, STATION_PASSWORD, HOSTNAME, + MESH_DEBUG_TYPES +}); +MeshCat sprocket( + { STARTUP_DELAY, SERIAL_BAUD_RATE }, + { OTA_PORT, OTA_PASSWORD }, + { WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE } +); + +void setup() { + delay(STARTUP_DELAY); + sprocket.join(net); +} + +void loop() { + sprocket.loop(); + yield(); +} \ No newline at end of file From b91255fe85ab6d8877715d087f0bfee13de8b6aa Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Sat, 6 Oct 2018 15:48:48 +0200 Subject: [PATCH 5/8] run default animation earlier --- platformio.ini | 2 +- src/IlluCat.h | 81 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 72 insertions(+), 11 deletions(-) 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){ From 75a4e3e94c9a9a8b4d5c76cd3f03d421cb879599 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Sun, 7 Oct 2018 11:47:57 +0200 Subject: [PATCH 6/8] minimize startup time --- src/config.h | 2 +- src/wifiMesh/main.cpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/config.h b/src/config.h index 7108353..b8025ea 100644 --- a/src/config.h +++ b/src/config.h @@ -8,7 +8,7 @@ // Chip config #define SPROCKET_TYPE "ILLUCAT" #define SERIAL_BAUD_RATE 115200 -#define STARTUP_DELAY 3000 +#define STARTUP_DELAY 1000 // Mesh config #define SPROCKET_MODE 0 diff --git a/src/wifiMesh/main.cpp b/src/wifiMesh/main.cpp index 7656152..a9b7f0a 100644 --- a/src/wifiMesh/main.cpp +++ b/src/wifiMesh/main.cpp @@ -15,7 +15,6 @@ MeshCat sprocket( ); void setup() { - delay(STARTUP_DELAY); sprocket.join(net); } From 0e13508158b7c038f373e7f18dd6c55114de2f73 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Sun, 7 Oct 2018 12:44:12 +0200 Subject: [PATCH 7/8] set build to default env --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 7a71f43..fd3924d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -9,7 +9,7 @@ ; http://docs.platformio.org/page/projectconf.html [platformio] -env_default = build-mesh +env_default = build [common] framework = arduino From 2a217755243885a0ae16518b76a93bb5cc8f09e2 Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Sun, 7 Oct 2018 12:58:08 +0200 Subject: [PATCH 8/8] use illu base, reduce pixel num --- data/pixelConfig.json | 2 +- platformio.ini | 2 ++ src/wifi/main.cpp | 5 ++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/data/pixelConfig.json b/data/pixelConfig.json index 0bbd7c5..7e7f125 100644 --- a/data/pixelConfig.json +++ b/data/pixelConfig.json @@ -1,6 +1,6 @@ { "pin": 4, - "length": 32, + "length": 8, "brightness": 64, "updateInterval": 100, "defaultColor": 100 diff --git a/platformio.ini b/platformio.ini index fd3924d..de10233 100644 --- a/platformio.ini +++ b/platformio.ini @@ -56,6 +56,7 @@ lib_deps = ${common.lib_deps} https://gitlab.com/wirelos/sprocket-core.git#develop [env:release] +src_filter = +<*> - + platform = ${common.platform} board = ${common.board} upload_speed = ${common.upload_speed} @@ -68,6 +69,7 @@ lib_deps = ${common.lib_deps} [env:nodemcu] +src_filter = +<*> - + platform = ${common.platform} board = nodemcu upload_speed = ${common.upload_speed} diff --git a/src/wifi/main.cpp b/src/wifi/main.cpp index 57f92bd..2b751c7 100644 --- a/src/wifi/main.cpp +++ b/src/wifi/main.cpp @@ -1,6 +1,6 @@ #include "config.h" #include "WiFiNet.h" -#include "WebCat.h" +#include "IlluCat.h" WiFiNet net( SPROCKET_MODE, @@ -11,14 +11,13 @@ WiFiNet net( HOSTNAME, CONNECT_TIMEOUT ); -WebCat sprocket( +IlluCat sprocket( { STARTUP_DELAY, SERIAL_BAUD_RATE }, { OTA_PORT, OTA_PASSWORD }, { WEB_CONTEXT_PATH, WEB_DOC_ROOT, WEB_DEFAULT_FILE } ); void setup() { - delay(STARTUP_DELAY); sprocket.join(net); }