diff --git a/.github/workflows/upload-idf-component.yml b/.github/workflows/upload-idf-component.yml new file mode 100644 index 00000000..e68384b8 --- /dev/null +++ b/.github/workflows/upload-idf-component.yml @@ -0,0 +1,50 @@ +name: Publish ESP-IDF Component + +on: + workflow_dispatch: + inputs: + tag: + description: 'Component version (1.2.3, 1.2.3-rc1 or 1.2.3.4)' + required: true + git_ref: + description: 'Git ref with the source (branch, tag or commit)' + required: true + +permissions: + contents: read + +jobs: + upload_components: + runs-on: ubuntu-latest + steps: + - name: Get the release tag + env: + head_branch: ${{ inputs.tag || github.event.workflow_run.head_branch }} + run: | + # Read and sanitize the branch/tag name + branch=$(echo "$head_branch" | tr -cd '[:alnum:]/_.-') + + if [[ $branch == refs/tags/* ]]; then + tag="${branch#refs/tags/}" + elif [[ $branch =~ ^[v]*[0-9]+\.[0-9]+\.[0-9]+.*$ ]]; then + tag=$branch + else + echo "Tag not found in $branch. Exiting..." + exit 1 + fi + + echo "Tag: $tag" + echo "RELEASE_TAG=$tag" >> $GITHUB_ENV + + - uses: actions/checkout@v4 + with: + ref: ${{ inputs.git_ref || env.RELEASE_TAG }} + submodules: "recursive" + + - name: Upload components to the component registry + uses: espressif/upload-components-ci-action@v1 + with: + name: espasyncwebserver + version: ${{ env.RELEASE_TAG }} + namespace: esp32async + api_token: ${{ secrets.IDF_COMPONENT_API_TOKEN }} diff --git a/idf_component.yml b/idf_component.yml new file mode 100644 index 00000000..c1432a20 --- /dev/null +++ b/idf_component.yml @@ -0,0 +1,32 @@ +description: "Async Web Server for ESP32 Arduino" +url: "https://github.com/ESP32Async/ESPAsyncWebServer" +license: "LGPL-3.0-or-later" +tags: + - arduino +files: + exclude: + - "idf_component_examples/" + - "idf_component_examples/**/*" + - "examples/" + - "examples/**/*" + - ".gitignore" + - ".clang-format" + - ".gitpod.Dockerfile" + - ".gitpod.yml" + - ".codespellrc" + - ".editorconfig" + - ".pre-commit-config.yaml" + - "CODE_OF_CONDUCT.md" + - "library.json" + - "library.properties" + - "partitions-4MB.csv" + - "platformio.ini" + - "pre-commit.requirements.txt" +dependencies: + esp32async/asynctcp: + version: "^3.3.5" + require: public +examples: + - path: ./idf_component_examples/catchall + - path: ./idf_component_examples/serversentevents + - path: ./idf_component_examples/websocket diff --git a/idf_component_examples/catchall/CMakeLists.txt b/idf_component_examples/catchall/CMakeLists.txt new file mode 100644 index 00000000..664d4587 --- /dev/null +++ b/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/idf_component_examples/catchall/README.md b/idf_component_examples/catchall/README.md new file mode 100644 index 00000000..1e09f91b --- /dev/null +++ b/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/idf_component_examples/catchall/main/CMakeLists.txt b/idf_component_examples/catchall/main/CMakeLists.txt new file mode 100644 index 00000000..9eb7ec47 --- /dev/null +++ b/idf_component_examples/catchall/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "main.cpp" + INCLUDE_DIRS ".") diff --git a/idf_component_examples/catchall/main/idf_component.yml b/idf_component_examples/catchall/main/idf_component.yml new file mode 100644 index 00000000..e2d1c657 --- /dev/null +++ b/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/idf_component_examples/catchall/main/main.cpp b/idf_component_examples/catchall/main/main.cpp new file mode 100644 index 00000000..c4915884 --- /dev/null +++ b/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 +#include +#include + +#include + +static AsyncWebServer server(80); + +static const char *htmlContent PROGMEM = R"( + + + + Sample HTML + + +

Hello, World!

+

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.

+

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.

+

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.

+

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.

+

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.

+

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.

+

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.

+

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.

+ + +)"; + +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/idf_component_examples/catchall/sdkconfig.defaults b/idf_component_examples/catchall/sdkconfig.defaults new file mode 100644 index 00000000..bb723653 --- /dev/null +++ b/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/idf_component_examples/serversentevents/CMakeLists.txt b/idf_component_examples/serversentevents/CMakeLists.txt new file mode 100644 index 00000000..664d4587 --- /dev/null +++ b/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/idf_component_examples/serversentevents/README.md b/idf_component_examples/serversentevents/README.md new file mode 100644 index 00000000..ea21ac97 --- /dev/null +++ b/idf_component_examples/serversentevents/README.md @@ -0,0 +1 @@ +### Basic example to show how to use ServerSentEvents diff --git a/idf_component_examples/serversentevents/main/CMakeLists.txt b/idf_component_examples/serversentevents/main/CMakeLists.txt new file mode 100644 index 00000000..9eb7ec47 --- /dev/null +++ b/idf_component_examples/serversentevents/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "main.cpp" + INCLUDE_DIRS ".") diff --git a/idf_component_examples/serversentevents/main/idf_component.yml b/idf_component_examples/serversentevents/main/idf_component.yml new file mode 100644 index 00000000..e2d1c657 --- /dev/null +++ b/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/idf_component_examples/serversentevents/main/main.cpp b/idf_component_examples/serversentevents/main/main.cpp new file mode 100644 index 00000000..59a1f591 --- /dev/null +++ b/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 +#include +#include + +#include + +static const char *htmlContent PROGMEM = R"( + + + + Server-Sent Events + + + +

Open your browser console!

+ + +)"; + +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/idf_component_examples/serversentevents/sdkconfig.defaults b/idf_component_examples/serversentevents/sdkconfig.defaults new file mode 100644 index 00000000..bb723653 --- /dev/null +++ b/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/idf_component_examples/websocket/CMakeLists.txt b/idf_component_examples/websocket/CMakeLists.txt new file mode 100644 index 00000000..664d4587 --- /dev/null +++ b/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/idf_component_examples/websocket/README.md b/idf_component_examples/websocket/README.md new file mode 100644 index 00000000..3741fc3c --- /dev/null +++ b/idf_component_examples/websocket/README.md @@ -0,0 +1 @@ +### Basic example to show how to use WebSockets diff --git a/idf_component_examples/websocket/main/CMakeLists.txt b/idf_component_examples/websocket/main/CMakeLists.txt new file mode 100644 index 00000000..9eb7ec47 --- /dev/null +++ b/idf_component_examples/websocket/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "main.cpp" + INCLUDE_DIRS ".") diff --git a/idf_component_examples/websocket/main/idf_component.yml b/idf_component_examples/websocket/main/idf_component.yml new file mode 100644 index 00000000..e2d1c657 --- /dev/null +++ b/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/idf_component_examples/websocket/main/main.cpp b/idf_component_examples/websocket/main/main.cpp new file mode 100644 index 00000000..843d1a49 --- /dev/null +++ b/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 +#include +#include + +#include + +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/idf_component_examples/websocket/sdkconfig.defaults b/idf_component_examples/websocket/sdkconfig.defaults new file mode 100644 index 00000000..bb723653 --- /dev/null +++ b/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