1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-07 05:42:20 +01:00

[captive_portal] ESP8266: Move strings to PROGMEM (saves 192 bytes RAM) (#10600)

This commit is contained in:
J. Nick Koston
2025-09-05 18:02:12 -05:00
committed by GitHub
parent b74463c3e6
commit 694c590eb6
2 changed files with 38 additions and 15 deletions

View File

@@ -11,17 +11,35 @@ namespace captive_portal {
static const char *const TAG = "captive_portal";
void CaptivePortal::handle_config(AsyncWebServerRequest *request) {
AsyncResponseStream *stream = request->beginResponseStream("application/json");
stream->addHeader("cache-control", "public, max-age=0, must-revalidate");
AsyncResponseStream *stream = request->beginResponseStream(F("application/json"));
stream->addHeader(F("cache-control"), F("public, max-age=0, must-revalidate"));
#ifdef USE_ESP8266
stream->print(F("{\"mac\":\""));
stream->print(get_mac_address_pretty().c_str());
stream->print(F("\",\"name\":\""));
stream->print(App.get_name().c_str());
stream->print(F("\",\"aps\":[{}"));
#else
stream->printf(R"({"mac":"%s","name":"%s","aps":[{})", get_mac_address_pretty().c_str(), App.get_name().c_str());
#endif
for (auto &scan : wifi::global_wifi_component->get_scan_result()) {
if (scan.get_is_hidden())
continue;
// Assumes no " in ssid, possible unicode isses?
// Assumes no " in ssid, possible unicode isses?
#ifdef USE_ESP8266
stream->print(F(",{\"ssid\":\""));
stream->print(scan.get_ssid().c_str());
stream->print(F("\",\"rssi\":"));
stream->print(scan.get_rssi());
stream->print(F(",\"lock\":"));
stream->print(scan.get_with_auth());
stream->print(F("}"));
#else
stream->printf(R"(,{"ssid":"%s","rssi":%d,"lock":%d})", scan.get_ssid().c_str(), scan.get_rssi(),
scan.get_with_auth());
#endif
}
stream->print(F("]}"));
request->send(stream);
@@ -34,7 +52,7 @@ void CaptivePortal::handle_wifisave(AsyncWebServerRequest *request) {
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");
request->redirect(F("/?save"));
}
void CaptivePortal::setup() {
@@ -53,18 +71,23 @@ void CaptivePortal::start() {
this->dns_server_ = make_unique<DNSServer>();
this->dns_server_->setErrorReplyCode(DNSReplyCode::NoError);
network::IPAddress ip = wifi::global_wifi_component->wifi_soft_ap_ip();
this->dns_server_->start(53, "*", ip);
this->dns_server_->start(53, F("*"), ip);
// Re-enable loop() when DNS server is started
this->enable_loop();
#endif
this->base_->get_server()->onNotFound([this](AsyncWebServerRequest *req) {
if (!this->active_ || req->host().c_str() == wifi::global_wifi_component->wifi_soft_ap_ip().str()) {
req->send(404, "text/html", "File not found");
req->send(404, F("text/html"), F("File not found"));
return;
}
#ifdef USE_ESP8266
String url = F("http://");
url += wifi::global_wifi_component->wifi_soft_ap_ip().str().c_str();
#else
auto url = "http://" + wifi::global_wifi_component->wifi_soft_ap_ip().str();
#endif
req->redirect(url.c_str());
});
@@ -73,19 +96,19 @@ void CaptivePortal::start() {
}
void CaptivePortal::handleRequest(AsyncWebServerRequest *req) {
if (req->url() == "/") {
if (req->url() == F("/")) {
#ifndef USE_ESP8266
auto *response = req->beginResponse(200, "text/html", INDEX_GZ, sizeof(INDEX_GZ));
auto *response = req->beginResponse(200, F("text/html"), INDEX_GZ, sizeof(INDEX_GZ));
#else
auto *response = req->beginResponse_P(200, "text/html", INDEX_GZ, sizeof(INDEX_GZ));
auto *response = req->beginResponse_P(200, F("text/html"), INDEX_GZ, sizeof(INDEX_GZ));
#endif
response->addHeader("Content-Encoding", "gzip");
response->addHeader(F("Content-Encoding"), F("gzip"));
req->send(response);
return;
} else if (req->url() == "/config.json") {
} else if (req->url() == F("/config.json")) {
this->handle_config(req);
return;
} else if (req->url() == "/wifisave") {
} else if (req->url() == F("/wifisave")) {
this->handle_wifisave(req);
return;
}

View File

@@ -45,11 +45,11 @@ class CaptivePortal : public AsyncWebHandler, public Component {
return false;
if (request->method() == HTTP_GET) {
if (request->url() == "/")
if (request->url() == F("/"))
return true;
if (request->url() == "/config.json")
if (request->url() == F("/config.json"))
return true;
if (request->url() == "/wifisave")
if (request->url() == F("/wifisave"))
return true;
}