ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
captive_portal.h
Go to the documentation of this file.
1#pragma once
3#ifdef USE_CAPTIVE_PORTAL
4#include <memory>
5#if defined(USE_ESP32)
7#elif defined(USE_ARDUINO)
8#include <DNSServer.h>
9#endif
14
15namespace esphome {
16
17namespace captive_portal {
18
19class CaptivePortal : public AsyncWebHandler, public Component {
20 public:
22 void setup() override;
23 void dump_config() override;
24 void loop() override {
25#if defined(USE_ESP32)
26 if (this->dns_server_ != nullptr) {
27 this->dns_server_->process_next_request();
28 }
29#elif defined(USE_ARDUINO)
30 if (this->dns_server_ != nullptr) {
31 this->dns_server_->processNextRequest();
32 }
33#endif
34 }
35 float get_setup_priority() const override;
36 void start();
37 bool is_active() const { return this->active_; }
38 void end() {
39 this->active_ = false;
40 this->disable_loop(); // Stop processing DNS requests
41 this->base_->deinit();
42 if (this->dns_server_ != nullptr) {
43 this->dns_server_->stop();
44 this->dns_server_ = nullptr;
45 }
46 }
47
48 bool canHandle(AsyncWebServerRequest *request) const override {
49 // Handle all GET requests when captive portal is active
50 // This allows us to respond with the portal page for any URL,
51 // triggering OS captive portal detection
52 return this->active_ && request->method() == HTTP_GET;
53 }
54
55 void handle_config(AsyncWebServerRequest *request);
56
57 void handle_wifisave(AsyncWebServerRequest *request);
58
59 void handleRequest(AsyncWebServerRequest *req) override;
60
61 protected:
63 bool initialized_{false};
64 bool active_{false};
65#if defined(USE_ARDUINO) || defined(USE_ESP32)
66 std::unique_ptr<DNSServer> dns_server_{nullptr};
67#endif
68};
69
70extern CaptivePortal *global_captive_portal; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
71
72} // namespace captive_portal
73} // namespace esphome
74#endif
void disable_loop()
Disable this component's loop.
bool canHandle(AsyncWebServerRequest *request) const override
std::unique_ptr< DNSServer > dns_server_
CaptivePortal(web_server_base::WebServerBase *base)
void handle_config(AsyncWebServerRequest *request)
web_server_base::WebServerBase * base_
void handleRequest(AsyncWebServerRequest *req) override
void handle_wifisave(AsyncWebServerRequest *request)
CaptivePortal * global_captive_portal
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7