From 8af3f408db8513597f17e7798a04c2b9d7fe07bf Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sun, 29 Aug 2021 00:17:57 +0200 Subject: [PATCH] udp stuff --- .gitmodules | 3 +++ components/espasyncudplistener | 1 + main/CMakeLists.txt | 6 ++++-- main/led.cpp | 20 +++++++++--------- main/led.h | 9 ++++++++ main/main.cpp | 4 ++++ main/udpcontrol.cpp | 31 +++++++++++++++++++++++++++ main/udpcontrol.h | 4 ++++ main/webserver.cpp | 38 ++++++++++++++++++++++++++++++++++ sdkconfig | 12 +++++++++++ 10 files changed, 116 insertions(+), 12 deletions(-) create mode 160000 components/espasyncudplistener create mode 100644 main/udpcontrol.cpp create mode 100644 main/udpcontrol.h diff --git a/.gitmodules b/.gitmodules index 8541c06..2899f6f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -40,3 +40,6 @@ [submodule "components/espasyncota"] path = components/espasyncota url = git@github.com:0xFEEDC0DE64/espasyncota.git +[submodule "components/espasyncudplistener"] + path = components/espasyncudplistener + url = git@github.com:0xFEEDC0DE64/espasyncudplistener diff --git a/components/espasyncudplistener b/components/espasyncudplistener new file mode 160000 index 0000000..1db1655 --- /dev/null +++ b/components/espasyncudplistener @@ -0,0 +1 @@ +Subproject commit 1db1655f18d690d3f292f6584cf07c0d74259b1f diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index e839778..11aced2 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -2,6 +2,7 @@ set(headers ble.h led.h ota.h + udpcontrol.h webserver.h wifi.h ) @@ -11,14 +12,15 @@ set(sources led.cpp main.cpp ota.cpp + udpcontrol.cpp webserver.cpp wifi.cpp ) set(dependencies freertos nvs_flash esp_http_server esp_https_ota mdns app_update esp_system esp_websocket_client driver - arduino-esp32 ArduinoJson FastLED-idf cpputils date espchrono espasyncota espcpputils esp-nimble-cpp - esphttpdutils espwifistack expected fmt + arduino-esp32 ArduinoJson FastLED-idf cpputils date espchrono espasyncota espasyncudplistener espcpputils + esp-nimble-cpp esphttpdutils espwifistack expected fmt ) idf_component_register( diff --git a/main/led.cpp b/main/led.cpp index 5b20558..99e344e 100644 --- a/main/led.cpp +++ b/main/led.cpp @@ -1,8 +1,5 @@ #include "led.h" -// system includes -#include - // esp-idf includes #include @@ -10,7 +7,6 @@ #include // 3rdparty lib includes -#include #include using namespace std::chrono_literals; @@ -19,8 +15,6 @@ using namespace std::chrono_literals; #define LEDC_BASE_FREQ 5000 namespace { -std::array leds; - CRGBPalette16 currentPalette; TBlendType currentBlending; @@ -53,6 +47,9 @@ void FillLEDsFromPaletteColors( uint8_t colorIndex); espchrono::millis_clock::time_point lastRedraw{}; } +std::array leds; +bool ledAnimationEnabled{true}; + void led_setup() { currentPalette = RainbowColors_p; @@ -105,12 +102,15 @@ void led_update() return; lastRedraw = espchrono::millis_clock::now(); - ChangePalettePeriodically(); + if (ledAnimationEnabled) + { + ChangePalettePeriodically(); - static uint8_t startIndex = 0; - startIndex = startIndex + 1; /* motion speed */ + static uint8_t startIndex = 0; + startIndex = startIndex + 1; /* motion speed */ - FillLEDsFromPaletteColors(startIndex); + FillLEDsFromPaletteColors(startIndex); + } ledcAnalogWrite(0, leds[0].red); ledcAnalogWrite(1, leds[0].green); diff --git a/main/led.h b/main/led.h index e6a3c24..9a498b3 100644 --- a/main/led.h +++ b/main/led.h @@ -1,4 +1,13 @@ #pragma once +// system includes +#include + +// 3rdparty lib includes +#include + +extern std::array leds; +extern bool ledAnimationEnabled; + void led_setup(); void led_update(); diff --git a/main/main.cpp b/main/main.cpp index 2c4b216..db66d61 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -20,6 +20,8 @@ #include "ble.h" #include "webserver.h" #include "ota.h" +#include "udpcontrol.h" +#include "udpcontrol.h" #include "led.h" using namespace std::chrono_literals; @@ -67,6 +69,7 @@ extern "C" void app_main() ble_setup(); webserver_setup(); ota_setup(); + udpcontrol_setup(); led_setup(); while (true) @@ -80,6 +83,7 @@ extern "C" void app_main() ble_update(); webserver_update(); ota_update(); + udpcontrol_update(); led_update(); } } diff --git a/main/udpcontrol.cpp b/main/udpcontrol.cpp new file mode 100644 index 0000000..26778c2 --- /dev/null +++ b/main/udpcontrol.cpp @@ -0,0 +1,31 @@ +#include "udpcontrol.h" + +// esp-idf includes +#include + +// 3rdparty lib includes +#include + +namespace { +constexpr const char * const TAG = "UDP_CONTROL"; + +AsyncUdpListener udp; +} // namespace + +void udpcontrol_setup() +{ + if (!udp.listen(1234)) + { + ESP_LOGE(TAG, "could not start listening on udp (port=%i)", 1234); + return; + } +} + +void udpcontrol_update() +{ + const auto &packet = udp.poll(); + if (!packet) + return; + + ESP_LOGI(TAG, "%s responded \"%.*s\"", wifi_stack::toString(packet->remoteAddr()).c_str(), packet->data().size(), packet->data().data()); +} diff --git a/main/udpcontrol.h b/main/udpcontrol.h new file mode 100644 index 0000000..5092669 --- /dev/null +++ b/main/udpcontrol.h @@ -0,0 +1,4 @@ +#pragma once + +void udpcontrol_setup(); +void udpcontrol_update(); diff --git a/main/webserver.cpp b/main/webserver.cpp index 78cd051..177dfe8 100644 --- a/main/webserver.cpp +++ b/main/webserver.cpp @@ -13,6 +13,7 @@ // local includes #include "wifi.h" #include "ota.h" +#include "led.h" using namespace esphttpdutils; @@ -23,6 +24,7 @@ httpd_handle_t httpdHandle; esp_err_t webserver_root_handler(httpd_req_t *req); esp_err_t webserver_ota_handler(httpd_req_t *req); +esp_err_t webserver_set_handler(httpd_req_t *req); } // namespace void webserver_setup() @@ -41,6 +43,7 @@ void webserver_setup() for (const httpd_uri_t &uri : { httpd_uri_t { .uri = "/", .method = HTTP_GET, .handler = webserver_root_handler, .user_ctx = NULL }, httpd_uri_t { .uri = "/ota", .method = HTTP_GET, .handler = webserver_ota_handler, .user_ctx = NULL }, + httpd_uri_t { .uri = "/set", .method = HTTP_GET, .handler = webserver_set_handler, .user_ctx = NULL }, }) { const auto result = httpd_register_uri_handler(httpdHandle, &uri); @@ -60,6 +63,13 @@ esp_err_t webserver_root_handler(httpd_req_t *req) { std::string body = fmt::format("

{}

", htmlentities(deviceName)); + body += fmt::format( + "
" + "" + "" + "" + "
", ledAnimationEnabled ? " checked" : ""); + if (const auto otaStatus = asyncOta.status(); otaStatus == OtaCloudUpdateStatus::Idle) { body += @@ -132,4 +142,32 @@ esp_err_t webserver_ota_handler(httpd_req_t *req) CALL_AND_EXIT(webserver_resp_send_succ, req, "text/plain", "OTA called...") } + +esp_err_t webserver_set_handler(httpd_req_t *req) +{ + std::string query; + + if (const size_t queryLength = httpd_req_get_url_query_len(req)) + { + query.resize(queryLength); + CALL_AND_EXIT_ON_ERROR(httpd_req_get_url_query_str, req, query.data(), query.size() + 1) + } + + char urlBufEncoded[256]; + if (const auto result = httpd_query_key_value(query.data(), "url", urlBufEncoded, sizeof(urlBufEncoded)); result == ESP_ERR_NOT_FOUND) + { + CALL_AND_EXIT(webserver_resp_send_err, req, HTTPD_400_BAD_REQUEST, "text/plain", "url parameter missing") + } + else if (result != ESP_OK) + { + const auto msg = fmt::format("httpd_query_key_value() {} failed with {}", "url", esp_err_to_name(result)); + ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); + CALL_AND_EXIT(webserver_resp_send_err, req, HTTPD_400_BAD_REQUEST, "text/plain", msg) + } + + char urlBuf[257]; + esphttpdutils::urldecode(urlBuf, urlBufEncoded); + + return ESP_OK; +} } diff --git a/sdkconfig b/sdkconfig index f0b6ecc..b0112ac 100644 --- a/sdkconfig +++ b/sdkconfig @@ -162,6 +162,18 @@ CONFIG_ARDUINO_SELECTIVE_SPI=y CONFIG_ARDUINO_SELECTIVE_Wire=y # end of Arduino Configuration +# +# Simple Async UDP Listener +# +# CONFIG_LOG_LOCAL_LEVEL_ASYNC_UDP_LISTENER_NONE is not set +# CONFIG_LOG_LOCAL_LEVEL_ASYNC_UDP_LISTENER_ERROR is not set +# CONFIG_LOG_LOCAL_LEVEL_ASYNC_UDP_LISTENER_WARN is not set +CONFIG_LOG_LOCAL_LEVEL_ASYNC_UDP_LISTENER_INFO=y +# CONFIG_LOG_LOCAL_LEVEL_ASYNC_UDP_LISTENER_DEBUG is not set +# CONFIG_LOG_LOCAL_LEVEL_ASYNC_UDP_LISTENER_VERBOSE is not set +CONFIG_LOG_LOCAL_LEVEL_ASYNC_UDP_LISTENER=3 +# end of Simple Async UDP Listener + # # espcpputils settings #