2#ifdef USE_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());
30 char escaped_ssid[32 * JSON_ESCAPE_MAX_EXPANSION + 1];
36 if (scan.get_is_hidden())
41 stream->print(ESPHOME_F(
",{\"ssid\":\""));
42 stream->print(escaped_ssid);
43 stream->print(ESPHOME_F(
"\",\"rssi\":"));
44 stream->print(scan.get_rssi());
45 stream->print(ESPHOME_F(
",\"lock\":"));
46 stream->print(scan.get_with_auth());
47 stream->print(ESPHOME_F(
"}"));
49 stream->printf(R
"(,{"ssid":"%s","rssi":%d,"lock":%d})", escaped_ssid, scan.get_rssi(), scan.get_with_auth());
53 stream->print(ESPHOME_F(
"]}"));
54 request->send(stream);
57 const auto &ssid = request->arg(
"ssid");
58 const auto &psk = request->arg(
"psk");
60 "Requested WiFi Settings Change:\n"
62 " Password=" LOG_SECRET(
"'%s'"),
63 ssid.c_str(), psk.c_str());
71 request->send(200, ESPHOME_F(
"text/plain"), ESPHOME_F(
"Saved. Connecting..."));
90#elif defined(USE_ARDUINO)
92 this->
dns_server_->setErrorReplyCode(DNSReplyCode::NoError);
102 ESP_LOGV(TAG,
"Captive portal started");
107 char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
110 const auto &url = req->url();
112 if (url == ESPHOME_F(
"/config.json")) {
115 }
else if (url == ESPHOME_F(
"/wifisave")) {
124 auto *response = req->beginResponse(200, ESPHOME_F(
"text/html"), INDEX_GZ,
sizeof(INDEX_GZ));
126 auto *response = req->beginResponse_P(200, ESPHOME_F(
"text/html"), INDEX_GZ,
sizeof(INDEX_GZ));
128#ifdef USE_CAPTIVE_PORTAL_GZIP
129 response->addHeader(ESPHOME_F(
"Content-Encoding"), ESPHOME_F(
"gzip"));
131 response->addHeader(ESPHOME_F(
"Content-Encoding"), ESPHOME_F(
"br"));
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
void enable_loop()
Enable this component's loop.
void defer(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call with a const char* name.
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.
Guards WiFiComponent::scan_result_.
void save_wifi_sta(const std::string &ssid, const std::string &password)
network::IPAddress wifi_soft_ap_ip()
const char * json_escape_into_buffer(std::span< char > buf, StringRef value)
Copy value into buf, escaping the characters that cannot appear raw inside a JSON string literal.
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.