ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
captive_portal.h
Go to the documentation of this file.
1#pragma once
3#ifdef USE_CAPTIVE_PORTAL
9
11
12class CaptivePortal final : public AsyncWebHandler, public Component {
13 public:
15 void setup() override;
16 void dump_config() override;
17 void loop() override { this->dns_.loop(); }
18 float get_setup_priority() const override;
19 void start();
20 bool is_active() const { return this->active_; }
21 void end() {
22 this->active_ = false;
23 this->disable_loop(); // Stop processing DNS requests
24 this->base_->deinit();
25 this->dns_.stop();
26 }
27
28 bool canHandle(AsyncWebServerRequest *request) const override {
29 // Handle all GET requests when captive portal is active
30 // This allows us to respond with the portal page for any URL,
31 // triggering OS captive portal detection
32 return this->active_ && request->method() == HTTP_GET;
33 }
34
35 void handle_config(AsyncWebServerRequest *request);
36
37 void handle_wifisave(AsyncWebServerRequest *request);
38
39 void handleRequest(AsyncWebServerRequest *req) override;
40
41 protected:
43 bool initialized_{false};
44 bool active_{false};
46};
47
48extern CaptivePortal *global_captive_portal; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
49
50} // namespace esphome::captive_portal
51
52#endif
void disable_loop()
Disable this component's loop.
bool canHandle(AsyncWebServerRequest *request) const override
web_server_base::CaptiveDNS dns_
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)
void loop()
Answer one pending query; call from the owner's loop() while running.
Definition captive_dns.h:46
CaptivePortal * global_captive_portal