ESPHome 2025.9.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#ifdef USE_ARDUINO
6#include <DNSServer.h>
7#endif
12
13namespace esphome {
14
15namespace captive_portal {
16
17class CaptivePortal : public AsyncWebHandler, public Component {
18 public:
20 void setup() override;
21 void dump_config() override;
22#ifdef USE_ARDUINO
23 void loop() override {
24 if (this->dns_server_ != nullptr) {
25 this->dns_server_->processNextRequest();
26 } else {
27 this->disable_loop();
28 }
29 }
30#endif
31 float get_setup_priority() const override;
32 void start();
33 bool is_active() const { return this->active_; }
34 void end() {
35 this->active_ = false;
36 this->base_->deinit();
37#ifdef USE_ARDUINO
38 this->dns_server_->stop();
39 this->dns_server_ = nullptr;
40#endif
41 }
42
43 bool canHandle(AsyncWebServerRequest *request) const override {
44 if (!this->active_)
45 return false;
46
47 if (request->method() == HTTP_GET) {
48 if (request->url() == "/")
49 return true;
50 if (request->url() == "/config.json")
51 return true;
52 if (request->url() == "/wifisave")
53 return true;
54 }
55
56 return false;
57 }
58
59 void handle_config(AsyncWebServerRequest *request);
60
61 void handle_wifisave(AsyncWebServerRequest *request);
62
63 void handleRequest(AsyncWebServerRequest *req) override;
64
65 protected:
67 bool initialized_{false};
68 bool active_{false};
69#ifdef USE_ARDUINO
70 std::unique_ptr<DNSServer> dns_server_{nullptr};
71#endif
72};
73
74extern CaptivePortal *global_captive_portal; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
75
76} // namespace captive_portal
77} // namespace esphome
78#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