/************************************************************** WiFiManager is a library for the ESP8266/Arduino platform (https://github.com/esp8266/Arduino) to enable easy configuration and reconfiguration of WiFi credentials using a Captive Portal 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/master/libraries/DNSServer/examples/CaptivePortalAdvanced Built by AlexT https://github.com/tzapu Licensed under MIT license **************************************************************/ #include "WiFiManager.h" WiFiManagerParameter::WiFiManagerParameter(const char *custom) { _id = NULL; _placeholder = NULL; _length = 0; _value = NULL; _customHTML = custom; } WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length) { init(id, placeholder, defaultValue, length, ""); } WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom) { init(id, placeholder, defaultValue, length, custom); } void WiFiManagerParameter::init(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom) { _id = id; _placeholder = placeholder; _length = length; _value = new char[length + 1]; for (int i = 0; i < length + 1; i++) { _value[i] = 0; } if (defaultValue != NULL) { strncpy(_value, defaultValue, length); } _customHTML = custom; } WiFiManagerParameter::~WiFiManagerParameter() { if (_value != NULL) { delete[] _value; } } const char* WiFiManagerParameter::getValue() { return _value; } const char* WiFiManagerParameter::getID() { return _id; } const char* WiFiManagerParameter::getPlaceholder() { return _placeholder; } int WiFiManagerParameter::getValueLength() { return _length; } const char* WiFiManagerParameter::getCustomHTML() { return _customHTML; } WiFiManager::WiFiManager() { _max_params = WIFI_MANAGER_MAX_PARAMS; _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*)); } WiFiManager::~WiFiManager() { if (_params != NULL) { DEBUG_WM(F("freeing allocated params!")); free(_params); } } bool WiFiManager::addParameter(WiFiManagerParameter *p) { if(_paramsCount + 1 > _max_params) { // rezise the params array _max_params += WIFI_MANAGER_MAX_PARAMS; DEBUG_WM(F("Increasing _max_params to:")); DEBUG_WM(_max_params); WiFiManagerParameter** new_params = (WiFiManagerParameter**)realloc(_params, _max_params * sizeof(WiFiManagerParameter*)); if (new_params != NULL) { _params = new_params; } else { DEBUG_WM(F("ERROR: failed to realloc params, size not increased!")); return false; } } _params[_paramsCount] = p; _paramsCount++; DEBUG_WM(F("Adding parameter")); DEBUG_WM(p->getID()); return true; } void WiFiManager::setupConfigPortal() { dnsServer.reset(new DNSServer()); server.reset(new ESP8266WebServer(80)); DEBUG_WM(F("")); _configPortalStart = millis(); DEBUG_WM(F("Configuring access point... ")); DEBUG_WM(_apName); if (_apPassword != NULL) { if (strlen(_apPassword) < 8 || strlen(_apPassword) > 63) { // fail passphrase to short or long! DEBUG_WM(F("Invalid AccessPoint password. Ignoring")); _apPassword = NULL; } DEBUG_WM(_apPassword); } //optional soft ip config if (_ap_static_ip) { DEBUG_WM(F("Custom AP IP/GW/Subnet")); WiFi.softAPConfig(_ap_static_ip, _ap_static_gw, _ap_static_sn); } if (_apPassword != NULL) { WiFi.softAP(_apName, _apPassword);//password option } else { WiFi.softAP(_apName); } delay(500); // Without delay I've seen the IP address blank DEBUG_WM(F("AP IP address: ")); DEBUG_WM(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(String(F("/")), std::bind(&WiFiManager::handleRoot, this)); server->on(String(F("/wifi")), std::bind(&WiFiManager::handleWifi, this, true)); server->on(String(F("/0wifi")), std::bind(&WiFiManager::handleWifi, this, false)); server->on(String(F("/wifisave")), std::bind(&WiFiManager::handleWifiSave, this)); server->on(String(F("/i")), std::bind(&WiFiManager::handleInfo, this)); server->on(String(F("/r")), std::bind(&WiFiManager::handleReset, this)); //server->on("/generate_204", std::bind(&WiFiManager::handle204, this)); //Android/Chrome OS captive portal check. server->on(String(F("/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_WM(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, char const *apPassword) { DEBUG_WM(F("")); DEBUG_WM(F("AutoConnect")); // read eeprom for ssid and pass //String ssid = getSSID(); //String pass = getPassword(); // attempt to connect; should it fail, fall back to AP WiFi.mode(WIFI_STA); if (connectWifi("", "") == WL_CONNECTED) { DEBUG_WM(F("IP Address:")); DEBUG_WM(WiFi.localIP()); //connected return true; } return startConfigPortal(apName, apPassword); } boolean WiFiManager::configPortalHasTimeout(){ if(_configPortalTimeout == 0 || wifi_softap_get_station_num() > 0){ _configPortalStart = millis(); // kludge, bump configportal start time to skew timeouts return false; } return (millis() > _configPortalStart + _configPortalTimeout); } boolean WiFiManager::startConfigPortal() { String ssid = "ESP" + String(ESP.getChipId()); return startConfigPortal(ssid.c_str(), NULL); } boolean WiFiManager::startConfigPortal(char const *apName, char const *apPassword) { //setup AP WiFi.mode(WIFI_AP_STA); DEBUG_WM(F("SET AP STA")); _apName = apName; _apPassword = apPassword; //notify we entered AP mode if ( _apcallback != NULL) { _apcallback(this); } connect = false; setupConfigPortal(); while(1){ // check if timeout if(configPortalHasTimeout()) break; //DNS dnsServer->processNextRequest(); //HTTP server->handleClient(); if (connect) { connect = false; delay(2000); DEBUG_WM(F("Connecting to new AP")); // using user-provided _ssid, _pass in place of system-stored ssid and pass if (connectWifi(_ssid, _pass) != WL_CONNECTED) { DEBUG_WM(F("Failed to connect.")); } else { //connected WiFi.mode(WIFI_STA); //notify that configuration has changed and any optional parameters should be saved if ( _savecallback != NULL) { //todo: check if any custom parameters actually exist, and check if they really changed maybe _savecallback(); } break; } if (_shouldBreakAfterConfig) { //flag set to exit after config after trying to connect //notify that configuration has changed and any optional parameters should be saved if ( _savecallback != NULL) { //todo: check if any custom parameters actually exist, and check if they really changed maybe _savecallback(); } break; } } yield(); } server.reset(); dnsServer.reset(); return WiFi.status() == WL_CONNECTED; } int WiFiManager::connectWifi(String ssid, String pass) { DEBUG_WM(F("Connecting as wifi client...")); // check if we've got static_ip settings, if we do, use those. if (_sta_static_ip) { DEBUG_WM(F("Custom STA IP/GW/Subnet")); WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn); DEBUG_WM(WiFi.localIP()); } //fix for auto connect racing issue if (WiFi.status() == WL_CONNECTED) { DEBUG_WM(F("Already connected. Bailing out.")); return WL_CONNECTED; } //check if we have ssid and pass and force those, if not, try with last saved values if (ssid != "") { WiFi.begin(ssid.c_str(), pass.c_str()); } else { if (WiFi.SSID()) { DEBUG_WM(F("Using last saved values, should be faster")); //trying to fix connection in progress hanging ETS_UART_INTR_DISABLE(); wifi_station_disconnect(); ETS_UART_INTR_ENABLE(); WiFi.begin(); } else { DEBUG_WM(F("No saved credentials")); } } int connRes = waitForConnectResult(); DEBUG_WM ("Connection result: "); DEBUG_WM ( connRes ); //not connected, WPS enabled, no pass - first attempt if (_tryWPS && connRes != WL_CONNECTED && pass == "") { startWPS(); //should be connected at the end of WPS connRes = waitForConnectResult(); } return connRes; } uint8_t WiFiManager::waitForConnectResult() { if (_connectTimeout == 0) { return WiFi.waitForConnectResult(); } else { DEBUG_WM (F("Waiting for connection result with time out")); unsigned long start = millis(); boolean keepConnecting = true; uint8_t status; while (keepConnecting) { status = WiFi.status(); if (millis() > start + _connectTimeout) { keepConnecting = false; DEBUG_WM (F("Connection timed out")); } if (status == WL_CONNECTED || status == WL_CONNECT_FAILED) { keepConnecting = false; } delay(100); } return status; } } void WiFiManager::startWPS() { DEBUG_WM(F("START WPS")); WiFi.beginWPSConfig(); DEBUG_WM(F("END WPS")); } /* String WiFiManager::getSSID() { if (_ssid == "") { DEBUG_WM(F("Reading SSID")); _ssid = WiFi.SSID(); DEBUG_WM(F("SSID: ")); DEBUG_WM(_ssid); } return _ssid; } String WiFiManager::getPassword() { if (_pass == "") { DEBUG_WM(F("Reading Password")); _pass = WiFi.psk(); DEBUG_WM("Password: " + _pass); //DEBUG_WM(_pass); } return _pass; } */ String WiFiManager::getConfigPortalSSID() { return _apName; } void WiFiManager::resetSettings() { DEBUG_WM(F("settings invalidated")); DEBUG_WM(F("THIS MAY CAUSE AP NOT TO START UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA.")); WiFi.disconnect(true); //delay(200); } void WiFiManager::setTimeout(unsigned long seconds) { setConfigPortalTimeout(seconds); } void WiFiManager::setConfigPortalTimeout(unsigned long seconds) { _configPortalTimeout = seconds * 1000; } void WiFiManager::setConnectTimeout(unsigned long seconds) { _connectTimeout = seconds * 1000; } void WiFiManager::setDebugOutput(boolean debug) { _debug = debug; } void WiFiManager::setAPStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn) { _ap_static_ip = ip; _ap_static_gw = gw; _ap_static_sn = sn; } void WiFiManager::setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn) { _sta_static_ip = ip; _sta_static_gw = gw; _sta_static_sn = sn; } void WiFiManager::setMinimumSignalQuality(int quality) { _minimumQuality = quality; } void WiFiManager::setBreakAfterConfig(boolean shouldBreak) { _shouldBreakAfterConfig = shouldBreak; } /** Handle root or redirect to captive portal */ void WiFiManager::handleRoot() { DEBUG_WM(F("Handle root")); if (captivePortal()) { // If caprive portal redirect instead of displaying the page. return; } String page = FPSTR(HTTP_HEAD); page.replace("{v}", "Options"); page += FPSTR(HTTP_SCRIPT); page += FPSTR(HTTP_STYLE); page += _customHeadElement; page += FPSTR(HTTP_HEAD_END); page += String(F("