ESPHome  2022.8.0
captive_portal.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ARDUINO
4 
5 #include <memory>
6 #include <DNSServer.h>
8 #include "esphome/core/helpers.h"
11 
12 namespace esphome {
13 
14 namespace captive_portal {
15 
16 class CaptivePortal : public AsyncWebHandler, public Component {
17  public:
19  void setup() override;
20  void dump_config() override;
21  void loop() override {
22  if (this->dns_server_ != nullptr)
23  this->dns_server_->processNextRequest();
24  }
25  float get_setup_priority() const override;
26  void start();
27  bool is_active() const { return this->active_; }
28  void end() {
29  this->active_ = false;
30  this->base_->deinit();
31  this->dns_server_->stop();
32  this->dns_server_ = nullptr;
33  }
34 
35  bool canHandle(AsyncWebServerRequest *request) override {
36  if (!this->active_)
37  return false;
38 
39  if (request->method() == HTTP_GET) {
40  if (request->url() == "/")
41  return true;
42  if (request->url() == "/config.json")
43  return true;
44  if (request->url() == "/wifisave")
45  return true;
46  }
47 
48  return false;
49  }
50 
51  void handle_config(AsyncWebServerRequest *request);
52 
53  void handle_wifisave(AsyncWebServerRequest *request);
54 
55  void handleRequest(AsyncWebServerRequest *req) override;
56 
57  protected:
59  bool initialized_{false};
60  bool active_{false};
61  std::unique_ptr<DNSServer> dns_server_{nullptr};
62 };
63 
64 extern CaptivePortal *global_captive_portal; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
65 
66 } // namespace captive_portal
67 } // namespace esphome
68 
69 #endif // USE_ARDUINO
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)
Definition: a4988.cpp:4
float get_setup_priority() const override
void handle_wifisave(AsyncWebServerRequest *request)