2#ifdef USE_CAPTIVE_PORTAL
10static const char *
const TAG =
"captive_portal";
13 AsyncResponseStream *stream = request->beginResponseStream(ESPHOME_F(
"application/json"));
14 stream->addHeader(ESPHOME_F(
"cache-control"), ESPHOME_F(
"public, max-age=0, must-revalidate"));
18 stream->print(ESPHOME_F(
"{\"mac\":\""));
19 stream->print(mac_str);
20 stream->print(ESPHOME_F(
"\",\"name\":\""));
22 stream->print(ESPHOME_F(
"\",\"aps\":[{}"));
24 stream->printf(R
"({"mac":"%s","name":"%s","aps":[{})", mac_str, App.get_name().c_str());
28 if (scan.get_is_hidden())
33 stream->print(ESPHOME_F(
",{\"ssid\":\""));
34 stream->print(scan.get_ssid().c_str());
35 stream->print(ESPHOME_F(
"\",\"rssi\":"));
36 stream->print(scan.get_rssi());
37 stream->print(ESPHOME_F(
",\"lock\":"));
38 stream->print(scan.get_with_auth());
39 stream->print(ESPHOME_F(
"}"));
41 stream->printf(R
"(,{"ssid":"%s","rssi":%d,"lock":%d})", scan.get_ssid().c_str(), scan.get_rssi(),
42 scan.get_with_auth());
45 stream->print(ESPHOME_F(
"]}"));
46 request->send(stream);
49 const auto &ssid = request->arg(
"ssid");
50 const auto &psk = request->arg(
"psk");
52 "Requested WiFi Settings Change:\n"
54 " Password=" LOG_SECRET(
"'%s'"),
55 ssid.c_str(), psk.c_str());
63 request->send(200, ESPHOME_F(
"text/plain"), ESPHOME_F(
"Saved. Connecting..."));
82#elif defined(USE_ARDUINO)
84 this->
dns_server_->setErrorReplyCode(DNSReplyCode::NoError);
94 ESP_LOGV(TAG,
"Captive portal started");
99 char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
102 const auto &url = req->url();
104 if (url == ESPHOME_F(
"/config.json")) {
107 }
else if (url == ESPHOME_F(
"/wifisave")) {
116 auto *response = req->beginResponse(200, ESPHOME_F(
"text/html"), INDEX_GZ,
sizeof(INDEX_GZ));
118 auto *response = req->beginResponse_P(200, ESPHOME_F(
"text/html"), INDEX_GZ,
sizeof(INDEX_GZ));
120#ifdef USE_CAPTIVE_PORTAL_GZIP
121 response->addHeader(ESPHOME_F(
"Content-Encoding"), ESPHOME_F(
"gzip"));
123 response->addHeader(ESPHOME_F(
"Content-Encoding"), ESPHOME_F(
"br"));
const StringRef & 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.
StringRef is a reference to a string owned by something else.
constexpr const char * c_str() const
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_without_auth(AsyncWebHandler *handler)
WARNING: Registers a handler that bypasses the USE_WEBSERVER_AUTH middleware.
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
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.