/** Handle root or redirect to captive portal */ void handleRoot() { if (captivePortal()) { // If caprive portal redirect instead of displaying the page. return; } server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); String Page; Page += F( "
" "You are connected through the soft AP: ")) + softAP_ssid + F("
"); } else { Page += String(F("You are connected through the wifi network: ")) + ssid + F("
"); } Page += F( "You may want to config the wifi connection.
" ""); server.send(200, "text/html", Page); } /** Redirect to captive portal if we got a request for another domain. Return true in that case so the page handler do not try to handle the request again. */ boolean captivePortal() { if (!isIp(server.hostHeader()) && server.hostHeader() != (String(myHostname) + ".local")) { Serial.println("Request redirected to captive portal"); server.sendHeader("Location", String("http://") + toStringIp(server.client().localIP()), true); server.send(302, "text/plain", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. server.client().stop(); // Stop is needed because we sent no content length return true; } return false; } /** Wifi config page handler */ void handleWifi() { server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); String Page; Page += F( "" "You are connected through the soft AP: ")) + softAP_ssid + F("
"); } else { Page += String(F("You are connected through the wifi network: ")) + ssid + F("
"); } Page += String(F( "\r\nSoftAP config |
---|
SSID ")) + String(softAP_ssid) + F(" |
IP ") + toStringIp(WiFi.softAPIP()) + F(" |
WLAN config |
---|
SSID ") + String(ssid) + F(" |
IP ") + toStringIp(WiFi.localIP()) + F(" |
WLAN list (refresh if any missing) |
---|
SSID ")) + WiFi.SSID(i) + ((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? F(" ") : F(" *")) + F(" (") + WiFi.RSSI(i) + F(") |
No WLAN found |
You may want to return to the home page.
" ""); server.send(200, "text/html", Page); server.client().stop(); // Stop is needed because we sent no content length } /** Handle the WLAN save form and redirect to WLAN config page again */ void handleWifiSave() { Serial.println("wifi save"); server.arg("n").toCharArray(ssid, sizeof(ssid) - 1); server.arg("p").toCharArray(password, sizeof(password) - 1); server.sendHeader("Location", "wifi", true); server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); server.send(302, "text/plain", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. server.client().stop(); // Stop is needed because we sent no content length saveCredentials(); connect = strlen(ssid) > 0; // Request WLAN connect with new credentials if there is a SSID } void handleNotFound() { if (captivePortal()) { // If caprive portal redirect instead of displaying the error page. return; } String message = F("File Not Found\n\n"); message += F("URI: "); message += server.uri(); message += F("\nMethod: "); message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += F("\nArguments: "); message += server.args(); message += F("\n"); for (uint8_t i = 0; i < server.args(); i++) { message += String(F(" ")) + server.argName(i) + F(": ") + server.arg(i) + F("\n"); } server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); server.send(404, "text/plain", message); }