added rescan option

added reset option
added info page
precomposed page for A LOT faster page sending
This commit is contained in:
Alex Tudorache
2016-01-25 10:51:25 +02:00
parent 031b7c97bc
commit 6e65f39895
3 changed files with 85 additions and 29 deletions
+76 -26
View File
@@ -91,6 +91,8 @@ void WiFiManager::setupConfigPortal() {
server->on("/wifi", std::bind(&WiFiManager::handleWifi, this, true));
server->on("/0wifi", std::bind(&WiFiManager::handleWifi, this, false));
server->on("/wifisave", std::bind(&WiFiManager::handleWifiSave, this));
server->on("/i", std::bind(&WiFiManager::handleInfo, this));
server->on("/r", std::bind(&WiFiManager::handleReset, this));
server->on("/generate_204", std::bind(&WiFiManager::handle204, this)); //Android/Chrome OS captive portal check.
server->on("/fwlink", std::bind(&WiFiManager::handleRoot, this)); //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
server->onNotFound (std::bind(&WiFiManager::handleNotFound, this));
@@ -311,31 +313,20 @@ void WiFiManager::handleRoot() {
return;
}
server->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server->sendHeader("Pragma", "no-cache");
server->sendHeader("Expires", "-1");
server->setContentLength(CONTENT_LENGTH_UNKNOWN);
server->send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
String page = FPSTR(HTTP_HEAD);
page.replace("{v}", "Options");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
page += FPSTR(HTTP_HEAD_END);
page += "<h1>";
page += _apName;
page += "</h1>";
page += F("<h3>WiFiManager</h3>");
page += FPSTR(HTTP_PORTAL_OPTIONS);
page += FPSTR(HTTP_END);
server->send(200, "text/html", page);
String head = HTTP_HEAD;
head.replace("{v}", "Options");
server->sendContent(head);
server->sendContent_P(HTTP_SCRIPT);
server->sendContent_P(HTTP_STYLE);
server->sendContent_P(HTTP_HEAD_END);
//server->sendContent(F("<h1>"));
String title = "<h1>";
title += _apName;
title += "</h1>";
server->sendContent(title);
server->sendContent(F("<h3>WiFiManager</h3>"));
server->sendContent_P(HTTP_PORTAL_OPTIONS);
server->sendContent_P(HTTP_END);
server->client().stop(); // Stop is needed because we sent no content length
}
/** Wifi config page handler */
@@ -347,7 +338,7 @@ void WiFiManager::handleWifi(boolean scan) {
server->send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
server->setContentLength(CONTENT_LENGTH_UNKNOWN);
String head = HTTP_HEAD;
String head = FPSTR(HTTP_HEAD);
head.replace("{v}", "Config ESP");
server->sendContent(head);
server->sendContent_P(HTTP_SCRIPT);
@@ -446,6 +437,7 @@ void WiFiManager::handleWifi(boolean scan) {
}
server->sendContent_P(HTTP_FORM_END);
server->sendContent_P(HTTP_SCAN_LINK);
server->sendContent_P(HTTP_END);
server->client().stop();
@@ -496,7 +488,7 @@ void WiFiManager::handleWifiSave() {
server->setContentLength(CONTENT_LENGTH_UNKNOWN);
server->send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
String head = HTTP_HEAD;
String head = FPSTR(HTTP_HEAD);
head.replace("{v}", "Credentials Saved");
server->sendContent(head);
server->sendContent_P(HTTP_SCRIPT);
@@ -513,6 +505,64 @@ void WiFiManager::handleWifiSave() {
connect = true; //signal ready to connect/reset
}
/** Handle the info page */
void WiFiManager::handleInfo() {
DEBUG_WM(F("Info"));
String page = FPSTR(HTTP_HEAD);
page.replace("{v}", "Info");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
page += FPSTR(HTTP_HEAD_END);
page += F("<dl>");
page += F("<dt>Chip ID</dt><dd>");
page += ESP.getChipId();
page += F("</dd>");
page += F("<dt>Flash Chip ID</dt><dd>");
page += ESP.getFlashChipId();
page += F("</dd>");
page += F("<dt>Flash Size</dt><dd>");
page += ESP.getFlashChipSize();
page += F(" bytes</dd>");
page += F("<dt>Soft AP IP</dt><dd>");
page += WiFi.softAPIP().toString();
page += F("</dd>");
page += F("<dt>Soft AP MAC</dt><dd>");
page += WiFi.softAPmacAddress();
page += F("</dd>");
page += F("<dt>Station MAC</dt><dd>");
page += WiFi.macAddress();
page += F("</dd>");
page += F("</dl>");
page += FPSTR(HTTP_END);
server->send(200, "text/html", page);
DEBUG_WM(F("Sent info page"));
}
/** Handle the reset page */
void WiFiManager::handleReset() {
DEBUG_WM(F("Reset"));
String page = FPSTR(HTTP_HEAD);
page.replace("{v}", "Info");
page += FPSTR(HTTP_SCRIPT);
page += FPSTR(HTTP_STYLE);
page += FPSTR(HTTP_HEAD_END);
page += F("Module will reset in a few seconds.");
page += FPSTR(HTTP_END);
server->send(200, "text/html", page);
DEBUG_WM(F("Sent reset page"));
delay(5000);
ESP.reset();
delay(2000);
}
void WiFiManager::handle204() {
DEBUG_WM(F("204 No Response"));
server->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
+6 -3
View File
@@ -20,16 +20,17 @@
const char HTTP_200[] PROGMEM = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
//const char HTTP_HEAD[] PROGMEM = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/><title>{v}</title>";
const char HTTP_HEAD[] PROGMEM = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/><title>{v}</title>";
const char HTTP_STYLE[] PROGMEM = "<style>div,input{padding:5px;font-size:1em;} input{width:95%;} body{text-align:center;font-family:verdana;} button{border:0;border-radius:0.3rem;background-color:#1fa3ec;color:#fff;line-height:2.4rem;font-size:1.2rem;width:100%;}</style>";
const char HTTP_SCRIPT[] PROGMEM = "<script>function c(l){document.getElementById('s').value=l.innerText||l.textContent;document.getElementById('p').focus();}</script>";
const char HTTP_HEAD_END[] PROGMEM = "</head><body><div style='text-align: left; display: inline-block;'>";
const char HTTP_PORTAL_OPTIONS[] PROGMEM = "<form action=\"/wifi\" method=\"get\"><button>Configure WiFi</button></form><br/><form action=\"/0wifi\" method=\"get\"><button>Configure WiFi (No Scan)</button></form>";
const char HTTP_PORTAL_OPTIONS[] PROGMEM = "<form action=\"/wifi\" method=\"get\"><button>Configure WiFi</button></form><br/><form action=\"/0wifi\" method=\"get\"><button>Configure WiFi (No Scan)</button></form><br/><form action=\"/i\" method=\"get\"><button>Info</button></form><br/><form action=\"/r\" method=\"post\"><button>Reset</button></form>";
const char HTTP_ITEM[] PROGMEM = "<div><a href='#' onclick='c(this)'>{v}</a> {r}% {i}</div>";
const char HTTP_ITEM_PADLOCK[] PROGMEM = "<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAALVBMVEX///8EBwfBwsLw8PAzNjaCg4NTVVUjJiZDRUUUFxdiZGSho6OSk5Pg4eFydHTCjaf3AAAAZElEQVQ4je2NSw7AIAhEBamKn97/uMXEGBvozkWb9C2Zx4xzWykBhFAeYp9gkLyZE0zIMno9n4g19hmdY39scwqVkOXaxph0ZCXQcqxSpgQpONa59wkRDOL93eAXvimwlbPbwwVAegLS1HGfZAAAAABJRU5ErkJggg==' width='13px'/>";
const char HTTP_FORM_START[] PROGMEM = "<form method='get' action='wifisave'><input id='s' name='s' length=32 placeholder='SSID'><br/><input id='p' name='p' length=64 type='password' placeholder='password'><br/>";
const char HTTP_FORM_PARAM[] PROGMEM = "<br/><input id='{i}' name='{n}' length={l} placeholder='{p}' value='{v}'>";
const char HTTP_FORM_END[] PROGMEM = "<br/><button type='submit'>save</button></form>";
const char HTTP_SCAN_LINK[] PROGMEM = "<br/><div style=\"text-align: center\"><a href=\"/wifi\">Scan</a></div>";
const char HTTP_SAVED[] PROGMEM = "<div>Credentials Saved<br />Trying to connect ESP to network.<br />If it fails reconnect to AP to try again</div>";
const char HTTP_END[] PROGMEM = "</div></body></html>";
@@ -102,7 +103,7 @@ class WiFiManager
const int WM_DONE = 0;
const int WM_WAIT = 10;
const String HTTP_HEAD = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/><title>{v}</title>";
//const String HTTP_HEAD = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/><title>{v}</title>";
void setupConfigPortal();
@@ -132,6 +133,8 @@ class WiFiManager
void handleRoot();
void handleWifi(boolean scan);
void handleWifiSave();
void handleInfo();
void handleReset();
void handleNotFound();
void handle204();
boolean captivePortal();
+3
View File
@@ -10,8 +10,11 @@
<div><a href='#'>PMica</a> 110% <img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAALVBMVEX///8EBwfBwsLw8PAzNjaCg4NTVVUjJiZDRUUUFxdiZGSho6OSk5Pg4eFydHTCjaf3AAAAZElEQVQ4je2NSw7AIAhEBamKn97/uMXEGBvozkWb9C2Zx4xzWykBhFAeYp9gkLyZE0zIMno9n4g19hmdY39scwqVkOXaxph0ZCXQcqxSpgQpONa59wkRDOL93eAXvimwlbPbwwVAegLS1HGfZAAAAABJRU5ErkJggg==' width="13px"/></div>
<br/>
<form method='get' action='wifisave'><input id='s' name='s' length=32 placeholder='SSID'><br/><input id='p' name='p' length=64 placeholder='password'><br/><button type='submit'>save</button></form>
<br/><div></div>
<br/>
<form action=\"/wifi\" method=\"get\"><button>Configure WiFi</button></form><br/>
<form action=\"/0wifi\" method=\"get\"><button>Configure WiFi (No Scan)</button></form>
<br>
<span><a style="text-align: center;" href=\"/wifi\">Scan</a></span>
</div>
</body></html>