123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- #ifndef WiFiManager_h
- #define WiFiManager_h
- #if defined(ESP8266) || defined(ESP32)
- #ifdef ESP8266
- #include <core_version.h>
- #endif
- #include <vector>
- #ifdef ARDUINO_ESP8266_RELEASE_2_3_0
- #warning "ARDUINO_ESP8266_RELEASE_2_3_0, some WM features disabled"
- #define WM_NOASYNC
- #endif
- #define WM_WEBSERVERSHIM
- #ifdef ESP8266
- extern "C" {
- #include "user_interface.h"
- }
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #ifdef WM_MDNS
- #include <ESP8266mDNS.h>
- #endif
- #define WIFI_getChipId() ESP.getChipId()
- #define WM_WIFIOPEN ENC_TYPE_NONE
- #elif defined(ESP32)
- #include <WiFi.h>
- #include <esp_wifi.h>
-
- #define WIFI_getChipId() (uint32_t)ESP.getEfuseMac()
- #define WM_WIFIOPEN WIFI_AUTH_OPEN
- #ifndef WEBSERVER_H
- #ifdef WM_WEBSERVERSHIM
- #include <WebServer.h>
- #else
- #include <ESP8266WebServer.h>
-
-
- #endif
- #endif
- #ifdef WM_ERASE_NVS
- #include <nvs.h>
- #include <nvs_flash.h>
- #endif
- #ifdef WM_MDNS
- #include <ESPmDNS.h>
- #endif
- #ifdef WM_RTC
- #include <rom/rtc.h>
- #endif
- #else
- #endif
- #include <DNSServer.h>
- #include <memory>
- #include "strings_en.h"
- #ifndef WIFI_MANAGER_MAX_PARAMS
- #define WIFI_MANAGER_MAX_PARAMS 5
- #endif
- #define WFM_LABEL_BEFORE 1
- #define WFM_LABEL_AFTER 2
- #define WFM_NO_LABEL 0
- class WiFiManagerParameter {
- public:
-
- WiFiManagerParameter();
- WiFiManagerParameter(const char *custom);
- WiFiManagerParameter(const char *id, const char *label);
- WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length);
- WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length, const char *custom);
- WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length, const char *custom, int labelPlacement);
- ~WiFiManagerParameter();
- const char *getID();
- const char *getValue();
- const char *getLabel();
- const char *getPlaceholder();
- int getValueLength();
- int getLabelPlacement();
- const char *getCustomHTML();
- void setValue(const char *defaultValue, int length);
- protected:
- void init(const char *id, const char *label, const char *defaultValue, int length, const char *custom, int labelPlacement);
- private:
- const char *_id;
- const char *_label;
- char *_value;
- int _length;
- int _labelPlacement;
- const char *_customHTML;
- friend class WiFiManager;
- };
- class WiFiManager
- {
- public:
- WiFiManager(Stream& consolePort);
- WiFiManager();
- ~WiFiManager();
- void WiFiManagerInit();
-
- boolean autoConnect();
- boolean autoConnect(char const *apName, char const *apPassword = NULL);
-
- boolean startConfigPortal();
- boolean startConfigPortal(char const *apName, char const *apPassword = NULL);
-
- bool stopConfigPortal();
-
-
- void startWebPortal();
-
- void stopWebPortal();
-
- boolean process();
-
- String getConfigPortalSSID();
- int getRSSIasQuality(int RSSI);
-
- void resetSettings();
-
- void reboot();
-
- bool disconnect();
-
- bool erase();
- bool erase(bool opt);
-
- bool addParameter(WiFiManagerParameter *p);
-
- WiFiManagerParameter** getParameters();
-
- int getParametersCount();
-
-
- void setAPCallback( std::function<void(WiFiManager*)> func );
-
- void setWebServerCallback( std::function<void()> func );
-
- void setConfigResetCallback( std::function<void()> func );
-
- void setSaveConfigCallback( std::function<void()> func );
-
- void setSaveParamsCallback( std::function<void()> func );
-
- void setPreSaveConfigCallback( std::function<void()> func );
-
-
-
- void setConfigPortalTimeout(unsigned long seconds);
- void setTimeout(unsigned long seconds);
-
- void setConnectTimeout(unsigned long seconds);
-
- void setSaveConnectTimeout(unsigned long seconds);
-
- void setDebugOutput(boolean debug);
-
- void setMinimumSignalQuality(int quality = 8);
-
- void setAPStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn);
-
- void setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn);
-
- void setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn, IPAddress dns);
-
- void setBreakAfterConfig(boolean shouldBreak);
-
-
-
- void setConfigPortalBlocking(boolean shouldBlock);
-
- void setCustomHeadElement(const char* element);
-
- void setRemoveDuplicateAPs(boolean removeDuplicates);
-
- void setRestorePersistent(boolean persistent);
-
- void setShowStaticFields(boolean alwaysShow);
-
- void setShowDnsFields(boolean alwaysShow);
-
- void setShowPassword(boolean show);
-
- void setCaptivePortalEnable(boolean enabled);
-
- void setAPClientCheck(boolean enabled);
-
- void setWebPortalClientCheck(boolean enabled);
-
- void setWiFiAutoReconnect(boolean enabled);
-
- void setScanDispPerc(boolean enabled);
-
- void setEnableConfigPortal(boolean enable);
-
- bool setHostname(const char * hostname);
-
- void setShowInfoErase(boolean enabled);
-
- void setWiFiAPChannel(int32_t channel);
-
- void setWiFiAPHidden(bool hidden);
-
- void setCleanConnect(bool enable);
-
-
- void setMenu(std::vector<const char*>& menu);
- void setMenu(const char* menu[], uint8_t size);
-
-
- void setParamsPage(bool enable);
-
- uint8_t getLastConxResult();
-
- String getWLStatusString(uint8_t status);
- String getModeString(uint8_t mode);
-
- bool getWiFiIsSaved();
-
- String getWiFiPass(bool persistent = false);
-
- String getWiFiSSID(bool persistent = false);
-
- void debugSoftAPConfig();
-
- void debugPlatformInfo();
- String htmlEntities(String str);
-
-
- void setCountry(String cc);
-
- void setClass(String str);
- String getDefaultAPName();
-
- std::unique_ptr<DNSServer> dnsServer;
- #if defined(ESP32) && defined(WM_WEBSERVERSHIM)
- using WM_WebServer = WebServer;
- #else
- using WM_WebServer = ESP8266WebServer;
- #endif
-
- std::unique_ptr<WM_WebServer> server;
- private:
- std::vector<uint8_t> _menuIds;
- std::vector<const char *> _menuIdsParams = {"wifi","param","info","exit"};
- std::vector<const char *> _menuIdsDefault = {"wifi","info","exit"};
-
- IPAddress _ap_static_ip;
- IPAddress _ap_static_gw;
- IPAddress _ap_static_sn;
- IPAddress _sta_static_ip;
- IPAddress _sta_static_gw;
- IPAddress _sta_static_sn;
- IPAddress _sta_static_dns;
-
- const byte DNS_PORT = 53;
- const byte HTTP_PORT = 80;
- String _apName = "no-net";
- String _apPassword = "";
- String _ssid = "";
- String _pass = "";
-
-
- unsigned long _configPortalTimeout = 0;
- unsigned long _connectTimeout = 0;
- unsigned long _saveTimeout = 0;
- unsigned long _configPortalStart = 0;
- unsigned long _webPortalAccessed = 0;
- WiFiMode_t _usermode = WIFI_OFF;
- String _wifissidprefix = FPSTR(S_ssidpre);
- uint8_t _lastconxresult = WL_IDLE_STATUS;
- int _numNetworks = 0;
- unsigned long _lastscan = 0;
- unsigned long _startscan = 0;
- int _cpclosedelay = 2000;
- bool _cleanConnect = false;
-
- bool _disableSTA = false;
- bool _disableSTAConn = true;
- bool _channelSync = false;
- int32_t _apChannel = 0;
- bool _apHidden = false;
- #ifdef ESP32
- static uint8_t _lastconxresulttmp;
- #endif
- #ifndef WL_STATION_WRONG_PASSWORD
- uint8_t WL_STATION_WRONG_PASSWORD = 7;
- #endif
-
- int _minimumQuality = -1;
- int _staShowStaticFields = 0;
- int _staShowDns = 0;
- boolean _removeDuplicateAPs = true;
- boolean _showPassword = false;
- boolean _shouldBreakAfterConfig = false;
- boolean _configPortalIsBlocking = true;
- boolean _enableCaptivePortal = true;
- boolean _userpersistent = true;
- boolean _wifiAutoReconnect = true;
- boolean _apClientCheck = false;
- boolean _webClientCheck = true;
- boolean _scanDispOptions = false;
- boolean _paramsInWifi = true;
- boolean _showInfoErase = true;
- boolean _enableConfigPortal = true;
- const char * _hostname = "";
- const char* _customHeadElement = "";
- String _bodyClass = "";
-
- boolean _preloadwifiscan = true;
- boolean _asyncScan = false;
- unsigned int _scancachetime = 30000;
- boolean _disableIpFields = false;
- String _wificountry = "";
-
- bool esp32persistent = false;
- bool _hasBegun = false;
- void _begin();
- void _end();
- void setupConfigPortal();
- bool shutdownConfigPortal();
- #ifdef NO_EXTRA_4K_HEAP
- boolean _tryWPS = false;
- void startWPS();
- #endif
- bool startAP();
- uint8_t connectWifi(String ssid, String pass);
- bool setSTAConfig();
- bool wifiConnectDefault();
- bool wifiConnectNew(String ssid, String pass);
- uint8_t waitForConnectResult();
- uint8_t waitForConnectResult(uint16_t timeout);
- void updateConxResult(uint8_t status);
-
- void handleRoot();
- void handleWifi(boolean scan);
- void handleWifiSave();
- void handleInfo();
- void handleReset();
- void handleNotFound();
- void handleExit();
- void handleClose();
-
- void handleErase(boolean opt);
- void handleParam();
- void handleWiFiStatus();
- void handleRequest();
- void handleParamSave();
- void doParamSave();
- boolean captivePortal();
- boolean configPortalHasTimeout();
- uint8_t processConfigPortal();
- void stopCaptivePortal();
-
- 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();
- String WiFi_SSID(bool persistent = false) const;
- String WiFi_psk(bool persistent = false) const;
- bool WiFi_scanNetworks();
- bool WiFi_scanNetworks(bool force,bool async);
- bool WiFi_scanNetworks(unsigned int cachetime,bool async);
- bool WiFi_scanNetworks(unsigned int cachetime);
- void WiFi_scanComplete(int networksFound);
- bool WiFiSetCountry();
- #ifdef ESP32
- void WiFiEvent(WiFiEvent_t event, system_event_info_t info);
- #endif
-
- String getParamOut();
- String getIpForm(String id, String title, String value);
- String getScanItemOut();
- String getStaticOut();
- String getHTTPHead(String title);
- String getMenuOut();
-
- boolean isIp(String str);
- String toStringIp(IPAddress ip);
- boolean validApPassword();
- String encryptionTypeStr(uint8_t authmode);
- void reportStatus(String &page);
- String getInfoData(String id);
-
- boolean connect;
- boolean abort;
- boolean reset = false;
- boolean configPortalActive = false;
- boolean webPortalActive = false;
- boolean portalTimeoutResult = false;
- boolean portalAbortResult = false;
- boolean storeSTAmode = true;
- int timer = 0;
-
-
- int _paramsCount = 0;
- int _max_params;
- WiFiManagerParameter** _params = NULL;
-
- typedef enum {
- DEBUG_ERROR = 0,
- DEBUG_NOTIFY = 1,
- DEBUG_VERBOSE = 2,
- DEBUG_DEV = 3,
- DEBUG_MAX = 4
- } wm_debuglevel_t;
- boolean _debug = true;
-
-
-
- #ifdef WM_DEBUG_LEVEL
- uint8_t _debugLevel = (uint8_t)WM_DEBUG_LEVEL;
- #else
- uint8_t _debugLevel = DEBUG_DEV;
- #endif
-
- #ifdef WM_DEBUG_PORT
- Stream& _debugPort = WM_DEBUG_PORT;
- #else
- Stream& _debugPort = Serial;
- #endif
- template <typename Generic>
- void DEBUG_WM(Generic text);
- template <typename Generic>
- void DEBUG_WM(wm_debuglevel_t level,Generic text);
- template <typename Generic, typename Genericb>
- void DEBUG_WM(Generic text,Genericb textb);
- template <typename Generic, typename Genericb>
- void DEBUG_WM(wm_debuglevel_t level, Generic text,Genericb textb);
-
-
- std::function<void(WiFiManager*)> _apcallback;
- std::function<void()> _webservercallback;
- std::function<void()> _savewificallback;
- std::function<void()> _presavecallback;
- std::function<void()> _saveparamscallback;
- std::function<void()> _resetcallback;
- template <class T>
- auto optionalIPFromString(T *obj, const char *s) -> decltype( obj->fromString(s) ) {
- return obj->fromString(s);
- }
- auto optionalIPFromString(...) -> bool {
-
- return false;
- }
- };
- #endif
- #endif
|