/************************************************************** 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 **************************************************************/ #ifndef WiFiManager_h #define WiFiManager_h #define WEBSERVERSHIM // use webserver shim lib #ifdef ESP8266 extern "C" { #include "user_interface.h" } #include #include #define WIFI_getChipId() ESP.getChipId() #define WIFI_AUTH_OPEN ENC_TYPE_NONE #elif defined(ESP32) #include #include #include #define WIFI_getChipId() (uint32_t)ESP.getEfuseMac() #define WIFI_AUTH_OPEN WIFI_AUTH_OPEN #else #endif #include #include const char HTTP_HEAD[] PROGMEM = "{v}"; // const char HTTP_STYLE[] PROGMEM = ""; const char HTTP_STYLE[] PROGMEM = ""; const char HTTP_SCRIPT[] PROGMEM = ""; const char HTTP_HEAD_END[] PROGMEM = "
"; const char HTTP_PORTAL_OPTIONS[] PROGMEM = "




"; const char HTTP_ITEM[] PROGMEM = ""; // const char HTTP_ITEM[] PROGMEM = "
{v} {R} {r}% {q} {e}
"; // test all tokens //const char HTTP_ITEM_PADLOCK[] PROGMEM = ""; const char HTTP_FORM_START[] PROGMEM = "


"; const char HTTP_FORM_END[] PROGMEM = "
"; const char HTTP_FORM_LABEL[] PROGMEM = ""; const char HTTP_FORM_PARAM_START[] PROGMEM = "
"; const char HTTP_FORM_PARAM[] PROGMEM = "
"; const char HTTP_FORM_PARAM_END[] PROGMEM = "
"; const char HTTP_SCAN_LINK[] PROGMEM = "
"; const char HTTP_SAVED[] PROGMEM = "
Saving Credentials
Trying to connect ESP to network.
If it fails reconnect to AP to try again
"; const char HTTP_END[] PROGMEM = "
"; #define WIFI_MANAGER_MAX_PARAMS 10 #define WFM_LABEL_BEFORE 1 #define WFM_LABEL_AFTER 2 #define WFM_NO_LABEL 0 class WiFiManagerParameter { public: WiFiManagerParameter(const char *custom); WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length); WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom); WiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom, int labelPlacement); const char *getID(); const char *getValue(); const char *getPlaceholder(); int getValueLength(); int getLabelPlacement(); const char *getCustomHTML(); private: const char *_id; const char *_placeholder; char *_value; int _length; int _labelPlacement; const char *_customHTML; void init(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom); void init(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom, int labelPlacement); friend class WiFiManager; }; class WiFiManager { public: WiFiManager(Stream& consolePort); WiFiManager(); ~WiFiManager(); boolean autoConnect(); boolean autoConnect(char const *apName, char const *apPassword = NULL); //if you want to always start the config portal, without trying to connect first boolean startConfigPortal(); boolean startConfigPortal(char const *apName, char const *apPassword = NULL); boolean process(); void startWebPortal(); void stopWebPortal(); // get the AP name of the config portal, so it can be used in the callback String getConfigPortalSSID(); void resetSettings(); void reboot(); //sets timeout before AP,webserver loop ends and exits even if there has been no setup. //useful for devices that failed to connect at some point and got stuck in a webserver loop //in seconds setConfigPortalTimeout is a new name for setTimeout void setConfigPortalTimeout(unsigned long seconds); void setTimeout(unsigned long seconds); // @deprecated, alias //sets timeout for which to attempt connecting, useful if you get a lot of failed connects void setConnectTimeout(unsigned long seconds); void setDebugOutput(boolean debug); //defaults to not showing anything under 8% signal quality if called void setMinimumSignalQuality(int quality = 8); //sets a custom ip /gateway /subnet configuration void setAPStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn); //sets config for a static IP void setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn); //called when AP mode and config portal is started void setAPCallback( void (*func)(WiFiManager*) ); //called when settings have been changed and connection was successful void setSaveConfigCallback( void (*func)(void) ); //adds a custom parameter void addParameter(WiFiManagerParameter *p); //if this is set, it will exit after config, even if connection is unsuccessful. void setBreakAfterConfig(boolean shouldBreak); //if this is set, portal will be blocking and wait until save or exit, is false user must manually process to handle config portal void setConfigPortalBlocking(boolean shouldBlock); //if this is set, customise style void setCustomHeadElement(const char* element); //if this is true, remove duplicated Access Points - defaut true void setRemoveDuplicateAPs(boolean removeDuplicates); //setter for ESP wifi.persistent so we can remember it and restore user preference, as WIFi._persistent is protected void setRestorePersistent(boolean persistent); //if true, always show static net inputs, IP, subnet, gateway, if false, only show when already set. void setShowStaticFields(boolean alwaysShow); //if true disable captive portal redirection void setCaptivePortalEnable(boolean enabled); // if true enable autoreconnecting void setWiFiAutoReconnect(boolean enabled); private: std::unique_ptr dnsServer; #if defined(ESP32) && defined(WEBSERVERSHIM) std::unique_ptr server; #else std::unique_ptr server; #endif //const int WM_DONE = 0; //const int WM_WAIT = 10; //const String HTTP_HEAD = "{v}"; void setupConfigPortal(); void startWPS(); bool startAP(); // typedef enum WiFiMode // { // WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 // } WiFiMode_t; String _apName = "no-net"; String _apPassword = ""; String _ssid = ""; String _pass = ""; unsigned long _configPortalTimeout = 0; unsigned long _connectTimeout = 0; unsigned long _configPortalStart = 0; boolean _userpersistent = true; WiFiMode_t _usermode = WIFI_OFF; boolean _enableCaptivePortal = true; #ifdef ESP8266 String _wifissidprefix = "ESP"; #elif defined(ESP32) String _wifissidprefix = "ESP32"; #else String _wifissidprefix = "WM"; #endif IPAddress _ap_static_ip; IPAddress _ap_static_gw; IPAddress _ap_static_sn; IPAddress _sta_static_ip; IPAddress _sta_static_gw; IPAddress _sta_static_sn; boolean _sta_show_static_fields = false; // option parameters int _paramsCount = 0; int _minimumQuality = -1; boolean _removeDuplicateAPs = true; boolean _shouldBreakAfterConfig = false; boolean _tryWPS = false; boolean _configPortalIsBlocking = true; boolean _wifiAutoReconnect = false; const char* _customHeadElement = ""; //String getEEPROMString(int start, int len); //void setEEPROMString(int start, int len, String string); int status = WL_IDLE_STATUS; int connectWifi(String ssid, String pass); uint8_t waitForConnectResult(); uint8_t waitForConnectResult(uint16_t timeout); String getWLStatusString(uint8_t status); void handleRoot(); void handleWifi(boolean scan); void handleWifiSave(); void handleInfo(); void handleReset(); void handleNotFound(); void handleExit(); void handleErase(); boolean captivePortal(); boolean configPortalHasTimeout(); boolean stopConfigPortal(); uint8_t handleConfigPortal(); bool WiFi_Mode(WiFiMode_t m); bool WiFi_Mode(WiFiMode_t m,bool persistent); bool WiFi_Disconnect(); bool WiFi_enableSTA(bool enable); bool WiFi_enableSTA(bool enable,bool persistent); bool WiFi_eraseConfig(); uint8_t WiFi_softap_num_stations(); bool WiFi_hasAutoConnect(); void WiFi_autoReconnect(); static void WiFiEvent(WiFiEvent_t event); void debugSoftAPConfig(); String getParamOut(); String getScanItemOut(); String getStaticOut(); // DNS server const byte DNS_PORT = 53; //helpers int getRSSIasQuality(int RSSI); boolean isIp(String str); String toStringIp(IPAddress ip); boolean validApPassword(); String encryptionTypeStr(uint8_t authmode); void reportStatus(String &page); boolean connect; boolean abort; boolean reset = false; boolean configPortalActive = false; boolean webPortalActive = false; boolean _debug = true; boolean portalTimeoutResult = false; boolean portalAbortResult = false; boolean storeSTAmode = true; // option store persistent STA mode in connectwifi int timer = 0; void (*_apcallback)(WiFiManager*) = NULL; void (*_savecallback)(void) = NULL; Stream& _debugPort; // debug output stream ref WiFiManagerParameter* _params[WIFI_MANAGER_MAX_PARAMS]; template void DEBUG_WM(Generic text); template void DEBUG_WM(Generic text,Genericb textb); template auto optionalIPFromString(T *obj, const char *s) -> decltype( obj->fromString(s) ) { return obj->fromString(s); } auto optionalIPFromString(...) -> bool { DEBUG_WM("NO fromString METHOD ON IPAddress, you need ESP8266 core 2.1.0 or newer for Custom IP configuration to work."); return false; } }; #endif