ESPHome 2025.10.0-dev
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(F("application/json"));
15 stream->addHeader(F("cache-control"), F("public, max-age=0, must-revalidate"));
16#ifdef USE_ESP8266
17 stream->print(F("{\"mac\":\""));
18 stream->print(get_mac_address_pretty().c_str());
19 stream->print(F("\",\"name\":\""));
20 stream->print(App.get_name().c_str());
21 stream->print(F("\",\"aps\":[{}"));
22#else
23 stream->printf(R"({"mac":"%s","name":"%s","aps":[{})", get_mac_address_pretty().c_str(), App.get_name().c_str());
24#endif
25
26 for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
27 if (scan.get_is_hidden())
28 continue;
29
30 // Assumes no " in ssid, possible unicode isses?
31#ifdef USE_ESP8266
32 stream->print(F(",{\"ssid\":\""));
33 stream->print(scan.get_ssid().c_str());
34 stream->print(F("\",\"rssi\":"));
35 stream->print(scan.get_rssi());
36 stream->print(F(",\"lock\":"));
37 stream->print(scan.get_with_auth());
38 stream->print(F("}"));
39#else
40 stream->printf(R"(,{"ssid":"%s","rssi":%d,"lock":%d})", scan.get_ssid().c_str(), scan.get_rssi(),
41 scan.get_with_auth());
42#endif
43 }
44 stream->print(F("]}"));
45 request->send(stream);
46}
47void CaptivePortal::handle_wifisave(AsyncWebServerRequest *request) {
48 std::string ssid = request->arg("ssid").c_str(); // NOLINT(readability-redundant-string-cstr)
49 std::string psk = request->arg("psk").c_str(); // NOLINT(readability-redundant-string-cstr)
50 ESP_LOGI(TAG, "Requested WiFi Settings Change:");
51 ESP_LOGI(TAG, " SSID='%s'", ssid.c_str());
52 ESP_LOGI(TAG, " Password=" LOG_SECRET("'%s'"), psk.c_str());
55 request->redirect(F("/?save"));
56}
57
59 // Disable loop by default - will be enabled when captive portal starts
60 this->disable_loop();
61}
63 this->base_->init();
64 if (!this->initialized_) {
65 this->base_->add_handler(this);
66 }
67
69
70#ifdef USE_ESP_IDF
71 // Create DNS server instance for ESP-IDF
72 this->dns_server_ = make_unique<DNSServer>();
73 this->dns_server_->start(ip);
74#endif
75#ifdef USE_ARDUINO
76 this->dns_server_ = make_unique<DNSServer>();
77 this->dns_server_->setErrorReplyCode(DNSReplyCode::NoError);
78 this->dns_server_->start(53, F("*"), ip);
79#endif
80
81 this->initialized_ = true;
82 this->active_ = true;
83
84 // Enable loop() now that captive portal is active
85 this->enable_loop();
86
87 ESP_LOGV(TAG, "Captive portal started");
88}
89
90void CaptivePortal::handleRequest(AsyncWebServerRequest *req) {
91 if (req->url() == F("/config.json")) {
92 this->handle_config(req);
93 return;
94 } else if (req->url() == F("/wifisave")) {
95 this->handle_wifisave(req);
96 return;
97 }
98
99 // All other requests get the captive portal page
100 // This includes OS captive portal detection endpoints which will trigger
101 // the captive portal when they don't receive their expected responses
102#ifndef USE_ESP8266
103 auto *response = req->beginResponse(200, F("text/html"), INDEX_GZ, sizeof(INDEX_GZ));
104#else
105 auto *response = req->beginResponse_P(200, F("text/html"), INDEX_GZ, sizeof(INDEX_GZ));
106#endif
107 response->addHeader(F("Content-Encoding"), F("gzip"));
108 req->send(response);
109}
110
113 // Before WiFi
114 return setup_priority::WIFI + 1.0f;
115}
116void CaptivePortal::dump_config() { ESP_LOGCONFIG(TAG, "Captive Portal:"); }
117
118CaptivePortal *global_captive_portal = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
119
120} // namespace captive_portal
121} // namespace esphome
122#endif
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.
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
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition helpers.cpp:598
Application App
Global storage of Application pointer - only one Application can exist.