From 85ea4e995a75abe061f6fc375ea0481084dddd43 Mon Sep 17 00:00:00 2001 From: schererleander Date: Tue, 20 Jan 2026 08:34:54 +0100 Subject: initial commit --- .../examples/CaptivePortal/CaptivePortal.ino | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 libraries/ESP_Async_WebServer/examples/CaptivePortal/CaptivePortal.ino (limited to 'libraries/ESP_Async_WebServer/examples/CaptivePortal/CaptivePortal.ino') diff --git a/libraries/ESP_Async_WebServer/examples/CaptivePortal/CaptivePortal.ino b/libraries/ESP_Async_WebServer/examples/CaptivePortal/CaptivePortal.ino new file mode 100644 index 0000000..a872a9b --- /dev/null +++ b/libraries/ESP_Async_WebServer/examples/CaptivePortal/CaptivePortal.ino @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +// Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov + +#include +#ifdef ESP32 +#include +#include +#elif defined(ESP8266) +#include +#include +#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350) +#include +#include +#endif +#include "ESPAsyncWebServer.h" + +static DNSServer dnsServer; +static AsyncWebServer server(80); + +class CaptiveRequestHandler : public AsyncWebHandler { +public: + bool canHandle(__unused AsyncWebServerRequest *request) const override { + return true; + } + + void handleRequest(AsyncWebServerRequest *request) { + AsyncResponseStream *response = request->beginResponseStream("text/html"); + response->print("Captive Portal"); + response->print("

This is our captive portal front page.

"); + response->printf("

You were trying to reach: http://%s%s

", request->host().c_str(), request->url().c_str()); +#ifndef CONFIG_IDF_TARGET_ESP32H2 + response->printf("

Try opening this link instead

", WiFi.softAPIP().toString().c_str()); +#endif + response->print(""); + request->send(response); + } +}; + +void setup() { + Serial.begin(115200); + Serial.println(); + Serial.println("Configuring access point..."); + +#ifndef CONFIG_IDF_TARGET_ESP32H2 + if (!WiFi.softAP("esp-captive")) { + Serial.println("Soft AP creation failed."); + while (1); + } + + dnsServer.start(53, "*", WiFi.softAPIP()); +#endif + + server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER); // only when requested from AP + // more handlers... + server.begin(); +} + +void loop() { + dnsServer.processNextRequest(); +} -- cgit v1.3.1