2#ifdef USE_CAPTIVE_PORTAL
9namespace captive_portal {
11static const char *
const TAG =
"captive_portal";
14 AsyncResponseStream *stream = request->beginResponseStream(ESPHOME_F(
"application/json"));
15 stream->addHeader(ESPHOME_F(
"cache-control"), ESPHOME_F(
"public, max-age=0, must-revalidate"));
19 stream->print(ESPHOME_F(
"{\"mac\":\""));
20 stream->print(mac_str);
21 stream->print(ESPHOME_F(
"\",\"name\":\""));
23 stream->print(ESPHOME_F(
"\",\"aps\":[{}"));
25 stream->printf(R
"({"mac":"%s","name":"%s","aps":[{})", mac_str, App.get_name().c_str());
29 if (scan.get_is_hidden())
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(
"}"));
42 stream->printf(R
"(,{"ssid":"%s","rssi":%d,"lock":%d})", scan.get_ssid().c_str(), scan.get_rssi(),
43 scan.get_with_auth());
46 stream->print(ESPHOME_F(
"]}"));
47 request->send(stream);
50 std::string ssid = request->arg(
"ssid").c_str();
51 std::string psk = request->arg(
"psk").c_str();
53 "Requested WiFi Settings Change:\n"
55 " Password=" LOG_SECRET(
"'%s'"),
56 ssid.c_str(), psk.c_str());
64 request->redirect(ESPHOME_F(
"/?save"));
83#elif defined(USE_ARDUINO)
85 this->
dns_server_->setErrorReplyCode(DNSReplyCode::NoError);
95 ESP_LOGV(TAG,
"Captive portal started");
99 if (req->url() == ESPHOME_F(
"/config.json")) {
102 }
else if (req->url() == ESPHOME_F(
"/wifisave")) {
111 auto *response = req->beginResponse(200, ESPHOME_F(
"text/html"), INDEX_GZ,
sizeof(INDEX_GZ));
113 auto *response = req->beginResponse_P(200, ESPHOME_F(
"text/html"), INDEX_GZ,
sizeof(INDEX_GZ));
115#ifdef USE_CAPTIVE_PORTAL_GZIP
116 response->addHeader(ESPHOME_F(
"Content-Encoding"), ESPHOME_F(
"gzip"));
118 response->addHeader(ESPHOME_F(
"Content-Encoding"), ESPHOME_F(
"br"));
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.
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
float get_setup_priority() const override
std::unique_ptr< DNSServer > dns_server_
void dump_config() override
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)
network::IPAddress wifi_soft_ap_ip()
CaptivePortal * global_captive_portal
WiFiComponent * global_wifi_component
Providing packet encoding functions for exchanging data with a remote host.
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.
Application App
Global storage of Application pointer - only one Application can exist.