#include "captive_portal.h" #include "esphome/core/log.h" #include "esphome/core/application.h" #include "esphome/components/wifi/wifi_component.h" namespace esphome { namespace captive_portal { static const char *TAG = "captive_portal"; void CaptivePortal::handle_index(AsyncWebServerRequest *request) { AsyncResponseStream *stream = request->beginResponseStream("text/html"); stream->print(F("")); stream->print(App.get_name().c_str()); stream->print(F("")); stream->print(F("")); stream->print(F("")); stream->print(F("

WiFi Networks

")); if (request->hasArg("save")) { stream->print(F("
The ESP will now try to connect to the network...
Please give it some " "time to connect.
Note: Copy the changed network to your YAML file - the next OTA update will " "overwrite these settings.
")); } for (auto &scan : wifi::global_wifi_component->get_scan_result()) { if (scan.get_is_hidden()) continue; stream->print(F("
")); if (scan.get_rssi() >= -50) { stream->print(F("")); } else if (scan.get_rssi() >= -65) { stream->print(F("")); } else if (scan.get_rssi() >= -85) { stream->print(F("")); } else { stream->print(F("")); } stream->print(F("")); stream->print(scan.get_ssid().c_str()); stream->print(F("")); if (scan.get_with_auth()) { stream->print(F("")); } stream->print(F("
")); } stream->print(F("

WiFi Settings







")); stream->print(F("

OTA Update

")); stream->print(F("
")); request->send(stream); } void CaptivePortal::handle_wifisave(AsyncWebServerRequest *request) { std::string ssid = request->arg("ssid").c_str(); std::string psk = request->arg("psk").c_str(); ESP_LOGI(TAG, "Captive Portal Requested WiFi Settings Change:"); ESP_LOGI(TAG, " SSID='%s'", ssid.c_str()); ESP_LOGI(TAG, " Password=" LOG_SECRET("'%s'"), psk.c_str()); wifi::global_wifi_component->save_wifi_sta(ssid, psk); request->redirect("/?save=true"); } void CaptivePortal::setup() {} void CaptivePortal::start() { this->base_->init(); if (!this->initialized_) { this->base_->add_handler(this); this->base_->add_ota_handler(); } this->dns_server_ = new DNSServer(); this->dns_server_->setErrorReplyCode(DNSReplyCode::NoError); IPAddress ip = wifi::global_wifi_component->wifi_soft_ap_ip(); this->dns_server_->start(53, "*", ip); this->base_->get_server()->onNotFound([this](AsyncWebServerRequest *req) { bool not_found = false; if (!this->active_) { not_found = true; } else if (req->host() == wifi::global_wifi_component->wifi_soft_ap_ip().toString()) { not_found = true; } if (not_found) { req->send(404, "text/html", "File not found"); return; } auto url = "http://" + wifi::global_wifi_component->wifi_soft_ap_ip().toString(); req->redirect(url); }); this->initialized_ = true; this->active_ = true; } const char STYLESHEET_CSS[] PROGMEM = R"(*{box-sizing:inherit}div,input{padding:5px;font-size:1em}input{width:95%}body{text-align:center;font-family:sans-serif}button{border:0;border-radius:.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;padding:0}.main{text-align:left;display:inline-block;min-width:260px}.network{display:flex;justify-content:space-between;align-items:center}.network-left{display:flex;align-items:center}.network-ssid{margin-bottom:-7px;margin-left:10px}.info{border:1px solid;margin:10px 0;padding:15px 10px;color:#4f8a10;background-color:#dff2bf})"; const char LOCK_SVG[] PROGMEM = R"()"; void CaptivePortal::handleRequest(AsyncWebServerRequest *req) { if (req->url() == "/") { this->handle_index(req); return; } else if (req->url() == "/wifisave") { this->handle_wifisave(req); return; } else if (req->url() == "/stylesheet.css") { req->send_P(200, "text/css", STYLESHEET_CSS); return; } else if (req->url() == "/lock.svg") { req->send_P(200, "image/svg+xml", LOCK_SVG); return; } AsyncResponseStream *stream = req->beginResponseStream("image/svg+xml"); stream->print(F("url() == "/wifi-strength-4.svg") { stream->print(F("3z")); } else { if (req->url() == "/wifi-strength-1.svg") { stream->print(F("3m0 2c3.07 0 6.09.86 8.71 2.45l-5.1 6.36a8.43 8.43 0 0 0-7.22-.01L3.27 7.4")); } else if (req->url() == "/wifi-strength-2.svg") { stream->print(F("3m0 2c3.07 0 6.09.86 8.71 2.45l-3.21 3.98a11.32 11.32 0 0 0-11 0L3.27 7.4")); } else if (req->url() == "/wifi-strength-3.svg") { stream->print(F("3m0 2c3.07 0 6.09.86 8.71 2.45l-1.94 2.43A13.6 13.6 0 0 0 12 8C9 8 6.68 9 5.21 9.84l-1.94-2.")); } stream->print(F("4A16.94 16.94 0 0 1 12 5z")); } stream->print(F("\"/>")); req->send(stream); } CaptivePortal::CaptivePortal(web_server_base::WebServerBase *base) : base_(base) { global_captive_portal = this; } float CaptivePortal::get_setup_priority() const { // Before WiFi return setup_priority::WIFI + 1.0f; } void CaptivePortal::dump_config() { ESP_LOGCONFIG(TAG, "Captive Portal:"); } CaptivePortal *global_captive_portal = nullptr; } // namespace captive_portal } // namespace esphome