ESPHome 2025.5.0
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 }
27#endif
28 float get_setup_priority() const override;
29 void start();
30 bool is_active() const { return this->active_; }
31 void end() {
32 this->active_ = false;
33 this->base_->deinit();
34#ifdef USE_ARDUINO
35 this->dns_server_->stop();
36 this->dns_server_ = nullptr;
37#endif
38 }
39
40 bool canHandle(AsyncWebServerRequest *request) override {
41 if (!this->active_)
42 return false;
43
44 if (request->method() == HTTP_GET) {
45 if (request->url() == "/")
46 return true;
47 if (request->url() == "/config.json")
48 return true;
49 if (request->url() == "/wifisave")
50 return true;
51 }
52
53 return false;
54 }
55
56 void handle_config(AsyncWebServerRequest *request);
57
58 void handle_wifisave(AsyncWebServerRequest *request);
59
60 void handleRequest(AsyncWebServerRequest *req) override;
61
62 protected:
64 bool initialized_{false};
65 bool active_{false};
66#ifdef USE_ARDUINO
67 std::unique_ptr<DNSServer> dns_server_{nullptr};
68#endif
69};
70
71extern CaptivePortal *global_captive_portal; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
72
73} // namespace captive_portal
74} // namespace esphome
75#endif
std::unique_ptr< DNSServer > dns_server_
bool canHandle(AsyncWebServerRequest *request) override
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