ESPHome 2026.1.1
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 char mac_s[18];
17 const char *mac_str = get_mac_address_pretty_into_buffer(mac_s);
18#ifdef USE_ESP8266
19 stream->print(ESPHOME_F("{\"mac\":\""));
20 stream->print(mac_str);
21 stream->print(ESPHOME_F("\",\"name\":\""));
22 stream->print(App.get_name().c_str());
23 stream->print(ESPHOME_F("\",\"aps\":[{}"));
24#else
25 stream->printf(R"({"mac":"%s","name":"%s","aps":[{})", mac_str, App.get_name().c_str());
26#endif
27
28 for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
29 if (scan.get_is_hidden())
30 continue;
31
32 // Assumes no " in ssid, possible unicode isses?
33#ifdef USE_ESP8266
34 stream->print(ESPHOME_F(",{\"ssid\":\""));
35 stream->print(scan.get_ssid().c_str());
36 stream->print(ESPHOME_F("\",\"rssi\":"));
37 stream->print(scan.get_rssi());
38 stream->print(ESPHOME_F(",\"lock\":"));
39 stream->print(scan.get_with_auth());
40 stream->print(ESPHOME_F("}"));
41#else
42 stream->printf(R"(,{"ssid":"%s","rssi":%d,"lock":%d})", scan.get_ssid().c_str(), scan.get_rssi(),
43 scan.get_with_auth());
44#endif
45 }
46 stream->print(ESPHOME_F("]}"));
47 request->send(stream);
48}
49void CaptivePortal::handle_wifisave(AsyncWebServerRequest *request) {
50 std::string ssid = request->arg("ssid").c_str(); // NOLINT(readability-redundant-string-cstr)
51 std::string psk = request->arg("psk").c_str(); // NOLINT(readability-redundant-string-cstr)
52 ESP_LOGI(TAG,
53 "Requested WiFi Settings Change:\n"
54 " SSID='%s'\n"
55 " Password=" LOG_SECRET("'%s'"),
56 ssid.c_str(), psk.c_str());
57#ifdef USE_ESP8266
58 // ESP8266 is single-threaded, call directly
60#else
61 // Defer save to main loop thread to avoid NVS operations from HTTP thread
62 this->defer([ssid, psk]() { wifi::global_wifi_component->save_wifi_sta(ssid, psk); });
63#endif
64 request->redirect(ESPHOME_F("/?save"));
65}
66
68 // Disable loop by default - will be enabled when captive portal starts
69 this->disable_loop();
70}
72 this->base_->init();
73 if (!this->initialized_) {
74 this->base_->add_handler(this);
75 }
76
78
79#if defined(USE_ESP32)
80 // Create DNS server instance for ESP-IDF
81 this->dns_server_ = make_unique<DNSServer>();
82 this->dns_server_->start(ip);
83#elif defined(USE_ARDUINO)
84 this->dns_server_ = make_unique<DNSServer>();
85 this->dns_server_->setErrorReplyCode(DNSReplyCode::NoError);
86 this->dns_server_->start(53, ESPHOME_F("*"), ip);
87#endif
88
89 this->initialized_ = true;
90 this->active_ = true;
91
92 // Enable loop() now that captive portal is active
93 this->enable_loop();
94
95 ESP_LOGV(TAG, "Captive portal started");
96}
97
98void CaptivePortal::handleRequest(AsyncWebServerRequest *req) {
99 if (req->url() == ESPHOME_F("/config.json")) {
100 this->handle_config(req);
101 return;
102 } else if (req->url() == ESPHOME_F("/wifisave")) {
103 this->handle_wifisave(req);
104 return;
105 }
106
107 // All other requests get the captive portal page
108 // This includes OS captive portal detection endpoints which will trigger
109 // the captive portal when they don't receive their expected responses
110#ifndef USE_ESP8266
111 auto *response = req->beginResponse(200, ESPHOME_F("text/html"), INDEX_GZ, sizeof(INDEX_GZ));
112#else
113 auto *response = req->beginResponse_P(200, ESPHOME_F("text/html"), INDEX_GZ, sizeof(INDEX_GZ));
114#endif
115#ifdef USE_CAPTIVE_PORTAL_GZIP
116 response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("gzip"));
117#else
118 response->addHeader(ESPHOME_F("Content-Encoding"), ESPHOME_F("br"));
119#endif
120 req->send(response);
121}
122
125 // Before WiFi
126 return setup_priority::WIFI + 1.0f;
127}
128void CaptivePortal::dump_config() { ESP_LOGCONFIG(TAG, "Captive Portal:"); }
129
130CaptivePortal *global_captive_portal = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
131
132} // namespace captive_portal
133} // namespace esphome
134#endif
const std::string & get_name() const
Get the name of this Application set by pre_setup().
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:492
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
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)
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
const char * get_mac_address_pretty_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in colon-separated uppercase hex notation.
Definition helpers.cpp:737
Application App
Global storage of Application pointer - only one Application can exist.