#ifdef USE_ARDUINO
#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 *const TAG = "captive_portal";
void CaptivePortal::handle_index(AsyncWebServerRequest *request) {
AsyncResponseStream *stream = request->beginResponseStream("text/html");
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("
"));
}
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);
wifi::global_wifi_component->start_scanning();
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_ = make_unique