udp stuff

This commit is contained in:
2021-08-29 00:17:57 +02:00
parent 851a2084f3
commit 8af3f408db
10 changed files with 116 additions and 12 deletions

3
.gitmodules vendored
View File

@ -40,3 +40,6 @@
[submodule "components/espasyncota"] [submodule "components/espasyncota"]
path = components/espasyncota path = components/espasyncota
url = git@github.com:0xFEEDC0DE64/espasyncota.git url = git@github.com:0xFEEDC0DE64/espasyncota.git
[submodule "components/espasyncudplistener"]
path = components/espasyncudplistener
url = git@github.com:0xFEEDC0DE64/espasyncudplistener

View File

@ -2,6 +2,7 @@ set(headers
ble.h ble.h
led.h led.h
ota.h ota.h
udpcontrol.h
webserver.h webserver.h
wifi.h wifi.h
) )
@ -11,14 +12,15 @@ set(sources
led.cpp led.cpp
main.cpp main.cpp
ota.cpp ota.cpp
udpcontrol.cpp
webserver.cpp webserver.cpp
wifi.cpp wifi.cpp
) )
set(dependencies set(dependencies
freertos nvs_flash esp_http_server esp_https_ota mdns app_update esp_system esp_websocket_client driver 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 arduino-esp32 ArduinoJson FastLED-idf cpputils date espchrono espasyncota espasyncudplistener espcpputils
esphttpdutils espwifistack expected fmt esp-nimble-cpp esphttpdutils espwifistack expected fmt
) )
idf_component_register( idf_component_register(

View File

@ -1,8 +1,5 @@
#include "led.h" #include "led.h"
// system includes
#include <array>
// esp-idf includes // esp-idf includes
#include <driver/ledc.h> #include <driver/ledc.h>
@ -10,7 +7,6 @@
#include <Arduino.h> #include <Arduino.h>
// 3rdparty lib includes // 3rdparty lib includes
#include <FastLED.h>
#include <espchrono.h> #include <espchrono.h>
using namespace std::chrono_literals; using namespace std::chrono_literals;
@ -19,8 +15,6 @@ using namespace std::chrono_literals;
#define LEDC_BASE_FREQ 5000 #define LEDC_BASE_FREQ 5000
namespace { namespace {
std::array<CRGB, 5> leds;
CRGBPalette16 currentPalette; CRGBPalette16 currentPalette;
TBlendType currentBlending; TBlendType currentBlending;
@ -53,6 +47,9 @@ void FillLEDsFromPaletteColors( uint8_t colorIndex);
espchrono::millis_clock::time_point lastRedraw{}; espchrono::millis_clock::time_point lastRedraw{};
} }
std::array<CRGB, 5> leds;
bool ledAnimationEnabled{true};
void led_setup() void led_setup()
{ {
currentPalette = RainbowColors_p; currentPalette = RainbowColors_p;
@ -105,12 +102,15 @@ void led_update()
return; return;
lastRedraw = espchrono::millis_clock::now(); lastRedraw = espchrono::millis_clock::now();
ChangePalettePeriodically(); if (ledAnimationEnabled)
{
ChangePalettePeriodically();
static uint8_t startIndex = 0; static uint8_t startIndex = 0;
startIndex = startIndex + 1; /* motion speed */ startIndex = startIndex + 1; /* motion speed */
FillLEDsFromPaletteColors(startIndex); FillLEDsFromPaletteColors(startIndex);
}
ledcAnalogWrite(0, leds[0].red); ledcAnalogWrite(0, leds[0].red);
ledcAnalogWrite(1, leds[0].green); ledcAnalogWrite(1, leds[0].green);

View File

@ -1,4 +1,13 @@
#pragma once #pragma once
// system includes
#include <array>
// 3rdparty lib includes
#include <FastLED.h>
extern std::array<CRGB, 5> leds;
extern bool ledAnimationEnabled;
void led_setup(); void led_setup();
void led_update(); void led_update();

View File

@ -20,6 +20,8 @@
#include "ble.h" #include "ble.h"
#include "webserver.h" #include "webserver.h"
#include "ota.h" #include "ota.h"
#include "udpcontrol.h"
#include "udpcontrol.h"
#include "led.h" #include "led.h"
using namespace std::chrono_literals; using namespace std::chrono_literals;
@ -67,6 +69,7 @@ extern "C" void app_main()
ble_setup(); ble_setup();
webserver_setup(); webserver_setup();
ota_setup(); ota_setup();
udpcontrol_setup();
led_setup(); led_setup();
while (true) while (true)
@ -80,6 +83,7 @@ extern "C" void app_main()
ble_update(); ble_update();
webserver_update(); webserver_update();
ota_update(); ota_update();
udpcontrol_update();
led_update(); led_update();
} }
} }

31
main/udpcontrol.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "udpcontrol.h"
// esp-idf includes
#include <esp_log.h>
// 3rdparty lib includes
#include <asyncudplistener.h>
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());
}

4
main/udpcontrol.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
void udpcontrol_setup();
void udpcontrol_update();

View File

@ -13,6 +13,7 @@
// local includes // local includes
#include "wifi.h" #include "wifi.h"
#include "ota.h" #include "ota.h"
#include "led.h"
using namespace esphttpdutils; 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_root_handler(httpd_req_t *req);
esp_err_t webserver_ota_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 } // namespace
void webserver_setup() void webserver_setup()
@ -41,6 +43,7 @@ void webserver_setup()
for (const httpd_uri_t &uri : { for (const httpd_uri_t &uri : {
httpd_uri_t { .uri = "/", .method = HTTP_GET, .handler = webserver_root_handler, .user_ctx = NULL }, 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 = "/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); 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("<h1>{}</h1>", htmlentities(deviceName)); std::string body = fmt::format("<h1>{}</h1>", htmlentities(deviceName));
body += fmt::format(
"<form action=\"/set\">"
"<input type=\"hidden\" name=\"key\" value=\"ledAnimationEnabled\" />"
"<label>ledAnimationEnabled <input type=\"checkbox\" name=\"value\" value=\"on\"{} /></label>"
"<button type=\"submit\">Save</button>"
"</form>", ledAnimationEnabled ? " checked" : "");
if (const auto otaStatus = asyncOta.status(); otaStatus == OtaCloudUpdateStatus::Idle) if (const auto otaStatus = asyncOta.status(); otaStatus == OtaCloudUpdateStatus::Idle)
{ {
body += 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...") 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;
}
} }

View File

@ -162,6 +162,18 @@ CONFIG_ARDUINO_SELECTIVE_SPI=y
CONFIG_ARDUINO_SELECTIVE_Wire=y CONFIG_ARDUINO_SELECTIVE_Wire=y
# end of Arduino Configuration # 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 # espcpputils settings
# #