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();
52 ESP_LOGI(TAG,
"Requested WiFi Settings Change:");
53 ESP_LOGI(TAG,
" SSID='%s'", ssid.c_str());
54 ESP_LOGI(TAG,
" Password=" LOG_SECRET(
"'%s'"), psk.c_str());
57 request->redirect(ESPHOME_F(
"/?save"));
79 this->
dns_server_->setErrorReplyCode(DNSReplyCode::NoError);
89 ESP_LOGV(TAG,
"Captive portal started");
93 if (req->url() == ESPHOME_F(
"/config.json")) {
96 }
else if (req->url() == ESPHOME_F(
"/wifisave")) {
105 auto *response = req->beginResponse(200, ESPHOME_F(
"text/html"), INDEX_GZ,
sizeof(INDEX_GZ));
107 auto *response = req->beginResponse_P(200, ESPHOME_F(
"text/html"), INDEX_GZ,
sizeof(INDEX_GZ));
109 response->addHeader(ESPHOME_F(
"Content-Encoding"), ESPHOME_F(
"gzip"));
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.
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.