/************************************************************** * WiFiManager is a library for the ESP8266/Arduino platform * (https://github.com/esp8266/Arduino) to enable easy * configuration and reconfiguration of WiFi credentials and * store them in EEPROM. * inspired by: * http://www.esp8266.com/viewtopic.php?f=29&t=2520 * https://github.com/chriscook8/esp-arduino-apboot * https://github.com/esp8266/Arduino/tree/esp8266/hardware/esp8266com/esp8266/libraries/DNSServer/examples/CaptivePortalAdvanced * Built by AlexT https://github.com/tzapu * Licensed under MIT license **************************************************************/ #include "WiFiManager.h" WiFiManager::WiFiManager() { } void WiFiManager::begin(char const *apName, char const *apPasswd) { dnsServer.reset(new DNSServer()); server.reset(new ESP8266WebServer(80)); DEBUG_PRINT(F("")); _apName = apName; _apPasswd = apPasswd; start = millis(); DEBUG_PRINT(F("Configuring access point... ")); DEBUG_PRINT(_apName); if (_apPasswd != NULL) { if(strlen(_apPasswd) < 8 || strlen(_apPasswd) > 63) { // fail passphrase to short or long! DEBUG_PRINT(F("Invalid AccessPoint password")); } DEBUG_PRINT(_apPasswd); } //optional soft ip config if (_ip) { DEBUG_PRINT(F("Custom IP/GW/Subnet")); WiFi.softAPConfig(_ip, _gw, _sn); } if (_apPasswd != NULL) { WiFi.softAP(_apName, _apPasswd);//password option } else { WiFi.softAP(_apName); } delay(500); // Without delay I've seen the IP address blank DEBUG_PRINT(F("AP IP address: ")); DEBUG_PRINT(WiFi.softAPIP()); /* Setup the DNS server redirecting all the domains to the apIP */ dnsServer->setErrorReplyCode(DNSReplyCode::NoError); dnsServer->start(DNS_PORT, "*", WiFi.softAPIP()); /* Setup web pages: root, wifi config pages, SO captive portal detectors and not found. */ server->on("/", std::bind(&WiFiManager::handleRoot, this)); 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("/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)); server->begin(); // Web server start DEBUG_PRINT(F("HTTP server started")); } boolean WiFiManager::autoConnect() { String ssid = "ESP" + String(ESP.getChipId()); return autoConnect(ssid.c_str(),NULL); } boolean WiFiManager::autoConnect(char const *apName) { return autoConnect(apName,NULL); } boolean WiFiManager::autoConnect(char const *apName, char const *apPasswd, bool forceAP) { DEBUG_PRINT(F("")); DEBUG_PRINT(F("AutoConnect")); // read eeprom for ssid and pass String ssid = getSSID(); String pass = getPassword(); if ( ! forceAP ) { // attempt to connect; should it fail, fall back to AP WiFi.mode(WIFI_STA); if(connectWifi(ssid, pass) == WL_CONNECTED) { DEBUG_PRINT(F("IP Address:")); DEBUG_PRINT(WiFi.localIP()); //connected return true; } } //not connected //setup AP WiFi.mode(WIFI_AP); //notify we entered AP mode if( _apcallback != NULL) { _apcallback(); } connect = false; begin(apName,apPasswd); while(timeout == 0 || millis() < start + timeout) { //DNS dnsServer->processNextRequest(); //HTTP server->handleClient(); if(connect) { delay(2000); DEBUG_PRINT(F("Connecting to new AP")); connect = false; // using user-provided _ssid, _pass in place of system-stored ssid amd pass if (connectWifi(_ssid, _pass) != WL_CONNECTED) { DEBUG_PRINT(F("Failed to connect.")); } else { //connected WiFi.mode(WIFI_STA); break; } } yield(); } server.reset(); dnsServer.reset(); return WiFi.status() == WL_CONNECTED; } int WiFiManager::connectWifi(String ssid, String pass) { DEBUG_PRINT(F("Connecting as wifi client...")); //WiFi.disconnect(); WiFi.begin(ssid.c_str(), pass.c_str()); int connRes = WiFi.waitForConnectResult(); DEBUG_PRINT ("Connection result: "); DEBUG_PRINT ( connRes ); return connRes; } String WiFiManager::getSSID() { if (_ssid == "") { DEBUG_PRINT(F("Reading SSID")); _ssid = WiFi.SSID();//getEEPROMString(0, 32); DEBUG_PRINT(F("SSID: ")); DEBUG_PRINT(_ssid); } return _ssid; } String WiFiManager::getPassword() { if (_pass == "") { DEBUG_PRINT(F("Reading Password")); _pass = WiFi.psk();//getEEPROMString(32, 64); DEBUG_PRINT("Password: " + _pass); //DEBUG_PRINT(_pass); } return _pass; } String WiFiManager::urldecode(const char *src) { String decoded = ""; char a, b; while (*src) { if ((*src == '%') && ((a = src[1]) && (b = src[2])) && (isxdigit(a) && isxdigit(b))) { if (a >= 'a') a -= 'a' - 'A'; if (a >= 'A') a -= ('A' - 10); else a -= '0'; if (b >= 'a') b -= 'a' - 'A'; if (b >= 'A') b -= ('A' - 10); else b -= '0'; decoded += char(16 * a + b); src += 3; } else if (*src == '+') { decoded += ' '; *src++; } else { decoded += *src; *src++; } } decoded += '\0'; return decoded; } void WiFiManager::resetSettings() { DEBUG_PRINT(F("settings invalidated")); DEBUG_PRINT(F("THIS MAY CAUSE AP NOT TO STRT UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA.")); WiFi.disconnect(true); //delay(200); } void WiFiManager::setTimeout(unsigned long seconds) { timeout = seconds * 1000; } void WiFiManager::setDebugOutput(boolean debug) { _debug = debug; } void WiFiManager::setAPConfig(IPAddress ip, IPAddress gw, IPAddress sn) { _ip = ip; _gw = gw; _sn = sn; } /** Handle root or redirect to captive portal */ void WiFiManager::handleRoot() { DEBUG_PRINT(F("Handle root")); 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"); server->send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. 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("