ESPHome 2025.11.0b4
Loading...
Searching...
No Matches
captive_portal.cpp
Go to the documentation of this file.
1#include "captive_portal.h"
2#ifdef USE_CAPTIVE_PORTAL
3#include "esphome/core/log.h"
6#include "captive_index.h"
7
8namespace esphome {
9namespace captive_portal {
10
11static const char *const TAG = "captive_portal";
12
13void CaptivePortal::handle_config(AsyncWebServerRequest *request) {
14 AsyncResponseStream *stream = request->beginResponseStream(ESPHOME_F("application/json"));
15 stream->addHeader(ESPHOME_F("cache-control"), ESPHOME_F("public, max-age=0, must-revalidate"));
16#ifdef USE_ESP8266
17 stream->print(ESPHOME_F("{\"mac\":\""));
18 stream->print(get_mac_address_pretty().c_str());
19 stream->print(ESPHOME_F("\",\"name\":\""));
20 stream->print(App.get_name().c_str());
21 stream->print(ESPHOME_F("\",\"aps\":[{}"));
22#else
23 stream->printf(R"({"mac":"%s","name":"%s","aps":[{})", get_mac_address_pretty().c_str(), App.get_name().c_str());
24#endif
25
26 for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
27 if (scan.get_is_hidden())
28 continue;
29
30 // Assumes no " in ssid, possible unicode isses?
31#ifdef USE_ESP8266
32 stream->print(ESPHOME_F(",{\"ssid\":\""));
33 stream->print(scan.get_ssid().c_str());
34 stream->print(ESPHOME_F("\",\"rssi\":"));
35 stream->print(scan.get_rssi());
36 stream->print(ESPHOME_F(",\"lock\":"));
37 stream->print(scan.get_with_auth());
38 stream->print(ESPHOME_F("}"));
39#else
40 stream->printf(R"(,{"ssid":"%s","rssi":%d,"lock":%d})", scan.get_ssid().c_str(), scan.get_rssi(),
41 scan.get_with_auth());
42#endif
43 }
44 stream->print(ESPHOME_F("]}"));
45 request->send(stream);
46}
47void CaptivePortal::handle_wifisave(AsyncWebServerRequest *request) {
48 std::string ssid = request->arg("ssid").c_str(); // NOLINT(readability-redundant-string-cstr)
49 std::string psk = request->arg("psk").c_str(); // NOLINT(readability-redundant-string-cstr)
50 ESP_LOGI(TAG, "Requested WiFi Settings Change:");
51 ESP_LOGI(TAG, " SSID='%s'", ssid.c_str());
52 ESP_LOGI(TAG, " Password=" LOG_SECRET("'%s'"), psk.c_str());
53 // Defer save to main loop thread to avoid NVS operations from HTTP thread
54 this->defer([ssid, psk]() { wifi::global_wifi_component->save_wifi_sta(ssid, psk); });
55 request->redirect(ESPHOME_F("/?save"));
56}
57
59 // Disable loop by default - will be enabled when captive portal starts
60 this->disable_loop();
61}
63 this->base_->init();
64 if (!this->initialized_) {
65 this->base_->add_handler(this);
66#ifdef USE_ESP32
67 // Enable LRU socket purging to handle captive portal detection probe bursts
68 // OS captive portal detection makes many simultaneous HTTP requests which can
69 // exhaust sockets. LRU purging automatically closes oldest idle connections.
70 this->base_->get_server()->set_lru_purge_enable(true);
71#endif
72 }
73
75
76#ifdef USE_ESP_IDF
77 // Create DNS server instance for ESP-IDF
78 this->dns_server_ = make_unique<DNSServer>();
79 this->dns_server_->start(ip);
80#endif
81#ifdef USE_ARDUINO
82 this->dns_server_ = make_unique<DNSServer>();
83 this->dns_server_->setErrorReplyCode(DNSReplyCode::NoError);
84 this->dns_server_->start(53, ESPHOME_F("*"), ip);
85#endif
86
87 this->initialized_ = true;
88 this->active_ = true;
89
90 // Enable loop() now that captive portal is active
91 this->enable_loop();
92
93 ESP_LOGV(TAG, "Captive portal started");
94}
95
96void CaptivePortal::handleRequest(AsyncWebServerRequest *req) {
97 if (req->url() == ESPHOME_F("/config.json")) {
98 this->handle_config(req);
99 return;
100 } else if (req->url() == ESPHOME_F("/wifisave")) {
101 this->handle_wifisave(req);
102 return;
103 }
104
105 // All other requests get the captive portal page
106 // This includes OS captive portal detection endpoints which will trigger
107 // the captive portal when they don't receive their expected responses
108#ifndef USE_ESP8266
109 auto *response = req->beginResponse(200, ESPHOME_F("text/html"), INDEX_GZ, sizeof(INDEX_GZ));
110#else
111 auto *response = req->beginResponse_P(200, ESPHOME_F("text/html"), INDEX_GZ, sizeof(INDEX_GZ));
112#endif
113 response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("gzip"));
114 req->send(response);
115}
116
119 // Before WiFi
120 return setup_priority::WIFI + 1.0f;
121}
122void CaptivePortal::dump_config() { ESP_LOGCONFIG(TAG, "Captive Portal:"); }
123
124CaptivePortal *global_captive_portal = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
125
126} // namespace captive_portal
127} // namespace esphome
128#endif
const std::string & get_name() const
Get the name of this Application set by pre_setup().
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
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)
std::shared_ptr< AsyncWebServer > get_server() const
void add_handler(AsyncWebHandler *handler)
void save_wifi_sta(const std::string &ssid, const std::string &password)
CaptivePortal * global_captive_portal
WiFiComponent * global_wifi_component
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition helpers.cpp:640
Application App
Global storage of Application pointer - only one Application can exist.