ESPHome  2024.10.2
captive_portal.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/defines.h"
3 #ifdef USE_CAPTIVE_PORTAL
4 #include <memory>
5 #ifdef USE_ARDUINO
6 #include <DNSServer.h>
7 #endif
9 #include "esphome/core/helpers.h"
12 
13 namespace esphome {
14 
15 namespace captive_portal {
16 
17 class 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 
71 extern CaptivePortal *global_captive_portal; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
72 
73 } // namespace captive_portal
74 } // namespace esphome
75 #endif
CaptivePortal(web_server_base::WebServerBase *base)
std::unique_ptr< DNSServer > dns_server_
void handleRequest(AsyncWebServerRequest *req) override
web_server_base::WebServerBase * base_
bool canHandle(AsyncWebServerRequest *request) override
CaptivePortal * global_captive_portal
void handle_config(AsyncWebServerRequest *request)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
float get_setup_priority() const override
void handle_wifisave(AsyncWebServerRequest *request)