ESPHome  2024.5.4
captive_portal.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #ifdef USE_ARDUINO
5 #include <DNSServer.h>
6 #endif
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 #ifdef USE_ARDUINO
22  void loop() override {
23  if (this->dns_server_ != nullptr)
24  this->dns_server_->processNextRequest();
25  }
26 #endif
27  float get_setup_priority() const override;
28  void start();
29  bool is_active() const { return this->active_; }
30  void end() {
31  this->active_ = false;
32  this->base_->deinit();
33 #ifdef USE_ARDUINO
34  this->dns_server_->stop();
35  this->dns_server_ = nullptr;
36 #endif
37  }
38 
39  bool canHandle(AsyncWebServerRequest *request) override {
40  if (!this->active_)
41  return false;
42 
43  if (request->method() == HTTP_GET) {
44  if (request->url() == "/")
45  return true;
46  if (request->url() == "/config.json")
47  return true;
48  if (request->url() == "/wifisave")
49  return true;
50  }
51 
52  return false;
53  }
54 
55  void handle_config(AsyncWebServerRequest *request);
56 
57  void handle_wifisave(AsyncWebServerRequest *request);
58 
59  void handleRequest(AsyncWebServerRequest *req) override;
60 
61  protected:
63  bool initialized_{false};
64  bool active_{false};
65 #ifdef USE_ARDUINO
66  std::unique_ptr<DNSServer> dns_server_{nullptr};
67 #endif
68 };
69 
70 extern CaptivePortal *global_captive_portal; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
71 
72 } // namespace captive_portal
73 } // namespace esphome
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)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
float get_setup_priority() const override
void handle_wifisave(AsyncWebServerRequest *request)