From 88558532ee48f3ee44c55ce3b7a7d84e9cfa889f Mon Sep 17 00:00:00 2001 From: Patrick Balsiger Date: Mon, 24 Sep 2018 12:10:09 +0200 Subject: [PATCH] optimize printing, add release profile --- .vscode/settings.json | 4 ++- api.md | 43 +++++++++++++++++++++++++++--- lib/sprocket-utils/utils_print.cpp | 2 +- platformio.ini | 29 +++++++++++++------- src/IlluCat.h | 18 ++++++++----- src/PixelPlugin.h | 4 +-- 6 files changed, 76 insertions(+), 24 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index feb10db..c61cd55 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,8 @@ "deque": "cpp", "list": "cpp", "unordered_map": "cpp", - "vector": "cpp" + "vector": "cpp", + "tuple": "cpp", + "utility": "cpp" } } \ No newline at end of file diff --git a/api.md b/api.md index 0635ace..cd74ef9 100644 --- a/api.md +++ b/api.md @@ -1,10 +1,18 @@ # API Two API architectures are supported: WebSocket and REST. Both can be used with the same set of fields. -Everything is propagated to the mesh network automatically. -As everything can be controlled with topics, only two fields are required: +As everything can be controlled with topics, only a few fields are required: +| Field | Type | Description | +| ----- | ---- | ---- | +| topic | String | Topic where the payload is dispatched | +| payload | String | Payload to be dispatched | +| broadcast | Integer | Where to send the payload; 0 = local, 1 = broadcast | + + + - topic - payload +- broadcast ## Topics All functionality can be used by sending messages to these topics. @@ -35,5 +43,32 @@ Example: Content-Type: application/x-www-form-urlencoded POST /pixel/api Request: Form post with topic and payload as fields -Response: 200 + JSON message that was used to notify the topics - \ No newline at end of file +Response: 200 + final payload as JSON + +Examples: +```shell +# set color by wheel +curl -v -X POST \ + --data "topic=pixels/colorWheel" \ + --data "payload=20" \ + http://illucat/pixel/api + +# set color only on current node +curl -v -X POST \ + --data "topic=pixels/colorWheel" \ + --data "payload=20" \ + --data "broadcast=1"\ + http://illucat/pixel/api + +# set brightness +curl -v -X POST \ + --data "topic=pixels/brightness" \ + --data "payload=10" \ + http://illucat/pixel/api + +# run rainbow pattern +curl -v -X POST \ + --data "topic=pixels/pattern" \ + --data "payload=1" \ + http://illucat/pixel/api +``` \ No newline at end of file diff --git a/lib/sprocket-utils/utils_print.cpp b/lib/sprocket-utils/utils_print.cpp index 20425ee..81fe4b2 100644 --- a/lib/sprocket-utils/utils_print.cpp +++ b/lib/sprocket-utils/utils_print.cpp @@ -20,6 +20,6 @@ void PRINT_MSG(Print &out, const char* prefix, const char* format, ...) { vsnprintf(ptr, sizeof(formatString)-1-strlen(formatString), formatString, args ); va_end (args); formatString[ sizeof(formatString)-1 ]='\0'; - out.print(ptr); + out.println(ptr); } } \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index 04877fb..13300eb 100644 --- a/platformio.ini +++ b/platformio.ini @@ -22,6 +22,13 @@ lib_deps = ESPAsyncTCP TaskScheduler SPIFFS + painlessMesh + ESP8266mDNS + ArduinoOTA + ArduinoJson + ESP Async WebServer + ESPAsyncTCP + Adafruit NeoPixel [env:build] platform = ${common.platform} @@ -30,13 +37,17 @@ upload_speed = ${common.upload_speed} monitor_baud = ${common.monitor_baud} framework = ${common.framework} build_flags = -Wl,-Teagle.flash.4m1m.ld -;lib_extra_dirs = ~/src/illucat/.piolibdeps/sprocket-core/lib + -DSPROCKET_PRINT=1 lib_deps = ${common.lib_deps} - painlessMesh - ESP8266mDNS - ArduinoOTA - ArduinoJson - ESP Async WebServer - ESPAsyncTCP - Adafruit NeoPixel - https://gitlab.com/wirelos/sprocket-core.git#develop \ No newline at end of file + https://gitlab.com/wirelos/sprocket-core.git#develop + +[env:release] +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=0 +lib_deps = ${common.lib_deps} + https://gitlab.com/wirelos/sprocket-core.git#master \ No newline at end of file diff --git a/src/IlluCat.h b/src/IlluCat.h index c4980be..b1b010a 100644 --- a/src/IlluCat.h +++ b/src/IlluCat.h @@ -90,9 +90,10 @@ class IlluCat : public MeshSprocket { // plugin? dnsServer->setErrorReplyCode(DNSReplyCode::NoError); dnsServer->start(DNS_PORT, "*", WiFi.softAPIP()); - Serial.println("SoftAP IP: " + WiFi.softAPIP().toString()); + String softApPrt = "SoftAP IP: " + WiFi.softAPIP().toString(); + PRINT_MSG(Serial, SPROCKET_TYPE, softApPrt.c_str()); - // TODO plugin + // 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)); @@ -111,30 +112,33 @@ class IlluCat : public MeshSprocket { } void patternWebRequestHandler(AsyncWebServerRequest *request) { - Serial.println("POST /pixel/state"); + 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(); - net->mesh.sendBroadcast(msg); publish(currentMessage.topic, currentMessage.payload); + if(currentMessage.broadcast){ + net->mesh.sendBroadcast(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); - net->mesh.sendBroadcast(frame); dispatch(0, frame); - //client->text(String(millis())); } } virtual void dispatch( uint32_t from, String &msg ) { - //Serial.println(msg); currentMessage.fromJsonString(msg); if(currentMessage.valid){ currentMessage.from = from; publish(currentMessage.topic, currentMessage.payload); + if(currentMessage.broadcast){ + net->mesh.sendBroadcast(msg); + } } } diff --git a/src/PixelPlugin.h b/src/PixelPlugin.h index 1e4ae5c..7c7cc16 100644 --- a/src/PixelPlugin.h +++ b/src/PixelPlugin.h @@ -38,9 +38,9 @@ class PixelPlugin : public Plugin { animation.set(TASK_MILLISECOND * pixelConfig.updateInterval, TASK_FOREVER, bind(&PixelPlugin::animate, this)); userScheduler->addTask(animation); animation.enable(); - Serial.println("NeoPixels activated"); + PRINT_MSG(Serial, SPROCKET_TYPE, "NeoPixels activated"); } - void setState(String msg){ + void setState(String msg) { state.fromJsonString(msg); pixels->setBrightness(state.brightness); pixels->ColorSet(state.color);