diff options
Diffstat (limited to 'libraries/ESP_Async_WebServer/idf_component_examples')
18 files changed, 409 insertions, 0 deletions
diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/catchall/CMakeLists.txt b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/CMakeLists.txt new file mode 100644 index 0000000..664d458 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/CMakeLists.txt @@ -0,0 +1,8 @@ +# For more information about build system see +# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(main) diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/catchall/README.md b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/README.md new file mode 100644 index 0000000..1e09f91 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/README.md @@ -0,0 +1 @@ +### Basic example to show how to catch all requests and send a 404 Not Found response diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/catchall/main/CMakeLists.txt b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/main/CMakeLists.txt new file mode 100644 index 0000000..9eb7ec4 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "main.cpp" + INCLUDE_DIRS ".") diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/catchall/main/idf_component.yml b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/main/idf_component.yml new file mode 100644 index 0000000..e2d1c65 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/main/idf_component.yml @@ -0,0 +1,6 @@ +## IDF Component Manager Manifest File +dependencies: + esp32async/espasyncwebserver: + version: "*" + override_path: "../../../" + pre_release: true diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/catchall/main/main.cpp b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/main/main.cpp new file mode 100644 index 0000000..c491588 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/main/main.cpp @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov + +// +// Shows how to catch all requests and send a 404 Not Found response +// + +#include <Arduino.h> +#include <AsyncTCP.h> +#include <WiFi.h> + +#include <ESPAsyncWebServer.h> + +static AsyncWebServer server(80); + +static const char *htmlContent PROGMEM = R"( +<!DOCTYPE html> +<html> +<head> + <title>Sample HTML</title> +</head> +<body> + <h1>Hello, World!</h1> + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin euismod, purus a euismod + rhoncus, urna ipsum cursus massa, eu dictum tellus justo ac justo. Quisque ullamcorper + arcu nec tortor ullamcorper, vel fermentum justo fermentum. Vivamus sed velit ut elit + accumsan congue ut ut enim. Ut eu justo eu lacus varius gravida ut a tellus. Nulla facilisi. + Integer auctor consectetur ultricies. Fusce feugiat, mi sit amet bibendum viverra, orci leo + dapibus elit, id varius sem dui id lacus.</p> + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin euismod, purus a euismod + rhoncus, urna ipsum cursus massa, eu dictum tellus justo ac justo. Quisque ullamcorper + arcu nec tortor ullamcorper, vel fermentum justo fermentum. Vivamus sed velit ut elit + accumsan congue ut ut enim. Ut eu justo eu lacus varius gravida ut a tellus. Nulla facilisi. + Integer auctor consectetur ultricies. Fusce feugiat, mi sit amet bibendum viverra, orci leo + dapibus elit, id varius sem dui id lacus.</p> + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin euismod, purus a euismod + rhoncus, urna ipsum cursus massa, eu dictum tellus justo ac justo. Quisque ullamcorper + arcu nec tortor ullamcorper, vel fermentum justo fermentum. Vivamus sed velit ut elit + accumsan congue ut ut enim. Ut eu justo eu lacus varius gravida ut a tellus. Nulla facilisi. + Integer auctor consectetur ultricies. Fusce feugiat, mi sit amet bibendum viverra, orci leo + dapibus elit, id varius sem dui id lacus.</p> + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin euismod, purus a euismod + rhoncus, urna ipsum cursus massa, eu dictum tellus justo ac justo. Quisque ullamcorper + arcu nec tortor ullamcorper, vel fermentum justo fermentum. Vivamus sed velit ut elit + accumsan congue ut ut enim. Ut eu justo eu lacus varius gravida ut a tellus. Nulla facilisi. + Integer auctor consectetur ultricies. Fusce feugiat, mi sit amet bibendum viverra, orci leo + dapibus elit, id varius sem dui id lacus.</p> + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin euismod, purus a euismod + rhoncus, urna ipsum cursus massa, eu dictum tellus justo ac justo. Quisque ullamcorper + arcu nec tortor ullamcorper, vel fermentum justo fermentum. Vivamus sed velit ut elit + accumsan congue ut ut enim. Ut eu justo eu lacus varius gravida ut a tellus. Nulla facilisi. + Integer auctor consectetur ultricies. Fusce feugiat, mi sit amet bibendum viverra, orci leo + dapibus elit, id varius sem dui id lacus.</p> + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin euismod, purus a euismod + rhoncus, urna ipsum cursus massa, eu dictum tellus justo ac justo. Quisque ullamcorper + arcu nec tortor ullamcorper, vel fermentum justo fermentum. Vivamus sed velit ut elit + accumsan congue ut ut enim. Ut eu justo eu lacus varius gravida ut a tellus. Nulla facilisi. + Integer auctor consectetur ultricies. Fusce feugiat, mi sit amet bibendum viverra, orci leo + dapibus elit, id varius sem dui id lacus.</p> + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin euismod, purus a euismod + rhoncus, urna ipsum cursus massa, eu dictum tellus justo ac justo. Quisque ullamcorper + arcu nec tortor ullamcorper, vel fermentum justo fermentum. Vivamus sed velit ut elit + accumsan congue ut ut enim. Ut eu justo eu lacus varius gravida ut a tellus. Nulla facilisi. + Integer auctor consectetur ultricies. Fusce feugiat, mi sit amet bibendum viverra, orci leo + dapibus elit, id varius sem dui id lacus.</p> + <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin euismod, purus a euismod + rhoncus, urna ipsum cursus massa, eu dictum tellus justo ac justo. Quisque ullamcorper + arcu nec tortor ullamcorper, vel fermentum justo fermentum. Vivamus sed velit ut elit + accumsan congue ut ut enim. Ut eu justo eu lacus varius gravida ut a tellus. Nulla facilisi. + Integer auctor consectetur ultricies. Fusce feugiat, mi sit amet bibendum viverra, orci leo + dapibus elit, id varius sem dui id lacus.</p> +</body> +</html> +)"; + +static const size_t htmlContentLength = strlen_P(htmlContent); + +void setup() { + Serial.begin(115200); + +#ifndef CONFIG_IDF_TARGET_ESP32H2 + WiFi.mode(WIFI_AP); + WiFi.softAP("esp-captive"); +#endif + + // curl -v http://192.168.4.1/ + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { + // need to cast to uint8_t* + // if you do not, the const char* will be copied in a temporary String buffer + request->send(200, "text/html", (uint8_t *)htmlContent, htmlContentLength); + }); + + // catch any request, and send a 404 Not Found response + // except for /game_log which is handled by onRequestBody + // + // curl -v http://192.168.4.1/foo + // + server.onNotFound([](AsyncWebServerRequest *request) { + if (request->url() == "/game_log") { + return; // response object already created by onRequestBody + } + + request->send(404, "text/plain", "Not found"); + }); + + // See: https://github.com/ESP32Async/ESPAsyncWebServer/issues/6 + // catch any POST request and send a 200 OK response + // + // curl -v -X POST http://192.168.4.1/game_log -H "Content-Type: application/json" -d '{"game": "test"}' + // + server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) { + if (request->url() == "/game_log") { + request->send(200, "application/json", "{\"status\":\"OK\"}"); + } + // note that there is no else here: the goal is only to prepare a response based on some body content + // onNotFound will always be called after this, and will not override the response object if `/game_log` is requested + }); + + server.begin(); +} + +// not needed +void loop() { + delay(100); +} diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/catchall/sdkconfig.defaults b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/sdkconfig.defaults new file mode 100644 index 0000000..bb72365 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/catchall/sdkconfig.defaults @@ -0,0 +1,12 @@ +# +# Arduino ESP32 +# +CONFIG_AUTOSTART_ARDUINO=y +# end of Arduino ESP32 + +# +# FREERTOS +# +CONFIG_FREERTOS_HZ=1000 +# end of FREERTOS +# end of Component config diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/CMakeLists.txt b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/CMakeLists.txt new file mode 100644 index 0000000..664d458 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/CMakeLists.txt @@ -0,0 +1,8 @@ +# For more information about build system see +# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(main) diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/README.md b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/README.md new file mode 100644 index 0000000..ea21ac9 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/README.md @@ -0,0 +1 @@ +### Basic example to show how to use ServerSentEvents diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/main/CMakeLists.txt b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/main/CMakeLists.txt new file mode 100644 index 0000000..9eb7ec4 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "main.cpp" + INCLUDE_DIRS ".") diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/main/idf_component.yml b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/main/idf_component.yml new file mode 100644 index 0000000..e2d1c65 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/main/idf_component.yml @@ -0,0 +1,6 @@ +## IDF Component Manager Manifest File +dependencies: + esp32async/espasyncwebserver: + version: "*" + override_path: "../../../" + pre_release: true diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/main/main.cpp b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/main/main.cpp new file mode 100644 index 0000000..59a1f59 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/main/main.cpp @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov + +// +// SSE example +// + +#include <Arduino.h> +#include <AsyncTCP.h> +#include <WiFi.h> + +#include <ESPAsyncWebServer.h> + +static const char *htmlContent PROGMEM = R"( +<!DOCTYPE html> +<html> +<head> + <title>Server-Sent Events</title> + <script> + if (!!window.EventSource) { + var source = new EventSource('/events'); + source.addEventListener('open', function(e) { + console.log("Events Connected"); + }, false); + source.addEventListener('error', function(e) { + if (e.target.readyState != EventSource.OPEN) { + console.log("Events Disconnected"); + } + }, false); + source.addEventListener('message', function(e) { + console.log("message", e.data); + }, false); + source.addEventListener('heartbeat', function(e) { + console.log("heartbeat", e.data); + }, false); + } + </script> +</head> +<body> + <h1>Open your browser console!</h1> +</body> +</html> +)"; + +static const size_t htmlContentLength = strlen_P(htmlContent); + +static AsyncWebServer server(80); +static AsyncEventSource events("/events"); + +void setup() { + Serial.begin(115200); + +#ifndef CONFIG_IDF_TARGET_ESP32H2 + WiFi.mode(WIFI_AP); + WiFi.softAP("esp-captive"); +#endif + + // curl -v http://192.168.4.1/ + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) { + // need to cast to uint8_t* + // if you do not, the const char* will be copied in a temporary String buffer + request->send(200, "text/html", (uint8_t *)htmlContent, htmlContentLength); + }); + + events.onConnect([](AsyncEventSourceClient *client) { + Serial.printf("SSE Client connected! ID: %" PRIu32 "\n", client->lastId()); + client->send("hello!", NULL, millis(), 1000); + }); + + events.onDisconnect([](AsyncEventSourceClient *client) { + Serial.printf("SSE Client disconnected! ID: %" PRIu32 "\n", client->lastId()); + }); + + server.addHandler(&events); + + server.begin(); +} + +static uint32_t lastSSE = 0; +static uint32_t deltaSSE = 3000; + +static uint32_t lastHeap = 0; + +void loop() { + uint32_t now = millis(); + if (now - lastSSE >= deltaSSE) { + events.send(String("ping-") + now, "heartbeat", now); + lastSSE = millis(); + } + + if (now - lastHeap >= 2000) { + Serial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap()); + lastHeap = now; + } +} diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/sdkconfig.defaults b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/sdkconfig.defaults new file mode 100644 index 0000000..bb72365 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/serversentevents/sdkconfig.defaults @@ -0,0 +1,12 @@ +# +# Arduino ESP32 +# +CONFIG_AUTOSTART_ARDUINO=y +# end of Arduino ESP32 + +# +# FREERTOS +# +CONFIG_FREERTOS_HZ=1000 +# end of FREERTOS +# end of Component config diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/websocket/CMakeLists.txt b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/CMakeLists.txt new file mode 100644 index 0000000..664d458 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/CMakeLists.txt @@ -0,0 +1,8 @@ +# For more information about build system see +# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(main) diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/websocket/README.md b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/README.md new file mode 100644 index 0000000..3741fc3 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/README.md @@ -0,0 +1 @@ +### Basic example to show how to use WebSockets diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/CMakeLists.txt b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/CMakeLists.txt new file mode 100644 index 0000000..9eb7ec4 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "main.cpp" + INCLUDE_DIRS ".") diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/idf_component.yml b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/idf_component.yml new file mode 100644 index 0000000..e2d1c65 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/idf_component.yml @@ -0,0 +1,6 @@ +## IDF Component Manager Manifest File +dependencies: + esp32async/espasyncwebserver: + version: "*" + override_path: "../../../" + pre_release: true diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/main.cpp b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/main.cpp new file mode 100644 index 0000000..843d1a4 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/main/main.cpp @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov + +// +// WebSocket example +// + +#include <Arduino.h> +#include <AsyncTCP.h> +#include <WiFi.h> + +#include <ESPAsyncWebServer.h> + +static AsyncWebServer server(80); +static AsyncWebSocket ws("/ws"); + +void setup() { + Serial.begin(115200); + +#ifndef CONFIG_IDF_TARGET_ESP32H2 + WiFi.mode(WIFI_AP); + WiFi.softAP("esp-captive"); +#endif + + // + // Run in terminal 1: websocat ws://192.168.4.1/ws => should stream data + // Run in terminal 2: websocat ws://192.168.4.1/ws => should stream data + // Run in terminal 3: websocat ws://192.168.4.1/ws => should fail: + // + // To send a message to the WebSocket server: + // + // echo "Hello!" | websocat ws://192.168.4.1/ws + // + ws.onEvent([](AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { + (void)len; + + if (type == WS_EVT_CONNECT) { + ws.textAll("new client connected"); + Serial.println("ws connect"); + client->setCloseClientOnQueueFull(false); + client->ping(); + + } else if (type == WS_EVT_DISCONNECT) { + ws.textAll("client disconnected"); + Serial.println("ws disconnect"); + + } else if (type == WS_EVT_ERROR) { + Serial.println("ws error"); + + } else if (type == WS_EVT_PONG) { + Serial.println("ws pong"); + + } else if (type == WS_EVT_DATA) { + AwsFrameInfo *info = (AwsFrameInfo *)arg; + String msg = ""; + if (info->final && info->index == 0 && info->len == len) { + if (info->opcode == WS_TEXT) { + data[len] = 0; + Serial.printf("ws text: %s\n", (char *)data); + } + } + } + }); + + // shows how to prevent a third WS client to connect + server.addHandler(&ws).addMiddleware([](AsyncWebServerRequest *request, ArMiddlewareNext next) { + // ws.count() is the current count of WS clients: this one is trying to upgrade its HTTP connection + if (ws.count() > 1) { + // if we have 2 clients or more, prevent the next one to connect + request->send(503, "text/plain", "Server is busy"); + } else { + // process next middleware and at the end the handler + next(); + } + }); + + server.addHandler(&ws); + + server.begin(); +} + +static uint32_t lastWS = 0; +static uint32_t deltaWS = 100; + +static uint32_t lastHeap = 0; + +void loop() { + uint32_t now = millis(); + + if (now - lastWS >= deltaWS) { + ws.printfAll("kp%.4f", (10.0 / 3.0)); + lastWS = millis(); + } + + if (now - lastHeap >= 2000) { + // cleanup disconnected clients or too many clients + ws.cleanupClients(); + + Serial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap()); + lastHeap = now; + } +} diff --git a/libraries/ESP_Async_WebServer/idf_component_examples/websocket/sdkconfig.defaults b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/sdkconfig.defaults new file mode 100644 index 0000000..bb72365 --- /dev/null +++ b/libraries/ESP_Async_WebServer/idf_component_examples/websocket/sdkconfig.defaults @@ -0,0 +1,12 @@ +# +# Arduino ESP32 +# +CONFIG_AUTOSTART_ARDUINO=y +# end of Arduino ESP32 + +# +# FREERTOS +# +CONFIG_FREERTOS_HZ=1000 +# end of FREERTOS +# end of Component config |
