WiFiManager.cpp 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906
  1. /**
  2. * WiFiManager.cpp
  3. *
  4. * WiFiManager, a library for the ESP8266/Arduino platform
  5. * for configuration of WiFi credentials using a Captive Portal
  6. *
  7. * @author Creator tzapu
  8. * @author tablatronix
  9. * @version 0.0.0
  10. * @license MIT
  11. */
  12. #include "WiFiManager.h"
  13. #if defined(ESP8266) || defined(ESP32)
  14. #ifdef ESP32
  15. uint8_t WiFiManager::_lastconxresulttmp = WL_IDLE_STATUS;
  16. #endif
  17. /**
  18. * --------------------------------------------------------------------------------
  19. * WiFiManagerParameter
  20. * --------------------------------------------------------------------------------
  21. **/
  22. WiFiManagerParameter::WiFiManagerParameter() {
  23. WiFiManagerParameter("");
  24. }
  25. WiFiManagerParameter::WiFiManagerParameter(const char *custom) {
  26. _id = NULL;
  27. _label = NULL;
  28. _length = 1;
  29. _value = NULL;
  30. _labelPlacement = WFM_LABEL_BEFORE;
  31. _customHTML = custom;
  32. }
  33. WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *label) {
  34. init(id, label, "", 0, "", WFM_LABEL_BEFORE);
  35. }
  36. WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length) {
  37. init(id, label, defaultValue, length, "", WFM_LABEL_BEFORE);
  38. }
  39. WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length, const char *custom) {
  40. init(id, label, defaultValue, length, custom, WFM_LABEL_BEFORE);
  41. }
  42. WiFiManagerParameter::WiFiManagerParameter(const char *id, const char *label, const char *defaultValue, int length, const char *custom, int labelPlacement) {
  43. init(id, label, defaultValue, length, custom, labelPlacement);
  44. }
  45. void WiFiManagerParameter::init(const char *id, const char *label, const char *defaultValue, int length, const char *custom, int labelPlacement) {
  46. _id = id;
  47. _label = label;
  48. _labelPlacement = labelPlacement;
  49. _customHTML = custom;
  50. setValue(defaultValue,length);
  51. }
  52. WiFiManagerParameter::~WiFiManagerParameter() {
  53. if (_value != NULL) {
  54. delete[] _value;
  55. }
  56. _length=0; // setting length 0, ideally the entire parameter should be removed, or added to wifimanager scope so it follows
  57. }
  58. // @note debug is not available in wmparameter class
  59. void WiFiManagerParameter::setValue(const char *defaultValue, int length) {
  60. if(!_id){
  61. // Serial.println("cannot set value of this parameter");
  62. return;
  63. }
  64. // if(strlen(defaultValue) > length){
  65. // // Serial.println("defaultValue length mismatch");
  66. // // return false; //@todo bail
  67. // }
  68. _length = length;
  69. _value = new char[_length + 1];
  70. memset(_value, 0, _length + 1); // explicit null
  71. if (defaultValue != NULL) {
  72. strncpy(_value, defaultValue, _length);
  73. }
  74. }
  75. const char* WiFiManagerParameter::getValue() {
  76. return _value;
  77. }
  78. const char* WiFiManagerParameter::getID() {
  79. return _id;
  80. }
  81. const char* WiFiManagerParameter::getPlaceholder() {
  82. return _label;
  83. }
  84. const char* WiFiManagerParameter::getLabel() {
  85. return _label;
  86. }
  87. int WiFiManagerParameter::getValueLength() {
  88. return _length;
  89. }
  90. int WiFiManagerParameter::getLabelPlacement() {
  91. return _labelPlacement;
  92. }
  93. const char* WiFiManagerParameter::getCustomHTML() {
  94. return _customHTML;
  95. }
  96. /**
  97. * [addParameter description]
  98. * @access public
  99. * @param {[type]} WiFiManagerParameter *p [description]
  100. */
  101. bool WiFiManager::addParameter(WiFiManagerParameter *p) {
  102. // check param id is valid, unless null
  103. if(p->getID()){
  104. for (size_t i = 0; i < strlen(p->getID()); i++){
  105. if(!(isAlphaNumeric(p->getID()[i])) && !(p->getID()[i]=='_')){
  106. DEBUG_WM(DEBUG_ERROR,"[ERROR] parameter IDs can only contain alpha numeric chars");
  107. return false;
  108. }
  109. }
  110. }
  111. // init params if never malloc
  112. if(_params == NULL){
  113. DEBUG_WM(DEBUG_DEV,"allocating params bytes:",_max_params * sizeof(WiFiManagerParameter*));
  114. _params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*));
  115. }
  116. // resize the params array by increment of WIFI_MANAGER_MAX_PARAMS
  117. if(_paramsCount == _max_params){
  118. _max_params += WIFI_MANAGER_MAX_PARAMS;
  119. DEBUG_WM(DEBUG_DEV,F("Updated _max_params:"),_max_params);
  120. DEBUG_WM(DEBUG_DEV,"re-allocating params bytes:",_max_params * sizeof(WiFiManagerParameter*));
  121. WiFiManagerParameter** new_params = (WiFiManagerParameter**)realloc(_params, _max_params * sizeof(WiFiManagerParameter*));
  122. // DEBUG_WM(WIFI_MANAGER_MAX_PARAMS);
  123. // DEBUG_WM(_paramsCount);
  124. // DEBUG_WM(_max_params);
  125. if (new_params != NULL) {
  126. _params = new_params;
  127. } else {
  128. DEBUG_WM(DEBUG_ERROR,"[ERROR] failed to realloc params, size not increased!");
  129. return false;
  130. }
  131. }
  132. _params[_paramsCount] = p;
  133. _paramsCount++;
  134. DEBUG_WM(DEBUG_VERBOSE,"Added Parameter:",p->getID());
  135. return true;
  136. }
  137. /**
  138. * [getParameters description]
  139. * @access public
  140. */
  141. WiFiManagerParameter** WiFiManager::getParameters() {
  142. return _params;
  143. }
  144. /**
  145. * [getParametersCount description]
  146. * @access public
  147. */
  148. int WiFiManager::getParametersCount() {
  149. return _paramsCount;
  150. }
  151. /**
  152. * --------------------------------------------------------------------------------
  153. * WiFiManager
  154. * --------------------------------------------------------------------------------
  155. **/
  156. // constructors
  157. WiFiManager::WiFiManager(Stream& consolePort):_debugPort(consolePort){
  158. WiFiManagerInit();
  159. }
  160. WiFiManager::WiFiManager() {
  161. WiFiManagerInit();
  162. }
  163. void WiFiManager::WiFiManagerInit(){
  164. setMenu(_menuIdsDefault);
  165. if(_debug && _debugLevel > DEBUG_DEV) debugPlatformInfo();
  166. _max_params = WIFI_MANAGER_MAX_PARAMS;
  167. }
  168. // destructor
  169. WiFiManager::~WiFiManager() {
  170. _end();
  171. // parameters
  172. // @todo below belongs to wifimanagerparameter
  173. if (_params != NULL){
  174. DEBUG_WM(DEBUG_DEV,F("freeing allocated params!"));
  175. free(_params);
  176. _params = NULL;
  177. }
  178. // @todo remove event
  179. // #ifdef ESP32
  180. // WiFi.removeEvent(std::bind(&WiFiManager::WiFiEvent,this));
  181. // #endif
  182. DEBUG_WM(DEBUG_DEV,F("unloading"));
  183. }
  184. void WiFiManager::_begin(){
  185. if(_hasBegun) return;
  186. _hasBegun = true;
  187. _usermode = WiFi.getMode();
  188. #ifndef ESP32
  189. WiFi.persistent(false); // disable persistent so scannetworks and mode switching do not cause overwrites
  190. #endif
  191. }
  192. void WiFiManager::_end(){
  193. _hasBegun = false;
  194. if(_userpersistent) WiFi.persistent(true); // reenable persistent, there is no getter we rely on _userpersistent
  195. // if(_usermode != WIFI_OFF) WiFi.mode(_usermode);
  196. }
  197. // AUTOCONNECT
  198. boolean WiFiManager::autoConnect() {
  199. String ssid = getDefaultAPName();
  200. return autoConnect(ssid.c_str(), NULL);
  201. }
  202. /**
  203. * [autoConnect description]
  204. * @access public
  205. * @param {[type]} char const *apName [description]
  206. * @param {[type]} char const *apPassword [description]
  207. * @return {[type]} [description]
  208. */
  209. boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) {
  210. DEBUG_WM(F("AutoConnect"));
  211. _begin();
  212. // attempt to connect using saved settings, on fail fallback to AP config portal
  213. if(!WiFi.enableSTA(true)){
  214. // handle failure mode Brownout detector etc.
  215. DEBUG_WM(DEBUG_ERROR,"[FATAL] Unable to enable wifi!");
  216. return false;
  217. }
  218. WiFiSetCountry();
  219. #ifdef ESP32
  220. if(esp32persistent) WiFi.persistent(false); // disable persistent for esp32 after esp_wifi_start or else saves wont work
  221. #endif
  222. _usermode = WIFI_STA;
  223. // no getter for autoreconnectpolicy before this
  224. // https://github.com/esp8266/Arduino/pull/4359
  225. // so we must force it on else, if not connectimeout then waitforconnectionresult gets stuck endless loop
  226. WiFi_autoReconnect();
  227. // set hostname before stating
  228. if((String)_hostname != ""){
  229. DEBUG_WM(DEBUG_VERBOSE,"Setting hostname:",_hostname);
  230. bool res = true;
  231. #ifdef ESP8266
  232. res = WiFi.hostname(_hostname);
  233. #ifdef ESP8266MDNS_H
  234. DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname");
  235. if(MDNS.begin(_hostname)){
  236. MDNS.addService("http", "tcp", 80);
  237. }
  238. #endif
  239. #elif defined(ESP32)
  240. // @note hostname must be set after STA_START
  241. delay(200); // do not remove, give time for STA_START
  242. res = WiFi.setHostname(_hostname);
  243. #ifdef ESP32MDNS_H
  244. DEBUG_WM(DEBUG_VERBOSE,"Setting MDNS hostname");
  245. if(MDNS.begin(_hostname)){
  246. MDNS.addService("http", "tcp", 80);
  247. }
  248. #endif
  249. #endif
  250. if(!res)DEBUG_WM(DEBUG_ERROR,F("[ERROR] hostname: set failed!"));
  251. if(WiFi.status() == WL_CONNECTED){
  252. DEBUG_WM(DEBUG_VERBOSE,F("reconnecting to set new hostname"));
  253. // WiFi.reconnect(); // This does not reset dhcp
  254. WiFi_Disconnect();
  255. delay(200); // do not remove, need a delay for disconnect to change status()
  256. }
  257. }
  258. // if already connected, or try stored connect
  259. // @note @todo ESP32 has no autoconnect, so connectwifi will always be called unless user called begin etc before
  260. // @todo check if correct ssid == saved ssid when already connected
  261. bool connected = false;
  262. if (WiFi.status() == WL_CONNECTED){
  263. connected = true;
  264. DEBUG_WM(F("AutoConnect: ESP Already Connected"));
  265. setSTAConfig();
  266. }
  267. if(connected || connectWifi("", "") == WL_CONNECTED){
  268. //connected
  269. DEBUG_WM(F("AutoConnect: SUCCESS"));
  270. DEBUG_WM(F("STA IP Address:"),WiFi.localIP());
  271. _lastconxresult = WL_CONNECTED;
  272. if((String)_hostname != ""){
  273. #ifdef ESP8266
  274. DEBUG_WM(DEBUG_DEV,"hostname: STA",WiFi.hostname());
  275. #elif defined(ESP32)
  276. DEBUG_WM(DEBUG_DEV,"hostname: STA",WiFi.getHostname());
  277. #endif
  278. }
  279. return true;
  280. }
  281. // possibly skip the config portal
  282. if (!_enableConfigPortal) {
  283. return false;
  284. }
  285. DEBUG_WM(F("AutoConnect: FAILED"));
  286. // not connected start configportal
  287. return startConfigPortal(apName, apPassword);
  288. }
  289. // CONFIG PORTAL
  290. bool WiFiManager::startAP(){
  291. bool ret = true;
  292. DEBUG_WM(F("StartAP with SSID: "),_apName);
  293. #ifdef ESP8266
  294. // @bug workaround for bug #4372 https://github.com/esp8266/Arduino/issues/4372
  295. if(!WiFi.enableAP(true)) {
  296. DEBUG_WM(DEBUG_ERROR,"[ERROR] enableAP failed!");
  297. return false;
  298. }
  299. delay(500); // workaround delay
  300. #endif
  301. // setup optional soft AP static ip config
  302. if (_ap_static_ip) {
  303. DEBUG_WM(F("Custom AP IP/GW/Subnet:"));
  304. if(!WiFi.softAPConfig(_ap_static_ip, _ap_static_gw, _ap_static_sn)){
  305. DEBUG_WM(DEBUG_ERROR,"[ERROR] softAPConfig failed!");
  306. }
  307. }
  308. //@todo add callback here if needed to modify ap but cannot use setAPStaticIPConfig
  309. //@todo rework wifi channelsync as it will work unpredictably when not connected in sta
  310. int32_t channel = 0;
  311. if(_channelSync) channel = WiFi.channel();
  312. else channel = _apChannel;
  313. if(channel>0){
  314. DEBUG_WM(DEBUG_VERBOSE,"Starting AP on channel:",channel);
  315. }
  316. // start soft AP with password or anonymous
  317. // default channel is 1 here and in esplib, @todo just change to default remove conditionals
  318. if (_apPassword != "") {
  319. if(channel>0){
  320. ret = WiFi.softAP(_apName.c_str(), _apPassword.c_str(),channel,_apHidden);
  321. }
  322. else{
  323. ret = WiFi.softAP(_apName.c_str(), _apPassword.c_str(),1,_apHidden);//password option
  324. }
  325. } else {
  326. DEBUG_WM(DEBUG_VERBOSE,F("AP has anonymous access!"));
  327. if(channel>0){
  328. ret = WiFi.softAP(_apName.c_str(),"",channel,_apHidden);
  329. }
  330. else{
  331. ret = WiFi.softAP(_apName.c_str(),"",1,_apHidden);
  332. }
  333. }
  334. if(_debugLevel >= DEBUG_DEV) debugSoftAPConfig();
  335. if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] There was a problem starting the AP");
  336. // @todo add softAP retry here
  337. delay(500); // slight delay to make sure we get an AP IP
  338. DEBUG_WM(F("AP IP address:"),WiFi.softAPIP());
  339. // set ap hostname
  340. #ifdef ESP32
  341. if(ret && (String)_hostname != ""){
  342. DEBUG_WM(DEBUG_VERBOSE,"setting softAP Hostname:",_hostname);
  343. bool res = WiFi.softAPsetHostname(_hostname);
  344. if(!res)DEBUG_WM(DEBUG_ERROR,F("[ERROR] hostname: AP set failed!"));
  345. DEBUG_WM(DEBUG_DEV,F("hostname: AP"),WiFi.softAPgetHostname());
  346. }
  347. #endif
  348. return ret;
  349. }
  350. /**
  351. * [startWebPortal description]
  352. * @access public
  353. * @return {[type]} [description]
  354. */
  355. void WiFiManager::startWebPortal() {
  356. if(configPortalActive || webPortalActive) return;
  357. setupConfigPortal();
  358. webPortalActive = true;
  359. }
  360. /**
  361. * [stopWebPortal description]
  362. * @access public
  363. * @return {[type]} [description]
  364. */
  365. void WiFiManager::stopWebPortal() {
  366. if(!configPortalActive && !webPortalActive) return;
  367. DEBUG_WM(DEBUG_VERBOSE,F("Stopping Web Portal"));
  368. webPortalActive = false;
  369. shutdownConfigPortal();
  370. }
  371. boolean WiFiManager::configPortalHasTimeout(){
  372. if(_configPortalTimeout == 0 || (_apClientCheck && (WiFi_softap_num_stations() > 0))){
  373. if(millis() - timer > 30000){
  374. timer = millis();
  375. DEBUG_WM(DEBUG_VERBOSE,"NUM CLIENTS: " + (String)WiFi_softap_num_stations());
  376. }
  377. _configPortalStart = millis(); // kludge, bump configportal start time to skew timeouts
  378. return false;
  379. }
  380. // handle timeout
  381. if(_webClientCheck && (_webPortalAccessed>_configPortalStart)>0) _configPortalStart = _webPortalAccessed;
  382. if(millis() > _configPortalStart + _configPortalTimeout){
  383. DEBUG_WM(F("config portal has timed out"));
  384. return true;
  385. } else if(_debugLevel > 0) {
  386. // log timeout
  387. if(_debug){
  388. uint16_t logintvl = 30000; // how often to emit timeing out counter logging
  389. if((millis() - timer) > logintvl){
  390. timer = millis();
  391. DEBUG_WM(DEBUG_VERBOSE,F("Portal Timeout In"),(String)((_configPortalStart + _configPortalTimeout-millis())/1000) + (String)F(" seconds"));
  392. }
  393. }
  394. }
  395. return false;
  396. }
  397. void WiFiManager::setupConfigPortal() {
  398. DEBUG_WM(F("Starting Web Portal"));
  399. // setup dns and web servers
  400. dnsServer.reset(new DNSServer());
  401. server.reset(new WM_WebServer(80));
  402. /* Setup the DNS server redirecting all the domains to the apIP */
  403. dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
  404. // DEBUG_WM("dns server started port: ",DNS_PORT);
  405. DEBUG_WM(DEBUG_DEV,"dns server started with ip: ",WiFi.softAPIP());
  406. dnsServer->start(DNS_PORT, F("*"), WiFi.softAPIP());
  407. // @todo new callback, webserver started, callback cannot override handlers, but can grab them first
  408. if ( _webservercallback != NULL) {
  409. _webservercallback();
  410. }
  411. /* Setup httpd callbacks, web pages: root, wifi config pages, SO captive portal detectors and not found. */
  412. server->on(String(FPSTR(R_root)).c_str(), std::bind(&WiFiManager::handleRoot, this));
  413. server->on(String(FPSTR(R_wifi)).c_str(), std::bind(&WiFiManager::handleWifi, this, true));
  414. server->on(String(FPSTR(R_wifinoscan)).c_str(), std::bind(&WiFiManager::handleWifi, this, false));
  415. server->on(String(FPSTR(R_wifisave)).c_str(), std::bind(&WiFiManager::handleWifiSave, this));
  416. server->on(String(FPSTR(R_info)).c_str(), std::bind(&WiFiManager::handleInfo, this));
  417. server->on(String(FPSTR(R_param)).c_str(), std::bind(&WiFiManager::handleParam, this));
  418. server->on(String(FPSTR(R_paramsave)).c_str(), std::bind(&WiFiManager::handleParamSave, this));
  419. server->on(String(FPSTR(R_restart)).c_str(), std::bind(&WiFiManager::handleReset, this));
  420. server->on(String(FPSTR(R_exit)).c_str(), std::bind(&WiFiManager::handleExit, this));
  421. server->on(String(FPSTR(R_close)).c_str(), std::bind(&WiFiManager::handleClose, this));
  422. server->on(String(FPSTR(R_erase)).c_str(), std::bind(&WiFiManager::handleErase, this, false));
  423. server->on(String(FPSTR(R_status)).c_str(), std::bind(&WiFiManager::handleWiFiStatus, this));
  424. server->onNotFound (std::bind(&WiFiManager::handleNotFound, this));
  425. server->begin(); // Web server start
  426. DEBUG_WM(DEBUG_VERBOSE,F("HTTP server started"));
  427. if(_preloadwifiscan) WiFi_scanNetworks(true,true); // preload wifiscan , async
  428. }
  429. boolean WiFiManager::startConfigPortal() {
  430. String ssid = getDefaultAPName();
  431. return startConfigPortal(ssid.c_str(), NULL);
  432. }
  433. /**
  434. * [startConfigPortal description]
  435. * @access public
  436. * @param {[type]} char const *apName [description]
  437. * @param {[type]} char const *apPassword [description]
  438. * @return {[type]} [description]
  439. */
  440. boolean WiFiManager::startConfigPortal(char const *apName, char const *apPassword) {
  441. _begin();
  442. //setup AP
  443. _apName = apName; // @todo check valid apname ?
  444. _apPassword = apPassword;
  445. DEBUG_WM(DEBUG_VERBOSE,F("Starting Config Portal"));
  446. if(_apName == "") _apName = getDefaultAPName();
  447. if(!validApPassword()) return false;
  448. // HANDLE issues with STA connections, shutdown sta if not connected, or else this will hang channel scanning and softap will not respond
  449. // @todo sometimes still cannot connect to AP for no known reason, no events in log either
  450. if(_disableSTA || (!WiFi.isConnected() && _disableSTAConn)){
  451. // this fixes most ap problems, however, simply doing mode(WIFI_AP) does not work if sta connection is hanging, must `wifi_station_disconnect`
  452. WiFi_Disconnect();
  453. WiFi_enableSTA(false);
  454. DEBUG_WM(DEBUG_VERBOSE,F("Disabling STA"));
  455. }
  456. else {
  457. // @todo even if sta is connected, it is possible that softap connections will fail, IOS says "invalid password", windows says "cannot connect to this network" researching
  458. WiFi_enableSTA(true);
  459. }
  460. // init configportal globals to known states
  461. configPortalActive = true;
  462. bool result = connect = abort = false; // loop flags, connect true success, abort true break
  463. uint8_t state;
  464. _configPortalStart = millis();
  465. // start access point
  466. DEBUG_WM(DEBUG_VERBOSE,F("Enabling AP"));
  467. startAP();
  468. WiFiSetCountry();
  469. // do AP callback if set
  470. if ( _apcallback != NULL) {
  471. _apcallback(this);
  472. }
  473. // init configportal
  474. DEBUG_WM(DEBUG_DEV,F("setupConfigPortal"));
  475. setupConfigPortal();
  476. if(!_configPortalIsBlocking){
  477. DEBUG_WM(DEBUG_VERBOSE,F("Config Portal Running, non blocking/processing"));
  478. return result;
  479. }
  480. DEBUG_WM(DEBUG_VERBOSE,F("Config Portal Running, blocking, waiting for clients..."));
  481. // blocking loop waiting for config
  482. while(1){
  483. // if timed out or abort, break
  484. if(configPortalHasTimeout() || abort){
  485. DEBUG_WM(DEBUG_DEV,F("configportal abort"));
  486. shutdownConfigPortal();
  487. result = abort ? portalAbortResult : portalTimeoutResult; // false, false
  488. break;
  489. }
  490. state = processConfigPortal();
  491. // status change, break
  492. if(state != WL_IDLE_STATUS){
  493. result = (state == WL_CONNECTED); // true if connected
  494. break;
  495. }
  496. yield(); // watchdog
  497. }
  498. DEBUG_WM(DEBUG_NOTIFY,F("config portal exiting"));
  499. return result;
  500. }
  501. /**
  502. * [process description]
  503. * @access public
  504. * @return {[type]} [description]
  505. */
  506. boolean WiFiManager::process(){
  507. if(webPortalActive || (configPortalActive && !_configPortalIsBlocking)){
  508. uint8_t state = processConfigPortal();
  509. return state == WL_CONNECTED;
  510. }
  511. return false;
  512. }
  513. //using esp enums returns for now, should be fine
  514. uint8_t WiFiManager::processConfigPortal(){
  515. //DNS handler
  516. dnsServer->processNextRequest();
  517. //HTTP handler
  518. server->handleClient();
  519. // Waiting for save...
  520. if(connect) {
  521. connect = false;
  522. DEBUG_WM(DEBUG_VERBOSE,F("process connect"));
  523. if(_enableCaptivePortal) delay(_cpclosedelay); // keeps the captiveportal from closing to fast.
  524. // skip wifi if no ssid
  525. if(_ssid == ""){
  526. DEBUG_WM(DEBUG_VERBOSE,F("No ssid, skipping wifi save"));
  527. }
  528. else{
  529. // attempt sta connection to submitted _ssid, _pass
  530. if (connectWifi(_ssid, _pass) == WL_CONNECTED) {
  531. DEBUG_WM(F("Connect to new AP [SUCCESS]"));
  532. DEBUG_WM(F("Got IP Address:"));
  533. DEBUG_WM(WiFi.localIP());
  534. if ( _savewificallback != NULL) {
  535. _savewificallback();
  536. }
  537. shutdownConfigPortal();
  538. return WL_CONNECTED; // CONNECT SUCCESS
  539. }
  540. DEBUG_WM(DEBUG_ERROR,F("[ERROR] Connect to new AP Failed"));
  541. }
  542. if (_shouldBreakAfterConfig) {
  543. // do save callback
  544. // @todo this is more of an exiting callback than a save, clarify when this should actually occur
  545. // confirm or verify data was saved to make this more accurate callback
  546. if ( _savewificallback != NULL) {
  547. _savewificallback();
  548. }
  549. shutdownConfigPortal();
  550. return WL_CONNECT_FAILED; // CONNECT FAIL
  551. }
  552. else{
  553. // clear save strings
  554. _ssid = "";
  555. _pass = "";
  556. // if connect fails, turn sta off to stabilize AP
  557. WiFi_Disconnect();
  558. WiFi_enableSTA(false);
  559. DEBUG_WM(DEBUG_VERBOSE,F("Disabling STA"));
  560. }
  561. }
  562. return WL_IDLE_STATUS;
  563. }
  564. /**
  565. * [shutdownConfigPortal description]
  566. * @access public
  567. * @return bool success (softapdisconnect)
  568. */
  569. bool WiFiManager::shutdownConfigPortal(){
  570. if(webPortalActive) return false;
  571. //DNS handler
  572. dnsServer->processNextRequest();
  573. //HTTP handler
  574. server->handleClient();
  575. // @todo what is the proper way to shutdown and free the server up
  576. server->stop();
  577. server.reset();
  578. dnsServer->stop(); // free heap ?
  579. dnsServer.reset();
  580. WiFi.scanDelete(); // free wifi scan results
  581. if(!configPortalActive) return false;
  582. // turn off AP
  583. // @todo bug workaround
  584. // https://github.com/esp8266/Arduino/issues/3793
  585. // [APdisconnect] set_config failed! *WM: disconnect configportal - softAPdisconnect failed
  586. // still no way to reproduce reliably
  587. DEBUG_WM(DEBUG_VERBOSE,F("disconnect configportal"));
  588. bool ret = false;
  589. ret = WiFi.softAPdisconnect(false);
  590. if(!ret)DEBUG_WM(DEBUG_ERROR,F("[ERROR] disconnect configportal - softAPdisconnect FAILED"));
  591. delay(1000);
  592. DEBUG_WM(DEBUG_VERBOSE,"restoring usermode",getModeString(_usermode));
  593. WiFi_Mode(_usermode); // restore users wifi mode, BUG https://github.com/esp8266/Arduino/issues/4372
  594. if(WiFi.status()==WL_IDLE_STATUS){
  595. WiFi.reconnect(); // restart wifi since we disconnected it in startconfigportal
  596. DEBUG_WM(DEBUG_VERBOSE,"WiFi Reconnect, was idle");
  597. }
  598. DEBUG_WM(DEBUG_VERBOSE,"wifi status:",getWLStatusString(WiFi.status()));
  599. DEBUG_WM(DEBUG_VERBOSE,"wifi mode:",getModeString(WiFi.getMode()));
  600. configPortalActive = false;
  601. _end();
  602. return ret;
  603. }
  604. // @todo refactor this up into seperate functions
  605. // one for connecting to flash , one for new client
  606. // clean up, flow is convoluted, and causes bugs
  607. uint8_t WiFiManager::connectWifi(String ssid, String pass) {
  608. DEBUG_WM(DEBUG_VERBOSE,F("Connecting as wifi client..."));
  609. uint8_t connRes = (uint8_t)WL_NO_SSID_AVAIL;
  610. setSTAConfig();
  611. //@todo catch failures in set_config
  612. // make sure sta is on before `begin` so it does not call enablesta->mode while persistent is ON ( which would save WM AP state to eeprom !)
  613. if(_cleanConnect) WiFi_Disconnect(); // disconnect before begin, in case anything is hung, this causes a 2 seconds delay for connect
  614. // @todo find out what status is when this is needed, can we detect it and handle it, say in between states or idle_status
  615. // if ssid argument provided connect to that
  616. if (ssid != "") {
  617. wifiConnectNew(ssid,pass);
  618. if(_saveTimeout > 0){
  619. connRes = waitForConnectResult(_saveTimeout); // use default save timeout for saves to prevent bugs in esp->waitforconnectresult loop
  620. }
  621. else {
  622. connRes = waitForConnectResult(0);
  623. }
  624. }
  625. else {
  626. // connect using saved ssid if there is one
  627. if (WiFi_hasAutoConnect()) {
  628. wifiConnectDefault();
  629. connRes = waitForConnectResult();
  630. }
  631. else {
  632. DEBUG_WM(F("No saved credentials, skipping wifi"));
  633. }
  634. }
  635. DEBUG_WM(DEBUG_VERBOSE,F("Connection result:"),getWLStatusString(connRes));
  636. // WPS enabled? https://github.com/esp8266/Arduino/pull/4889
  637. #ifdef NO_EXTRA_4K_HEAP
  638. // do WPS, if WPS options enabled and not connected and no password was supplied
  639. // @todo this seems like wrong place for this, is it a fallback or option?
  640. if (_tryWPS && connRes != WL_CONNECTED && pass == "") {
  641. startWPS();
  642. // should be connected at the end of WPS
  643. connRes = waitForConnectResult();
  644. }
  645. #endif
  646. if(connRes != WL_SCAN_COMPLETED){
  647. updateConxResult(connRes);
  648. }
  649. return connRes;
  650. }
  651. /**
  652. * connect to a new wifi ap
  653. * @since $dev
  654. * @param String ssid
  655. * @param String pass
  656. * @return bool success
  657. */
  658. bool WiFiManager::wifiConnectNew(String ssid, String pass){
  659. bool ret = false;
  660. DEBUG_WM(F("CONNECTED:"),WiFi.status() == WL_CONNECTED);
  661. DEBUG_WM(F("Connecting to NEW AP:"),ssid);
  662. DEBUG_WM(DEBUG_DEV,F("Using Password:"),pass);
  663. WiFi_enableSTA(true,storeSTAmode); // storeSTAmode will also toggle STA on in default opmode (persistent) if true (default)
  664. WiFi.persistent(true);
  665. ret = WiFi.begin(ssid.c_str(), pass.c_str());
  666. WiFi.persistent(false);
  667. if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi begin failed");
  668. return ret;
  669. }
  670. /**
  671. * connect to stored wifi
  672. * @since dev
  673. * @return bool success
  674. */
  675. bool WiFiManager::wifiConnectDefault(){
  676. bool ret = false;
  677. DEBUG_WM(F("Connecting to SAVED AP:"),WiFi_SSID(true));
  678. DEBUG_WM(DEBUG_DEV,F("Using Password:"),WiFi_psk(true));
  679. ret = WiFi_enableSTA(true,storeSTAmode);
  680. if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi enableSta failed");
  681. ret = WiFi.begin();
  682. if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi begin failed");
  683. return ret;
  684. }
  685. /**
  686. * set sta config if set
  687. * @since $dev
  688. * @return bool success
  689. */
  690. bool WiFiManager::setSTAConfig(){
  691. DEBUG_WM(DEBUG_DEV,F("STA static IP:"),_sta_static_ip);
  692. bool ret = true;
  693. if (_sta_static_ip) {
  694. DEBUG_WM(DEBUG_VERBOSE,F("Custom static IP/GW/Subnet/DNS"));
  695. if(_sta_static_dns) {
  696. DEBUG_WM(DEBUG_VERBOSE,F("Custom static DNS"));
  697. ret = WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn, _sta_static_dns);
  698. }
  699. else {
  700. DEBUG_WM(DEBUG_VERBOSE,F("Custom STA IP/GW/Subnet"));
  701. ret = WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn);
  702. }
  703. if(!ret) DEBUG_WM(DEBUG_ERROR,"[ERROR] wifi config failed");
  704. else DEBUG_WM(F("STA IP set:"),WiFi.localIP());
  705. } else {
  706. DEBUG_WM(DEBUG_VERBOSE,F("setSTAConfig static ip not set, skipping"));
  707. }
  708. return ret;
  709. }
  710. // @todo change to getLastFailureReason and do not touch conxresult
  711. void WiFiManager::updateConxResult(uint8_t status){
  712. // hack in wrong password detection
  713. _lastconxresult = status;
  714. #ifdef ESP8266
  715. if(_lastconxresult == WL_CONNECT_FAILED){
  716. if(wifi_station_get_connect_status() == STATION_WRONG_PASSWORD){
  717. _lastconxresult = WL_STATION_WRONG_PASSWORD;
  718. }
  719. }
  720. #elif defined(ESP32)
  721. // if(_lastconxresult == WL_CONNECT_FAILED){
  722. if(_lastconxresult == WL_CONNECT_FAILED || _lastconxresult == WL_DISCONNECTED){
  723. DEBUG_WM(DEBUG_DEV,"lastconxresulttmp:",getWLStatusString(_lastconxresulttmp));
  724. if(_lastconxresulttmp != WL_IDLE_STATUS){
  725. _lastconxresult = _lastconxresulttmp;
  726. // _lastconxresulttmp = WL_IDLE_STATUS;
  727. }
  728. }
  729. #endif
  730. DEBUG_WM(DEBUG_DEV,"lastconxresult:",getWLStatusString(_lastconxresult));
  731. }
  732. uint8_t WiFiManager::waitForConnectResult() {
  733. if(_connectTimeout > 0) DEBUG_WM(DEBUG_VERBOSE,_connectTimeout,F("ms connectTimeout set"));
  734. return waitForConnectResult(_connectTimeout);
  735. }
  736. /**
  737. * waitForConnectResult
  738. * @param uint16_t timeout in seconds
  739. * @return uint8_t WL Status
  740. */
  741. uint8_t WiFiManager::waitForConnectResult(uint16_t timeout) {
  742. if (timeout == 0){
  743. DEBUG_WM(F("connectTimeout not set, ESP waitForConnectResult..."));
  744. return WiFi.waitForConnectResult();
  745. }
  746. unsigned long timeoutmillis = millis() + timeout;
  747. DEBUG_WM(DEBUG_VERBOSE,timeout,F("ms timeout, waiting for connect..."));
  748. uint8_t status = WiFi.status();
  749. while(millis() < timeoutmillis) {
  750. status = WiFi.status();
  751. // @todo detect additional states, connect happens, then dhcp then get ip, there is some delay here, make sure not to timeout if waiting on IP
  752. if (status == WL_CONNECTED || status == WL_CONNECT_FAILED) {
  753. return status;
  754. }
  755. DEBUG_WM (DEBUG_VERBOSE,F("."));
  756. delay(100);
  757. }
  758. return status;
  759. }
  760. // WPS enabled? https://github.com/esp8266/Arduino/pull/4889
  761. #ifdef NO_EXTRA_4K_HEAP
  762. void WiFiManager::startWPS() {
  763. DEBUG_WM(F("START WPS"));
  764. #ifdef ESP8266
  765. WiFi.beginWPSConfig();
  766. #else
  767. // @todo
  768. #endif
  769. DEBUG_WM(F("END WPS"));
  770. }
  771. #endif
  772. String WiFiManager::getHTTPHead(String title){
  773. String page;
  774. page += FPSTR(HTTP_HEAD_START);
  775. page.replace(FPSTR(T_v), title);
  776. page += FPSTR(HTTP_SCRIPT);
  777. page += FPSTR(HTTP_STYLE);
  778. page += _customHeadElement;
  779. if(_bodyClass != ""){
  780. String p = FPSTR(HTTP_HEAD_END);
  781. p.replace(FPSTR(T_c), _bodyClass); // add class str
  782. page += p;
  783. }
  784. else {
  785. page += FPSTR(HTTP_HEAD_END);
  786. }
  787. return page;
  788. }
  789. /**
  790. * HTTPD handler for page requests
  791. */
  792. void WiFiManager::handleRequest() {
  793. _webPortalAccessed = millis();
  794. }
  795. /**
  796. * HTTPD CALLBACK root or redirect to captive portal
  797. */
  798. void WiFiManager::handleRoot() {
  799. DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Root"));
  800. if (captivePortal()) return; // If captive portal redirect instead of displaying the page
  801. handleRequest();
  802. String page = getHTTPHead(FPSTR(S_options)); // @token options
  803. String str = FPSTR(HTTP_ROOT_MAIN);
  804. str.replace(FPSTR(T_v),configPortalActive ? _apName : WiFi.localIP().toString()); // use ip if ap is not active for heading
  805. page += str;
  806. page += FPSTR(HTTP_PORTAL_OPTIONS);
  807. page += getMenuOut();
  808. reportStatus(page);
  809. page += FPSTR(HTTP_END);
  810. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
  811. server->send(200, FPSTR(HTTP_HEAD_CT), page);
  812. // server->close(); // testing reliability fix for content length mismatches during mutiple flood hits WiFi_scanNetworks(); // preload wifiscan
  813. if(_preloadwifiscan) WiFi_scanNetworks(_scancachetime,true); // preload wifiscan throttled, async
  814. // @todo buggy, captive portals make a query on every page load, causing this to run every time in addition to the real page load
  815. // I dont understand why, when you are already in the captive portal, I guess they want to know that its still up and not done or gone
  816. // if we can detect these and ignore them that would be great, since they come from the captive portal redirect maybe there is a refferer
  817. }
  818. /**
  819. * HTTPD CALLBACK Wifi config page handler
  820. */
  821. void WiFiManager::handleWifi(boolean scan) {
  822. DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Wifi"));
  823. handleRequest();
  824. String page = getHTTPHead(FPSTR(S_titlewifi)); // @token titlewifi
  825. if (scan) {
  826. // DEBUG_WM(DEBUG_DEV,"refresh flag:",server->hasArg(F("refresh")));
  827. WiFi_scanNetworks(server->hasArg(F("refresh")),false); //wifiscan, force if arg refresh
  828. page += getScanItemOut();
  829. }
  830. String pitem = "";
  831. pitem = FPSTR(HTTP_FORM_START);
  832. pitem.replace(FPSTR(T_v), F("wifisave")); // set form action
  833. page += pitem;
  834. pitem = FPSTR(HTTP_FORM_WIFI);
  835. pitem.replace(FPSTR(T_v), WiFi_SSID());
  836. if(_showPassword){
  837. pitem.replace(FPSTR(T_p), WiFi_psk());
  838. }
  839. else {
  840. pitem.replace(FPSTR(T_p),FPSTR(S_passph));
  841. }
  842. page += pitem;
  843. page += getStaticOut();
  844. page += FPSTR(HTTP_FORM_WIFI_END);
  845. if(_paramsInWifi && _paramsCount>0){
  846. page += FPSTR(HTTP_FORM_PARAM_HEAD);
  847. page += getParamOut();
  848. }
  849. page += FPSTR(HTTP_FORM_END);
  850. page += FPSTR(HTTP_SCAN_LINK);
  851. reportStatus(page);
  852. page += FPSTR(HTTP_END);
  853. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
  854. server->send(200, FPSTR(HTTP_HEAD_CT), page);
  855. // server->close(); // testing reliability fix for content length mismatches during mutiple flood hits
  856. DEBUG_WM(DEBUG_DEV,F("Sent config page"));
  857. }
  858. /**
  859. * HTTPD CALLBACK Wifi param page handler
  860. */
  861. void WiFiManager::handleParam(){
  862. DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Param"));
  863. handleRequest();
  864. String page = getHTTPHead(FPSTR(S_titleparam)); // @token titlewifi
  865. String pitem = "";
  866. pitem = FPSTR(HTTP_FORM_START);
  867. pitem.replace(FPSTR(T_v), F("paramsave"));
  868. page += pitem;
  869. page += getParamOut();
  870. page += FPSTR(HTTP_FORM_END);
  871. reportStatus(page);
  872. page += FPSTR(HTTP_END);
  873. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
  874. server->send(200, FPSTR(HTTP_HEAD_CT), page);
  875. DEBUG_WM(DEBUG_DEV,F("Sent param page"));
  876. }
  877. String WiFiManager::getMenuOut(){
  878. String page;
  879. for(auto menuId :_menuIds ){
  880. if(((String)menuId == "param") && (_paramsCount == 0)) continue; // no params set, omit params from menu, @todo this may be undesired by someone
  881. page += HTTP_PORTAL_MENU[menuId];
  882. }
  883. return page;
  884. }
  885. // // is it possible in softap mode to detect aps without scanning
  886. // bool WiFiManager::WiFi_scanNetworksForAP(bool force){
  887. // WiFi_scanNetworks(force);
  888. // }
  889. void WiFiManager::WiFi_scanComplete(int networksFound){
  890. _lastscan = millis();
  891. _numNetworks = networksFound;
  892. DEBUG_WM(DEBUG_VERBOSE,F("WiFi Scan ASYNC completed"), "in "+(String)(_lastscan - _startscan)+" ms");
  893. DEBUG_WM(DEBUG_VERBOSE,F("WiFi Scan ASYNC found:"),_numNetworks);
  894. }
  895. bool WiFiManager::WiFi_scanNetworks(){
  896. return WiFi_scanNetworks(false,false);
  897. }
  898. bool WiFiManager::WiFi_scanNetworks(unsigned int cachetime,bool async){
  899. return WiFi_scanNetworks(millis()-_lastscan > cachetime,async);
  900. }
  901. bool WiFiManager::WiFi_scanNetworks(unsigned int cachetime){
  902. return WiFi_scanNetworks(millis()-_lastscan > cachetime,false);
  903. }
  904. bool WiFiManager::WiFi_scanNetworks(bool force,bool async){
  905. // DEBUG_WM(DEBUG_DEV,"scanNetworks async:",async == true);
  906. // DEBUG_WM(DEBUG_DEV,_numNetworks,(millis()-_lastscan ));
  907. // DEBUG_WM(DEBUG_DEV,"scanNetworks force:",force == true);
  908. if(force || _numNetworks == 0 || (millis()-_lastscan > 60000)){
  909. int8_t res;
  910. _startscan = millis();
  911. if(async && _asyncScan){
  912. #ifdef ESP8266
  913. #ifndef WM_NOASYNC // no async available < 2.4.0
  914. DEBUG_WM(DEBUG_VERBOSE,F("WiFi Scan ASYNC started"));
  915. using namespace std::placeholders; // for `_1`
  916. WiFi.scanNetworksAsync(std::bind(&WiFiManager::WiFi_scanComplete,this,_1));
  917. #else
  918. res = WiFi.scanNetworks();
  919. #endif
  920. #else
  921. DEBUG_WM(DEBUG_VERBOSE,F("WiFi Scan ASYNC started"));
  922. res = WiFi.scanNetworks(true);
  923. #endif
  924. return false;
  925. }
  926. else{
  927. res = WiFi.scanNetworks();
  928. }
  929. if(res == WIFI_SCAN_FAILED) DEBUG_WM(DEBUG_ERROR,"[ERROR] scan failed");
  930. else if(res == WIFI_SCAN_RUNNING){
  931. DEBUG_WM(DEBUG_ERROR,"[ERROR] scan waiting");
  932. while(WiFi.scanComplete() == WIFI_SCAN_RUNNING){
  933. DEBUG_WM(DEBUG_ERROR,".");
  934. delay(100);
  935. }
  936. _numNetworks = WiFi.scanComplete();
  937. }
  938. else if(res >=0 ) _numNetworks = res;
  939. _lastscan = millis();
  940. DEBUG_WM(DEBUG_VERBOSE,F("WiFi Scan completed"), "in "+(String)(_lastscan - _startscan)+" ms");
  941. return true;
  942. } else DEBUG_WM(DEBUG_VERBOSE,"Scan is cached",(String)(millis()-_lastscan )+" ms ago");
  943. return false;
  944. }
  945. String WiFiManager::WiFiManager::getScanItemOut(){
  946. String page;
  947. if(!_numNetworks) WiFi_scanNetworks(); // scan in case this gets called before any scans
  948. int n = _numNetworks;
  949. if (n == 0) {
  950. DEBUG_WM(F("No networks found"));
  951. page += FPSTR(S_nonetworks); // @token nonetworks
  952. }
  953. else {
  954. DEBUG_WM(n,F("networks found"));
  955. //sort networks
  956. int indices[n];
  957. for (int i = 0; i < n; i++) {
  958. indices[i] = i;
  959. }
  960. // RSSI SORT
  961. for (int i = 0; i < n; i++) {
  962. for (int j = i + 1; j < n; j++) {
  963. if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) {
  964. std::swap(indices[i], indices[j]);
  965. }
  966. }
  967. }
  968. /* test std:sort
  969. std::sort(indices, indices + n, [](const int & a, const int & b) -> bool
  970. {
  971. return WiFi.RSSI(a) > WiFi.RSSI(b);
  972. });
  973. */
  974. // remove duplicates ( must be RSSI sorted )
  975. if (_removeDuplicateAPs) {
  976. String cssid;
  977. for (int i = 0; i < n; i++) {
  978. if (indices[i] == -1) continue;
  979. cssid = WiFi.SSID(indices[i]);
  980. for (int j = i + 1; j < n; j++) {
  981. if (cssid == WiFi.SSID(indices[j])) {
  982. DEBUG_WM(DEBUG_VERBOSE,F("DUP AP:"),WiFi.SSID(indices[j]));
  983. indices[j] = -1; // set dup aps to index -1
  984. }
  985. }
  986. }
  987. }
  988. // token precheck, to speed up replacements on large ap lists
  989. String HTTP_ITEM_STR = FPSTR(HTTP_ITEM);
  990. // toggle icons with percentage
  991. HTTP_ITEM_STR.replace("{qp}", FPSTR(HTTP_ITEM_QP));
  992. HTTP_ITEM_STR.replace("{h}",_scanDispOptions ? "" : "h");
  993. HTTP_ITEM_STR.replace("{qi}", FPSTR(HTTP_ITEM_QI));
  994. HTTP_ITEM_STR.replace("{h}",_scanDispOptions ? "h" : "");
  995. // set token precheck flags
  996. bool tok_r = HTTP_ITEM_STR.indexOf(FPSTR(T_r)) > 0;
  997. bool tok_R = HTTP_ITEM_STR.indexOf(FPSTR(T_R)) > 0;
  998. bool tok_e = HTTP_ITEM_STR.indexOf(FPSTR(T_e)) > 0;
  999. bool tok_q = HTTP_ITEM_STR.indexOf(FPSTR(T_q)) > 0;
  1000. bool tok_i = HTTP_ITEM_STR.indexOf(FPSTR(T_i)) > 0;
  1001. //display networks in page
  1002. for (int i = 0; i < n; i++) {
  1003. if (indices[i] == -1) continue; // skip dups
  1004. DEBUG_WM(DEBUG_VERBOSE,F("AP: "),(String)WiFi.RSSI(indices[i]) + " " + (String)WiFi.SSID(indices[i]));
  1005. int rssiperc = getRSSIasQuality(WiFi.RSSI(indices[i]));
  1006. uint8_t enc_type = WiFi.encryptionType(indices[i]);
  1007. if (_minimumQuality == -1 || _minimumQuality < rssiperc) {
  1008. String item = HTTP_ITEM_STR;
  1009. item.replace(FPSTR(T_v), htmlEntities(WiFi.SSID(indices[i]))); // ssid no encoding
  1010. if(tok_e) item.replace(FPSTR(T_e), encryptionTypeStr(enc_type));
  1011. if(tok_r) item.replace(FPSTR(T_r), (String)rssiperc); // rssi percentage 0-100
  1012. if(tok_R) item.replace(FPSTR(T_R), (String)WiFi.RSSI(indices[i])); // rssi db
  1013. if(tok_q) item.replace(FPSTR(T_q), (String)int(round(map(rssiperc,0,100,1,4)))); //quality icon 1-4
  1014. if(tok_i){
  1015. if (enc_type != WM_WIFIOPEN) {
  1016. item.replace(FPSTR(T_i), F("l"));
  1017. } else {
  1018. item.replace(FPSTR(T_i), "");
  1019. }
  1020. }
  1021. //DEBUG_WM(item);
  1022. page += item;
  1023. delay(0);
  1024. } else {
  1025. DEBUG_WM(DEBUG_VERBOSE,F("Skipping , does not meet _minimumQuality"));
  1026. }
  1027. }
  1028. page += FPSTR(HTTP_BR);
  1029. }
  1030. return page;
  1031. }
  1032. String WiFiManager::getIpForm(String id, String title, String value){
  1033. String item = FPSTR(HTTP_FORM_LABEL);
  1034. item += FPSTR(HTTP_FORM_PARAM);
  1035. item.replace(FPSTR(T_i), id);
  1036. item.replace(FPSTR(T_n), id);
  1037. item.replace(FPSTR(T_p), FPSTR(T_t));
  1038. // item.replace(FPSTR(T_p), default);
  1039. item.replace(FPSTR(T_t), title);
  1040. item.replace(FPSTR(T_l), F("15"));
  1041. item.replace(FPSTR(T_v), value);
  1042. item.replace(FPSTR(T_c), "");
  1043. return item;
  1044. }
  1045. String WiFiManager::getStaticOut(){
  1046. String page;
  1047. if ((_staShowStaticFields || _sta_static_ip) && _staShowStaticFields>=0) {
  1048. DEBUG_WM(DEBUG_DEV,"_staShowStaticFields");
  1049. page += FPSTR(HTTP_FORM_STATIC_HEAD);
  1050. // @todo how can we get these accurate settings from memory , wifi_get_ip_info does not seem to reveal if struct ip_info is static or not
  1051. page += getIpForm(FPSTR(S_ip),FPSTR(S_staticip),(_sta_static_ip ? _sta_static_ip.toString() : "")); // @token staticip
  1052. // WiFi.localIP().toString();
  1053. page += getIpForm(FPSTR(S_gw),FPSTR(S_staticgw),(_sta_static_gw ? _sta_static_gw.toString() : "")); // @token staticgw
  1054. // WiFi.gatewayIP().toString();
  1055. page += getIpForm(FPSTR(S_sn),FPSTR(S_subnet),(_sta_static_sn ? _sta_static_sn.toString() : "")); // @token subnet
  1056. // WiFi.subnetMask().toString();
  1057. }
  1058. if((_staShowDns || _sta_static_dns) && _staShowDns>=0){
  1059. page += getIpForm(FPSTR(S_dns),FPSTR(S_staticdns),(_sta_static_dns ? _sta_static_dns.toString() : "")); // @token dns
  1060. }
  1061. if(page!="") page += FPSTR(HTTP_BR); // @todo remove these, use css
  1062. return page;
  1063. }
  1064. String WiFiManager::getParamOut(){
  1065. String page;
  1066. if(_paramsCount > 0){
  1067. String HTTP_PARAM_temp = FPSTR(HTTP_FORM_LABEL);
  1068. HTTP_PARAM_temp += FPSTR(HTTP_FORM_PARAM);
  1069. bool tok_I = HTTP_PARAM_temp.indexOf(FPSTR(T_I)) > 0;
  1070. bool tok_i = HTTP_PARAM_temp.indexOf(FPSTR(T_i)) > 0;
  1071. bool tok_n = HTTP_PARAM_temp.indexOf(FPSTR(T_n)) > 0;
  1072. bool tok_p = HTTP_PARAM_temp.indexOf(FPSTR(T_p)) > 0;
  1073. bool tok_t = HTTP_PARAM_temp.indexOf(FPSTR(T_t)) > 0;
  1074. bool tok_l = HTTP_PARAM_temp.indexOf(FPSTR(T_l)) > 0;
  1075. bool tok_v = HTTP_PARAM_temp.indexOf(FPSTR(T_v)) > 0;
  1076. bool tok_c = HTTP_PARAM_temp.indexOf(FPSTR(T_c)) > 0;
  1077. char valLength[5];
  1078. // add the extra parameters to the form
  1079. for (int i = 0; i < _paramsCount; i++) {
  1080. if (_params[i] == NULL || _params[i]->_length == 0) {
  1081. DEBUG_WM(DEBUG_ERROR,"[ERROR] WiFiManagerParameter is out of scope");
  1082. break;
  1083. }
  1084. // label before or after, @todo this could be done via floats or CSS and eliminated
  1085. String pitem;
  1086. switch (_params[i]->getLabelPlacement()) {
  1087. case WFM_LABEL_BEFORE:
  1088. pitem = FPSTR(HTTP_FORM_LABEL);
  1089. pitem += FPSTR(HTTP_FORM_PARAM);
  1090. break;
  1091. case WFM_LABEL_AFTER:
  1092. pitem = FPSTR(HTTP_FORM_PARAM);
  1093. pitem += FPSTR(HTTP_FORM_LABEL);
  1094. break;
  1095. default:
  1096. // WFM_NO_LABEL
  1097. pitem = FPSTR(HTTP_FORM_PARAM);
  1098. break;
  1099. }
  1100. // Input templating
  1101. // "<br/><input id='{i}' name='{n}' maxlength='{l}' value='{v}' {c}>";
  1102. // if no ID use customhtml for item, else generate from param string
  1103. if (_params[i]->getID() != NULL) {
  1104. if(tok_I)pitem.replace(FPSTR(T_I), (String)FPSTR(S_parampre)+(String)i); // T_I id number
  1105. if(tok_i)pitem.replace(FPSTR(T_i), _params[i]->getID()); // T_i id name
  1106. if(tok_n)pitem.replace(FPSTR(T_n), _params[i]->getID()); // T_n id name alias
  1107. if(tok_p)pitem.replace(FPSTR(T_p), FPSTR(T_t)); // T_p replace legacy placeholder token
  1108. if(tok_t)pitem.replace(FPSTR(T_t), _params[i]->getLabel()); // T_t title/label
  1109. snprintf(valLength, 5, "%d", _params[i]->getValueLength());
  1110. if(tok_l)pitem.replace(FPSTR(T_l), valLength); // T_l value length
  1111. if(tok_v)pitem.replace(FPSTR(T_v), _params[i]->getValue()); // T_v value
  1112. if(tok_c)pitem.replace(FPSTR(T_c), _params[i]->getCustomHTML()); // T_c meant for additional attributes, not html, but can stuff
  1113. } else {
  1114. pitem = _params[i]->getCustomHTML();
  1115. }
  1116. page += pitem;
  1117. }
  1118. }
  1119. return page;
  1120. }
  1121. void WiFiManager::handleWiFiStatus(){
  1122. DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP WiFi status "));
  1123. handleRequest();
  1124. String page;
  1125. // String page = "{\"result\":true,\"count\":1}";
  1126. #ifdef JSTEST
  1127. page = FPSTR(HTTP_JS);
  1128. #endif
  1129. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
  1130. server->send(200, FPSTR(HTTP_HEAD_CT), page);
  1131. }
  1132. /**
  1133. * HTTPD CALLBACK save form and redirect to WLAN config page again
  1134. */
  1135. void WiFiManager::handleWifiSave() {
  1136. DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP WiFi save "));
  1137. DEBUG_WM(DEBUG_DEV,F("Method:"),server->method() == HTTP_GET ? (String)FPSTR(S_GET) : (String)FPSTR(S_POST));
  1138. handleRequest();
  1139. // @todo use new callback for before paramsaves
  1140. if ( _presavecallback != NULL) {
  1141. _presavecallback();
  1142. }
  1143. //SAVE/connect here
  1144. _ssid = server->arg(F("s")).c_str();
  1145. _pass = server->arg(F("p")).c_str();
  1146. if(_paramsInWifi) doParamSave();
  1147. if (server->arg(FPSTR(S_ip)) != "") {
  1148. //_sta_static_ip.fromString(server->arg(FPSTR(S_ip));
  1149. String ip = server->arg(FPSTR(S_ip));
  1150. optionalIPFromString(&_sta_static_ip, ip.c_str());
  1151. DEBUG_WM(DEBUG_DEV,F("static ip:"),ip);
  1152. }
  1153. if (server->arg(FPSTR(S_gw)) != "") {
  1154. String gw = server->arg(FPSTR(S_gw));
  1155. optionalIPFromString(&_sta_static_gw, gw.c_str());
  1156. DEBUG_WM(DEBUG_DEV,F("static gateway:"),gw);
  1157. }
  1158. if (server->arg(FPSTR(S_sn)) != "") {
  1159. String sn = server->arg(FPSTR(S_sn));
  1160. optionalIPFromString(&_sta_static_sn, sn.c_str());
  1161. DEBUG_WM(DEBUG_DEV,F("static netmask:"),sn);
  1162. }
  1163. if (server->arg(FPSTR(S_dns)) != "") {
  1164. String dns = server->arg(FPSTR(S_dns));
  1165. optionalIPFromString(&_sta_static_dns, dns.c_str());
  1166. DEBUG_WM(DEBUG_DEV,F("static DNS:"),dns);
  1167. }
  1168. String page;
  1169. if(_ssid == ""){
  1170. page = getHTTPHead(FPSTR(S_titlewifisettings)); // @token titleparamsaved
  1171. page += FPSTR(HTTP_PARAMSAVED);
  1172. }
  1173. else {
  1174. page = getHTTPHead(FPSTR(S_titlewifisaved)); // @token titlewifisaved
  1175. page += FPSTR(HTTP_SAVED);
  1176. }
  1177. page += FPSTR(HTTP_END);
  1178. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
  1179. server->sendHeader(FPSTR(HTTP_HEAD_CORS), FPSTR(HTTP_HEAD_CORS_ALLOW_ALL));
  1180. server->send(200, FPSTR(HTTP_HEAD_CT), page);
  1181. DEBUG_WM(DEBUG_DEV,F("Sent wifi save page"));
  1182. connect = true; //signal ready to connect/reset process in processConfigPortal
  1183. }
  1184. void WiFiManager::handleParamSave() {
  1185. DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP WiFi save "));
  1186. DEBUG_WM(DEBUG_DEV,F("Method:"),server->method() == HTTP_GET ? (String)FPSTR(S_GET) : (String)FPSTR(S_POST));
  1187. handleRequest();
  1188. doParamSave();
  1189. String page = getHTTPHead(FPSTR(S_titleparamsaved)); // @token titleparamsaved
  1190. page += FPSTR(HTTP_PARAMSAVED);
  1191. page += FPSTR(HTTP_END);
  1192. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
  1193. server->send(200, FPSTR(HTTP_HEAD_CT), page);
  1194. DEBUG_WM(DEBUG_DEV,F("Sent param save page"));
  1195. }
  1196. void WiFiManager::doParamSave(){
  1197. // @todo use new callback for before paramsaves, is this really needed?
  1198. if ( _presavecallback != NULL) {
  1199. _presavecallback();
  1200. }
  1201. //parameters
  1202. if(_paramsCount > 0){
  1203. DEBUG_WM(DEBUG_VERBOSE,F("Parameters"));
  1204. DEBUG_WM(DEBUG_VERBOSE,FPSTR(D_HR));
  1205. for (int i = 0; i < _paramsCount; i++) {
  1206. if (_params[i] == NULL || _params[i]->_length == 0) {
  1207. DEBUG_WM(DEBUG_ERROR,"[ERROR] WiFiManagerParameter is out of scope");
  1208. break; // @todo might not be needed anymore
  1209. }
  1210. //read parameter from server
  1211. String name = (String)FPSTR(S_parampre)+(String)i;
  1212. String value;
  1213. if(server->hasArg(name)) {
  1214. value = server->arg(name);
  1215. } else {
  1216. value = server->arg(_params[i]->getID());
  1217. }
  1218. //store it in params array
  1219. value.toCharArray(_params[i]->_value, _params[i]->_length+1); // length+1 null terminated
  1220. DEBUG_WM(DEBUG_VERBOSE,(String)_params[i]->getID() + ":",value);
  1221. }
  1222. DEBUG_WM(DEBUG_VERBOSE,FPSTR(D_HR));
  1223. }
  1224. if ( _saveparamscallback != NULL) {
  1225. _saveparamscallback();
  1226. }
  1227. }
  1228. /**
  1229. * HTTPD CALLBACK info page
  1230. */
  1231. void WiFiManager::handleInfo() {
  1232. DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Info"));
  1233. handleRequest();
  1234. String page = getHTTPHead(FPSTR(S_titleinfo)); // @token titleinfo
  1235. reportStatus(page);
  1236. uint16_t infos = 0;
  1237. //@todo convert to enum or refactor to strings
  1238. //@todo wrap in build flag to remove all info code for memory saving
  1239. #ifdef ESP8266
  1240. infos = 27;
  1241. String infoids[] = {
  1242. F("esphead"),
  1243. F("uptime"),
  1244. F("chipid"),
  1245. F("fchipid"),
  1246. F("idesize"),
  1247. F("flashsize"),
  1248. F("sdkver"),
  1249. F("corever"),
  1250. F("bootver"),
  1251. F("cpufreq"),
  1252. F("freeheap"),
  1253. F("memsketch"),
  1254. F("memsmeter"),
  1255. F("lastreset"),
  1256. F("wifihead"),
  1257. F("apip"),
  1258. F("apmac"),
  1259. F("apssid"),
  1260. F("apbssid"),
  1261. F("staip"),
  1262. F("stagw"),
  1263. F("stasub"),
  1264. F("dnss"),
  1265. F("host"),
  1266. F("stamac"),
  1267. F("conx"),
  1268. F("autoconx")
  1269. };
  1270. #elif defined(ESP32)
  1271. infos = 22;
  1272. String infoids[] = {
  1273. F("esphead"),
  1274. F("uptime"),
  1275. F("chipid"),
  1276. F("chiprev"),
  1277. F("idesize"),
  1278. F("sdkver"),
  1279. F("cpufreq"),
  1280. F("freeheap"),
  1281. F("lastreset"),
  1282. // F("temp"),
  1283. F("wifihead"),
  1284. F("apip"),
  1285. F("apmac"),
  1286. F("aphost"),
  1287. F("apssid"),
  1288. F("apbssid"),
  1289. F("staip"),
  1290. F("stagw"),
  1291. F("stasub"),
  1292. F("dnss"),
  1293. F("host"),
  1294. F("stamac"),
  1295. F("conx")
  1296. };
  1297. #endif
  1298. for(size_t i=0; i<infos;i++){
  1299. if(infoids[i] != NULL) page += getInfoData(infoids[i]);
  1300. }
  1301. page += F("</dl>");
  1302. if(_showInfoErase) page += FPSTR(HTTP_ERASEBTN);
  1303. page += FPSTR(HTTP_HELP);
  1304. page += FPSTR(HTTP_END);
  1305. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
  1306. server->send(200, FPSTR(HTTP_HEAD_CT), page);
  1307. DEBUG_WM(DEBUG_DEV,F("Sent info page"));
  1308. }
  1309. String WiFiManager::getInfoData(String id){
  1310. String p;
  1311. // @todo add WM versioning
  1312. if(id==F("esphead"))p = FPSTR(HTTP_INFO_esphead);
  1313. else if(id==F("wifihead"))p = FPSTR(HTTP_INFO_wifihead);
  1314. else if(id==F("uptime")){
  1315. // subject to rollover!
  1316. p = FPSTR(HTTP_INFO_uptime);
  1317. p.replace(FPSTR(T_1),(String)(millis() / 1000 / 60));
  1318. p.replace(FPSTR(T_2),(String)((millis() / 1000) % 60));
  1319. }
  1320. else if(id==F("chipid")){
  1321. p = FPSTR(HTTP_INFO_chipid);
  1322. p.replace(FPSTR(T_1),String(WIFI_getChipId(),HEX));
  1323. }
  1324. #ifdef ESP32
  1325. else if(id==F("chiprev")){
  1326. p = FPSTR(HTTP_INFO_chiprev);
  1327. String rev = (String)ESP.getChipRevision();
  1328. #ifdef _SOC_EFUSE_REG_H_
  1329. String revb = (String)(REG_READ(EFUSE_BLK0_RDATA3_REG) >> (EFUSE_RD_CHIP_VER_RESERVE_S)&&EFUSE_RD_CHIP_VER_RESERVE_V);
  1330. p.replace(FPSTR(T_1),rev+"<br/>"+revb);
  1331. #else
  1332. p.replace(FPSTR(T_1),rev);
  1333. #endif
  1334. }
  1335. #endif
  1336. #ifdef ESP8266
  1337. else if(id==F("fchipid")){
  1338. p = FPSTR(HTTP_INFO_fchipid);
  1339. p.replace(FPSTR(T_1),(String)ESP.getFlashChipId());
  1340. }
  1341. #endif
  1342. else if(id==F("idesize")){
  1343. p = FPSTR(HTTP_INFO_idesize);
  1344. p.replace(FPSTR(T_1),(String)ESP.getFlashChipSize());
  1345. }
  1346. else if(id==F("flashsize")){
  1347. #ifdef ESP8266
  1348. p = FPSTR(HTTP_INFO_flashsize);
  1349. p.replace(FPSTR(T_1),(String)ESP.getFlashChipRealSize());
  1350. #endif
  1351. }
  1352. else if(id==F("sdkver")){
  1353. p = FPSTR(HTTP_INFO_sdkver);
  1354. #ifdef ESP32
  1355. p.replace(FPSTR(T_1),(String)esp_get_idf_version());
  1356. #else
  1357. p.replace(FPSTR(T_1),(String)system_get_sdk_version());
  1358. #endif
  1359. }
  1360. else if(id==F("corever")){
  1361. #ifdef ESP8266
  1362. p = FPSTR(HTTP_INFO_corever);
  1363. p.replace(FPSTR(T_1),(String)ESP.getCoreVersion());
  1364. #endif
  1365. }
  1366. #ifdef ESP8266
  1367. else if(id==F("bootver")){
  1368. p = FPSTR(HTTP_INFO_bootver);
  1369. p.replace(FPSTR(T_1),(String)system_get_boot_version());
  1370. }
  1371. #endif
  1372. else if(id==F("cpufreq")){
  1373. p = FPSTR(HTTP_INFO_cpufreq);
  1374. p.replace(FPSTR(T_1),(String)ESP.getCpuFreqMHz());
  1375. }
  1376. else if(id==F("freeheap")){
  1377. p = FPSTR(HTTP_INFO_freeheap);
  1378. p.replace(FPSTR(T_1),(String)ESP.getFreeHeap());
  1379. }
  1380. #ifdef ESP8266
  1381. else if(id==F("memsketch")){
  1382. p = FPSTR(HTTP_INFO_memsketch);
  1383. p.replace(FPSTR(T_1),(String)(ESP.getSketchSize()));
  1384. p.replace(FPSTR(T_2),(String)(ESP.getSketchSize()+ESP.getFreeSketchSpace()));
  1385. }
  1386. #endif
  1387. #ifdef ESP8266
  1388. else if(id==F("memsmeter")){
  1389. p = FPSTR(HTTP_INFO_memsmeter);
  1390. p.replace(FPSTR(T_1),(String)(ESP.getSketchSize()));
  1391. p.replace(FPSTR(T_2),(String)(ESP.getSketchSize()+ESP.getFreeSketchSpace()));
  1392. }
  1393. #endif
  1394. else if(id==F("lastreset")){
  1395. #ifdef ESP8266
  1396. p = FPSTR(HTTP_INFO_lastreset);
  1397. p.replace(FPSTR(T_1),(String)ESP.getResetReason());
  1398. #elif defined(ESP32) && defined(_ROM_RTC_H_)
  1399. // requires #include <rom/rtc.h>
  1400. p = FPSTR(HTTP_INFO_lastreset);
  1401. for(int i=0;i<2;i++){
  1402. int reason = rtc_get_reset_reason(i);
  1403. String tok = (String)T_ss+(String)(i+1)+(String)T_es;
  1404. switch (reason)
  1405. {
  1406. //@todo move to array
  1407. case 1 : p.replace(tok,F("Vbat power on reset"));break;
  1408. case 3 : p.replace(tok,F("Software reset digital core"));break;
  1409. case 4 : p.replace(tok,F("Legacy watch dog reset digital core"));break;
  1410. case 5 : p.replace(tok,F("Deep Sleep reset digital core"));break;
  1411. case 6 : p.replace(tok,F("Reset by SLC module, reset digital core"));break;
  1412. case 7 : p.replace(tok,F("Timer Group0 Watch dog reset digital core"));break;
  1413. case 8 : p.replace(tok,F("Timer Group1 Watch dog reset digital core"));break;
  1414. case 9 : p.replace(tok,F("RTC Watch dog Reset digital core"));break;
  1415. case 10 : p.replace(tok,F("Instrusion tested to reset CPU"));break;
  1416. case 11 : p.replace(tok,F("Time Group reset CPU"));break;
  1417. case 12 : p.replace(tok,F("Software reset CPU"));break;
  1418. case 13 : p.replace(tok,F("RTC Watch dog Reset CPU"));break;
  1419. case 14 : p.replace(tok,F("for APP CPU, reseted by PRO CPU"));break;
  1420. case 15 : p.replace(tok,F("Reset when the vdd voltage is not stable"));break;
  1421. case 16 : p.replace(tok,F("RTC Watch dog reset digital core and rtc module"));break;
  1422. default : p.replace(tok,F("NO_MEAN"));
  1423. }
  1424. }
  1425. #endif
  1426. }
  1427. else if(id==F("apip")){
  1428. p = FPSTR(HTTP_INFO_apip);
  1429. p.replace(FPSTR(T_1),WiFi.softAPIP().toString());
  1430. }
  1431. else if(id==F("apmac")){
  1432. p = FPSTR(HTTP_INFO_apmac);
  1433. p.replace(FPSTR(T_1),(String)WiFi.softAPmacAddress());
  1434. }
  1435. #ifdef ESP32
  1436. else if(id==F("aphost")){
  1437. p = FPSTR(HTTP_INFO_aphost);
  1438. p.replace(FPSTR(T_1),WiFi.softAPgetHostname());
  1439. }
  1440. #endif
  1441. else if(id==F("apssid")){
  1442. p = FPSTR(HTTP_INFO_apssid);
  1443. p.replace(FPSTR(T_1),htmlEntities((String)WiFi_SSID()));
  1444. }
  1445. else if(id==F("apbssid")){
  1446. p = FPSTR(HTTP_INFO_apbssid);
  1447. p.replace(FPSTR(T_1),(String)WiFi.BSSIDstr());
  1448. }
  1449. else if(id==F("staip")){
  1450. p = FPSTR(HTTP_INFO_staip);
  1451. p.replace(FPSTR(T_1),WiFi.localIP().toString());
  1452. }
  1453. else if(id==F("stagw")){
  1454. p = FPSTR(HTTP_INFO_stagw);
  1455. p.replace(FPSTR(T_1),WiFi.gatewayIP().toString());
  1456. }
  1457. else if(id==F("stasub")){
  1458. p = FPSTR(HTTP_INFO_stasub);
  1459. p.replace(FPSTR(T_1),WiFi.subnetMask().toString());
  1460. }
  1461. else if(id==F("dnss")){
  1462. p = FPSTR(HTTP_INFO_dnss);
  1463. p.replace(FPSTR(T_1),WiFi.dnsIP().toString());
  1464. }
  1465. else if(id==F("host")){
  1466. p = FPSTR(HTTP_INFO_host);
  1467. #ifdef ESP32
  1468. p.replace(FPSTR(T_1),WiFi.getHostname());
  1469. #else
  1470. p.replace(FPSTR(T_1),WiFi.hostname());
  1471. #endif
  1472. }
  1473. else if(id==F("stamac")){
  1474. p = FPSTR(HTTP_INFO_stamac);
  1475. p.replace(FPSTR(T_1),WiFi.macAddress());
  1476. }
  1477. else if(id==F("conx")){
  1478. p = FPSTR(HTTP_INFO_conx);
  1479. p.replace(FPSTR(T_1),WiFi.isConnected() ? FPSTR(S_y) : FPSTR(S_n));
  1480. }
  1481. #ifdef ESP8266
  1482. else if(id==F("autoconx")){
  1483. p = FPSTR(HTTP_INFO_autoconx);
  1484. p.replace(FPSTR(T_1),WiFi.getAutoConnect() ? FPSTR(S_enable) : FPSTR(S_disable));
  1485. }
  1486. #endif
  1487. #ifdef ESP32
  1488. else if(id==F("temp")){
  1489. // temperature is not calibrated, varying large offsets are present, use for relative temp changes only
  1490. p = FPSTR(HTTP_INFO_temp);
  1491. p.replace(FPSTR(T_1),(String)temperatureRead());
  1492. p.replace(FPSTR(T_2),(String)((temperatureRead()+32)*1.8));
  1493. }
  1494. #endif
  1495. return p;
  1496. }
  1497. /**
  1498. * HTTPD CALLBACK root or redirect to captive portal
  1499. */
  1500. void WiFiManager::handleExit() {
  1501. DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Exit"));
  1502. handleRequest();
  1503. String page = getHTTPHead(FPSTR(S_titleexit)); // @token titleexit
  1504. page += FPSTR(S_exiting); // @token exiting
  1505. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
  1506. server->send(200, FPSTR(HTTP_HEAD_CT), page);
  1507. abort = true;
  1508. }
  1509. /**
  1510. * HTTPD CALLBACK reset page
  1511. */
  1512. void WiFiManager::handleReset() {
  1513. DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Reset"));
  1514. handleRequest();
  1515. String page = getHTTPHead(FPSTR(S_titlereset)); //@token titlereset
  1516. page += FPSTR(S_resetting); //@token resetting
  1517. page += FPSTR(HTTP_END);
  1518. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
  1519. server->send(200, FPSTR(HTTP_HEAD_CT), page);
  1520. DEBUG_WM(F("RESETTING ESP"));
  1521. delay(1000);
  1522. reboot();
  1523. }
  1524. /**
  1525. * HTTPD CALLBACK erase page
  1526. */
  1527. // void WiFiManager::handleErase() {
  1528. // handleErase(false);
  1529. // }
  1530. void WiFiManager::handleErase(boolean opt) {
  1531. DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP Erase"));
  1532. handleRequest();
  1533. String page = getHTTPHead(FPSTR(S_titleerase)); // @token titleerase
  1534. bool ret = erase(opt);
  1535. if(ret) page += FPSTR(S_resetting); // @token resetting
  1536. else {
  1537. page += FPSTR(S_error); // @token erroroccur
  1538. DEBUG_WM(DEBUG_ERROR,F("[ERROR] WiFi EraseConfig failed"));
  1539. }
  1540. page += FPSTR(HTTP_END);
  1541. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
  1542. server->send(200, FPSTR(HTTP_HEAD_CT), page);
  1543. if(ret){
  1544. delay(2000);
  1545. DEBUG_WM(F("RESETTING ESP"));
  1546. reboot();
  1547. }
  1548. }
  1549. /**
  1550. * HTTPD CALLBACK 404
  1551. */
  1552. void WiFiManager::handleNotFound() {
  1553. if (captivePortal()) return; // If captive portal redirect instead of displaying the page
  1554. handleRequest();
  1555. String message = FPSTR(S_notfound); // @token notfound
  1556. message += FPSTR(S_uri); // @token uri
  1557. message += server->uri();
  1558. message += FPSTR(S_method); // @token method
  1559. message += ( server->method() == HTTP_GET ) ? FPSTR(S_GET) : FPSTR(S_POST);
  1560. message += FPSTR(S_args); // @token args
  1561. message += server->args();
  1562. message += F("\n");
  1563. for ( uint8_t i = 0; i < server->args(); i++ ) {
  1564. message += " " + server->argName ( i ) + ": " + server->arg ( i ) + "\n";
  1565. }
  1566. server->sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate"));
  1567. server->sendHeader(F("Pragma"), F("no-cache"));
  1568. server->sendHeader(F("Expires"), F("-1"));
  1569. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(message.length()));
  1570. server->send ( 404, FPSTR(HTTP_HEAD_CT2), message );
  1571. }
  1572. /**
  1573. * HTTPD redirector
  1574. * Redirect to captive portal if we got a request for another domain.
  1575. * Return true in that case so the page handler do not try to handle the request again.
  1576. */
  1577. boolean WiFiManager::captivePortal() {
  1578. DEBUG_WM(DEBUG_DEV,"-> " + server->hostHeader());
  1579. if(!_enableCaptivePortal) return false; // skip redirections
  1580. if (!isIp(server->hostHeader())) {
  1581. DEBUG_WM(DEBUG_VERBOSE,F("<- Request redirected to captive portal"));
  1582. server->sendHeader(F("Location"), (String)F("http://") + toStringIp(server->client().localIP()), true);
  1583. server->send ( 302, FPSTR(HTTP_HEAD_CT2), ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
  1584. server->client().stop(); // Stop is needed because we sent no content length
  1585. return true;
  1586. }
  1587. return false;
  1588. }
  1589. void WiFiManager::stopCaptivePortal(){
  1590. _enableCaptivePortal= false;
  1591. // @todo maybe disable configportaltimeout(optional), or just provide callback for user
  1592. }
  1593. void WiFiManager::handleClose(){
  1594. stopCaptivePortal();
  1595. DEBUG_WM(DEBUG_VERBOSE,F("<- HTTP close"));
  1596. handleRequest();
  1597. String page = getHTTPHead(FPSTR(S_titleclose)); // @token titleclose
  1598. page += FPSTR(S_closing); // @token closing
  1599. server->sendHeader(FPSTR(HTTP_HEAD_CL), String(page.length()));
  1600. server->send(200, FPSTR(HTTP_HEAD_CT), page);
  1601. }
  1602. void WiFiManager::reportStatus(String &page){
  1603. updateConxResult(WiFi.status());
  1604. String str;
  1605. if (WiFi_SSID() != ""){
  1606. if (WiFi.status()==WL_CONNECTED){
  1607. str = FPSTR(HTTP_STATUS_ON);
  1608. str.replace(FPSTR(T_i),WiFi.localIP().toString());
  1609. str.replace(FPSTR(T_v),htmlEntities(WiFi_SSID()));
  1610. }
  1611. else {
  1612. str = FPSTR(HTTP_STATUS_OFF);
  1613. str.replace(FPSTR(T_v),htmlEntities(WiFi_SSID()));
  1614. if(_lastconxresult == WL_STATION_WRONG_PASSWORD){
  1615. // wrong password
  1616. str.replace(FPSTR(T_c),"D"); // class
  1617. str.replace(FPSTR(T_r),FPSTR(HTTP_STATUS_OFFPW));
  1618. }
  1619. else if(_lastconxresult == WL_NO_SSID_AVAIL){
  1620. // connect failed, or ap not found
  1621. str.replace(FPSTR(T_c),"D");
  1622. str.replace(FPSTR(T_r),FPSTR(HTTP_STATUS_OFFNOAP));
  1623. }
  1624. else if(_lastconxresult == WL_CONNECT_FAILED){
  1625. // connect failed
  1626. str.replace(FPSTR(T_c),"D");
  1627. str.replace(FPSTR(T_r),FPSTR(HTTP_STATUS_OFFFAIL));
  1628. }
  1629. else{
  1630. str.replace(FPSTR(T_c),"");
  1631. str.replace(FPSTR(T_r),"");
  1632. }
  1633. }
  1634. }
  1635. else {
  1636. str = FPSTR(HTTP_STATUS_NONE);
  1637. }
  1638. page += str;
  1639. }
  1640. // PUBLIC
  1641. // METHODS
  1642. /**
  1643. * reset wifi settings, clean stored ap password
  1644. */
  1645. /**
  1646. * [stopConfigPortal description]
  1647. * @return {[type]} [description]
  1648. */
  1649. bool WiFiManager::stopConfigPortal(){
  1650. if(_configPortalIsBlocking){
  1651. abort = true;
  1652. return true;
  1653. }
  1654. return shutdownConfigPortal();
  1655. }
  1656. /**
  1657. * disconnect
  1658. * @access public
  1659. * @since $dev
  1660. * @return bool success
  1661. */
  1662. bool WiFiManager::disconnect(){
  1663. if(WiFi.status() != WL_CONNECTED){
  1664. DEBUG_WM(DEBUG_VERBOSE,"Disconnecting: Not connected");
  1665. return false;
  1666. }
  1667. DEBUG_WM("Disconnecting");
  1668. return WiFi_Disconnect();
  1669. }
  1670. /**
  1671. * reboot the device
  1672. * @access public
  1673. */
  1674. void WiFiManager::reboot(){
  1675. DEBUG_WM("Restarting");
  1676. ESP.restart();
  1677. }
  1678. /**
  1679. * reboot the device
  1680. * @access public
  1681. */
  1682. bool WiFiManager::erase(){
  1683. return erase(false);
  1684. }
  1685. bool WiFiManager::erase(bool opt){
  1686. DEBUG_WM("Erasing");
  1687. #if defined(ESP32) && ((defined(WM_ERASE_NVS) || defined(nvs_flash_h)))
  1688. // if opt true, do nvs erase
  1689. if(opt){
  1690. DEBUG_WM("Erasing NVS");
  1691. int err;
  1692. esp_err_t err;
  1693. err = nvs_flash_init();
  1694. DEBUG_WM(DEBUG_VERBOSE,"nvs_flash_init: ",err!=ESP_OK ? (String)err : "Success");
  1695. err = nvs_flash_erase();
  1696. DEBUG_WM(DEBUG_VERBOSE,"nvs_flash_erase: ", err!=ESP_OK ? (String)err : "Success");
  1697. return err == ESP_OK;
  1698. }
  1699. #elif defined(ESP8266) && defined(spiffs_api_h)
  1700. if(opt){
  1701. bool ret = false;
  1702. if(SPIFFS.begin()){
  1703. DEBUG_WM("Erasing SPIFFS");
  1704. bool ret = SPIFFS.format();
  1705. DEBUG_WM(DEBUG_VERBOSE,"spiffs erase: ",ret ? "Success" : "ERROR");
  1706. } else DEBUG_WM("[ERROR] Could not start SPIFFS");
  1707. return ret;
  1708. }
  1709. #else
  1710. (void)opt;
  1711. #endif
  1712. DEBUG_WM("Erasing WiFi Config");
  1713. return WiFi_eraseConfig();
  1714. }
  1715. /**
  1716. * [resetSettings description]
  1717. * ERASES STA CREDENTIALS
  1718. * @access public
  1719. */
  1720. void WiFiManager::resetSettings() {
  1721. DEBUG_WM(F("SETTINGS ERASED"));
  1722. WiFi_enableSTA(true,true); // must be sta to disconnect erase
  1723. if (_resetcallback != NULL)
  1724. _resetcallback();
  1725. #ifdef ESP32
  1726. WiFi.disconnect(true,true);
  1727. #else
  1728. WiFi.persistent(true);
  1729. WiFi.disconnect(true);
  1730. WiFi.persistent(false);
  1731. #endif
  1732. }
  1733. // SETTERS
  1734. /**
  1735. * [setTimeout description]
  1736. * @access public
  1737. * @param {[type]} unsigned long seconds [description]
  1738. */
  1739. void WiFiManager::setTimeout(unsigned long seconds) {
  1740. setConfigPortalTimeout(seconds);
  1741. }
  1742. /**
  1743. * [setConfigPortalTimeout description]
  1744. * @access public
  1745. * @param {[type]} unsigned long seconds [description]
  1746. */
  1747. void WiFiManager::setConfigPortalTimeout(unsigned long seconds) {
  1748. _configPortalTimeout = seconds * 1000;
  1749. }
  1750. /**
  1751. * [setConnectTimeout description]
  1752. * @access public
  1753. * @param {[type]} unsigned long seconds [description]
  1754. */
  1755. void WiFiManager::setConnectTimeout(unsigned long seconds) {
  1756. _connectTimeout = seconds * 1000;
  1757. }
  1758. /**
  1759. * toggle _cleanconnect, always disconnect before connecting
  1760. * @param {[type]} bool enable [description]
  1761. */
  1762. void WiFiManager::setCleanConnect(bool enable){
  1763. _cleanConnect = enable;
  1764. }
  1765. /**
  1766. * [setConnectTimeout description
  1767. * @access public
  1768. * @param {[type]} unsigned long seconds [description]
  1769. */
  1770. void WiFiManager::setSaveConnectTimeout(unsigned long seconds) {
  1771. _saveTimeout = seconds * 1000;
  1772. }
  1773. /**
  1774. * [setDebugOutput description]
  1775. * @access public
  1776. * @param {[type]} boolean debug [description]
  1777. */
  1778. void WiFiManager::setDebugOutput(boolean debug) {
  1779. _debug = debug;
  1780. if(_debug && _debugLevel == DEBUG_DEV) debugPlatformInfo();
  1781. }
  1782. /**
  1783. * [setAPStaticIPConfig description]
  1784. * @access public
  1785. * @param {[type]} IPAddress ip [description]
  1786. * @param {[type]} IPAddress gw [description]
  1787. * @param {[type]} IPAddress sn [description]
  1788. */
  1789. void WiFiManager::setAPStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn) {
  1790. _ap_static_ip = ip;
  1791. _ap_static_gw = gw;
  1792. _ap_static_sn = sn;
  1793. }
  1794. /**
  1795. * [setSTAStaticIPConfig description]
  1796. * @access public
  1797. * @param {[type]} IPAddress ip [description]
  1798. * @param {[type]} IPAddress gw [description]
  1799. * @param {[type]} IPAddress sn [description]
  1800. */
  1801. void WiFiManager::setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn) {
  1802. _sta_static_ip = ip;
  1803. _sta_static_gw = gw;
  1804. _sta_static_sn = sn;
  1805. }
  1806. /**
  1807. * [setSTAStaticIPConfig description]
  1808. * @since $dev
  1809. * @access public
  1810. * @param {[type]} IPAddress ip [description]
  1811. * @param {[type]} IPAddress gw [description]
  1812. * @param {[type]} IPAddress sn [description]
  1813. * @param {[type]} IPAddress dns [description]
  1814. */
  1815. void WiFiManager::setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn, IPAddress dns) {
  1816. setSTAStaticIPConfig(ip,gw,sn);
  1817. _sta_static_dns = dns;
  1818. }
  1819. /**
  1820. * [setMinimumSignalQuality description]
  1821. * @access public
  1822. * @param {[type]} int quality [description]
  1823. */
  1824. void WiFiManager::setMinimumSignalQuality(int quality) {
  1825. _minimumQuality = quality;
  1826. }
  1827. /**
  1828. * [setBreakAfterConfig description]
  1829. * @access public
  1830. * @param {[type]} boolean shouldBreak [description]
  1831. */
  1832. void WiFiManager::setBreakAfterConfig(boolean shouldBreak) {
  1833. _shouldBreakAfterConfig = shouldBreak;
  1834. }
  1835. /**
  1836. * setAPCallback, set a callback when softap is started
  1837. * @access public
  1838. * @param {[type]} void (*func)(WiFiManager* wminstance)
  1839. */
  1840. void WiFiManager::setAPCallback( std::function<void(WiFiManager*)> func ) {
  1841. _apcallback = func;
  1842. }
  1843. /**
  1844. * setWebServerCallback, set a callback after webserver is reset, and before routes are setup
  1845. * if we set webserver handlers before wm, they are used and wm is not by esp webserver
  1846. * on events cannot be overrided once set, and are not mutiples
  1847. * @access public
  1848. * @param {[type]} void (*func)(void)
  1849. */
  1850. void WiFiManager::setWebServerCallback( std::function<void()> func ) {
  1851. _webservercallback = func;
  1852. }
  1853. /**
  1854. * setSaveConfigCallback, set a save config callback after closing configportal
  1855. * @note calls only if wifi is saved or changed, or setBreakAfterConfig(true)
  1856. * @access public
  1857. * @param {[type]} void (*func)(void)
  1858. */
  1859. void WiFiManager::setSaveConfigCallback( std::function<void()> func ) {
  1860. _savewificallback = func;
  1861. }
  1862. /**
  1863. * setConfigResetCallback, set a callback to occur when a resetSettings() occurs
  1864. * @access public
  1865. * @param {[type]} void(*func)(void)
  1866. */
  1867. void WiFiManager::setConfigResetCallback( std::function<void()> func ) {
  1868. _resetcallback = func;
  1869. }
  1870. /**
  1871. * setSaveParamsCallback, set a save params callback on params save in wifi or params pages
  1872. * @access public
  1873. * @param {[type]} void (*func)(void)
  1874. */
  1875. void WiFiManager::setSaveParamsCallback( std::function<void()> func ) {
  1876. _saveparamscallback = func;
  1877. }
  1878. /**
  1879. * setPreSaveConfigCallback, set a callback to fire before saving wifi or params
  1880. * @access public
  1881. * @param {[type]} void (*func)(void)
  1882. */
  1883. void WiFiManager::setPreSaveConfigCallback( std::function<void()> func ) {
  1884. _presavecallback = func;
  1885. }
  1886. /**
  1887. * set custom head html
  1888. * custom element will be added to head, eg. new style tag etc.
  1889. * @access public
  1890. * @param char element
  1891. */
  1892. void WiFiManager::setCustomHeadElement(const char* element) {
  1893. _customHeadElement = element;
  1894. }
  1895. /**
  1896. * toggle wifiscan hiding of duplicate ssid names
  1897. * if this is false, wifiscan will remove duplicat Access Points - defaut true
  1898. * @access public
  1899. * @param boolean removeDuplicates [true]
  1900. */
  1901. void WiFiManager::setRemoveDuplicateAPs(boolean removeDuplicates) {
  1902. _removeDuplicateAPs = removeDuplicates;
  1903. }
  1904. /**
  1905. * toggle configportal blocking loop
  1906. * if enabled, then the configportal will enter a blocking loop and wait for configuration
  1907. * if disabled use with process() to manually process webserver
  1908. * @since $dev
  1909. * @access public
  1910. * @param boolean shoudlBlock [false]
  1911. */
  1912. void WiFiManager::setConfigPortalBlocking(boolean shoudlBlock) {
  1913. _configPortalIsBlocking = shoudlBlock;
  1914. }
  1915. /**
  1916. * toggle restore persistent, track internally
  1917. * sets ESP wifi.persistent so we can remember it and restore user preference on destruct
  1918. * there is no getter in esp8266 platform prior to https://github.com/esp8266/Arduino/pull/3857
  1919. * @since $dev
  1920. * @access public
  1921. * @param boolean persistent [true]
  1922. */
  1923. void WiFiManager::setRestorePersistent(boolean persistent) {
  1924. _userpersistent = persistent;
  1925. if(!persistent) DEBUG_WM(F("persistent is off"));
  1926. }
  1927. /**
  1928. * toggle showing static ip form fields
  1929. * if enabled, then the static ip, gateway, subnet fields will be visible, even if not set in code
  1930. * @since $dev
  1931. * @access public
  1932. * @param boolean alwaysShow [false]
  1933. */
  1934. void WiFiManager::setShowStaticFields(boolean alwaysShow){
  1935. if(_disableIpFields) _staShowStaticFields = alwaysShow ? 1 : -1;
  1936. else _staShowStaticFields = alwaysShow ? 1 : 0;
  1937. }
  1938. /**
  1939. * toggle showing dns fields
  1940. * if enabled, then the dns1 field will be visible, even if not set in code
  1941. * @since $dev
  1942. * @access public
  1943. * @param boolean alwaysShow [false]
  1944. */
  1945. void WiFiManager::setShowDnsFields(boolean alwaysShow){
  1946. if(_disableIpFields) _staShowDns = alwaysShow ? 1 : -1;
  1947. _staShowDns = alwaysShow ? 1 : 0;
  1948. }
  1949. /**
  1950. * toggle showing password in wifi password field
  1951. * if not enabled, placeholder will be S_passph
  1952. * @since $dev
  1953. * @access public
  1954. * @param boolean alwaysShow [false]
  1955. */
  1956. void WiFiManager::setShowPassword(boolean show){
  1957. _showPassword = show;
  1958. }
  1959. /**
  1960. * toggle captive portal
  1961. * if enabled, then devices that use captive portal checks will be redirected to root
  1962. * if not you will automatically have to navigate to ip [192.168.4.1]
  1963. * @since $dev
  1964. * @access public
  1965. * @param boolean enabled [true]
  1966. */
  1967. void WiFiManager::setCaptivePortalEnable(boolean enabled){
  1968. _enableCaptivePortal = enabled;
  1969. }
  1970. /**
  1971. * toggle wifi autoreconnect policy
  1972. * if enabled, then wifi will autoreconnect automatically always
  1973. * On esp8266 we force this on when autoconnect is called, see notes
  1974. * On esp32 this is handled on SYSTEM_EVENT_STA_DISCONNECTED since it does not exist in core yet
  1975. * @since $dev
  1976. * @access public
  1977. * @param boolean enabled [true]
  1978. */
  1979. void WiFiManager::setWiFiAutoReconnect(boolean enabled){
  1980. _wifiAutoReconnect = enabled;
  1981. }
  1982. /**
  1983. * toggle configportal timeout wait for station client
  1984. * if enabled, then the configportal will start timeout when no stations are connected to softAP
  1985. * disabled by default as rogue stations can keep it open if there is no auth
  1986. * @since $dev
  1987. * @access public
  1988. * @param boolean enabled [false]
  1989. */
  1990. void WiFiManager::setAPClientCheck(boolean enabled){
  1991. _apClientCheck = enabled;
  1992. }
  1993. /**
  1994. * toggle configportal timeout wait for web client
  1995. * if enabled, then the configportal will restart timeout when client requests come in
  1996. * @since $dev
  1997. * @access public
  1998. * @param boolean enabled [true]
  1999. */
  2000. void WiFiManager::setWebPortalClientCheck(boolean enabled){
  2001. _webClientCheck = enabled;
  2002. }
  2003. /**
  2004. * toggle wifiscan percentages or quality icons
  2005. * @since $dev
  2006. * @access public
  2007. * @param boolean enabled [false]
  2008. */
  2009. void WiFiManager::setScanDispPerc(boolean enabled){
  2010. _scanDispOptions = enabled;
  2011. }
  2012. /**
  2013. * toggle configportal if autoconnect failed
  2014. * if enabled, then the configportal will be activated on autoconnect failure
  2015. * @since $dev
  2016. * @access public
  2017. * @param boolean enabled [true]
  2018. */
  2019. void WiFiManager::setEnableConfigPortal(boolean enable)
  2020. {
  2021. _enableConfigPortal = enable;
  2022. }
  2023. /**
  2024. * set the hostname (dhcp client id)
  2025. * @since $dev
  2026. * @access public
  2027. * @param char* hostname 32 character hostname to use for sta+ap in esp32, sta in esp8266
  2028. * @return bool false if hostname is not valid
  2029. */
  2030. bool WiFiManager::setHostname(const char * hostname){
  2031. //@todo max length 32
  2032. _hostname = hostname;
  2033. return true;
  2034. }
  2035. /**
  2036. * set the soft ao channel, ignored if channelsync is true and connected
  2037. * @param int32_t wifi channel, 0 to disable
  2038. */
  2039. void WiFiManager::setWiFiAPChannel(int32_t channel){
  2040. _apChannel = channel;
  2041. }
  2042. /**
  2043. * set the soft ap hidden
  2044. * @param bool wifi ap hidden, default is false
  2045. */
  2046. void WiFiManager::setWiFiAPHidden(bool hidden){
  2047. _apHidden = hidden;
  2048. }
  2049. /**
  2050. * toggle showing erase wifi config button on info page
  2051. * @param boolean enabled
  2052. */
  2053. void WiFiManager::setShowInfoErase(boolean enabled){
  2054. _showInfoErase = enabled;
  2055. }
  2056. /**
  2057. * set menu items and order
  2058. * if param is present in menu , params will be removed from wifi page automatically
  2059. * eg.
  2060. * const char * menu[] = {"wifi","setup","sep","info","exit"};
  2061. * WiFiManager.setMenu(menu);
  2062. * @since $dev
  2063. * @param uint8_t menu[] array of menu ids
  2064. */
  2065. void WiFiManager::setMenu(const char * menu[], uint8_t size){
  2066. // DEBUG_WM(DEBUG_VERBOSE,"setmenu array");
  2067. _menuIds.clear();
  2068. for(size_t i = 0; i < size; i++){
  2069. for(size_t j = 0; j < _nummenutokens; j++){
  2070. if(menu[i] == _menutokens[j]){
  2071. if((String)menu[i] == "param") _paramsInWifi = false; // param auto flag
  2072. _menuIds.push_back(j);
  2073. }
  2074. }
  2075. }
  2076. DEBUG_WM(getMenuOut());
  2077. }
  2078. /**
  2079. * setMenu with vector
  2080. * eg.
  2081. * std::vector<const char *> menu = {"wifi","setup","sep","info","exit"};
  2082. * WiFiManager.setMenu(menu);
  2083. * tokens can be found in _menutokens array in strings_en.h
  2084. * @shiftIncrement $dev
  2085. * @param {[type]} std::vector<const char *>& menu [description]
  2086. */
  2087. void WiFiManager::setMenu(std::vector<const char *>& menu){
  2088. // DEBUG_WM(DEBUG_VERBOSE,"setmenu vector");
  2089. _menuIds.clear();
  2090. for(auto menuitem : menu ){
  2091. for(size_t j = 0; j < _nummenutokens; j++){
  2092. if(menuitem == _menutokens[j]){
  2093. if((String)menuitem == "param") _paramsInWifi = false; // param auto flag
  2094. _menuIds.push_back(j);
  2095. }
  2096. }
  2097. }
  2098. DEBUG_WM(getMenuOut());
  2099. }
  2100. /**
  2101. * set params as sperate page not in wifi
  2102. * NOT COMPATIBLE WITH setMenu! @todo scan menuids and insert param after wifi or something
  2103. * @param bool enable
  2104. * @since $dev
  2105. */
  2106. void WiFiManager::setParamsPage(bool enable){
  2107. _paramsInWifi = !enable;
  2108. setMenu(enable ? _menuIdsParams : _menuIdsDefault);
  2109. }
  2110. // GETTERS
  2111. /**
  2112. * get config portal AP SSID
  2113. * @since 0.0.1
  2114. * @access public
  2115. * @return String the configportal ap name
  2116. */
  2117. String WiFiManager::getConfigPortalSSID() {
  2118. return _apName;
  2119. }
  2120. /**
  2121. * return the last known connection result
  2122. * logged on autoconnect and wifisave, can be used to check why failed
  2123. * get as readable string with getWLStatusString(getLastConxResult);
  2124. * @since $dev
  2125. * @access public
  2126. * @return bool return wl_status codes
  2127. */
  2128. uint8_t WiFiManager::getLastConxResult(){
  2129. return _lastconxresult;
  2130. }
  2131. /**
  2132. * check if wifi has a saved ap or not
  2133. * @since $dev
  2134. * @access public
  2135. * @return bool true if a saved ap config exists
  2136. */
  2137. bool WiFiManager::getWiFiIsSaved(){
  2138. return WiFi_hasAutoConnect();
  2139. }
  2140. String WiFiManager::getDefaultAPName(){
  2141. String hostString = String(WIFI_getChipId(),HEX);
  2142. hostString.toUpperCase();
  2143. // char hostString[16] = {0};
  2144. // sprintf(hostString, "%06X", ESP.getChipId());
  2145. return _wifissidprefix + "_" + hostString;
  2146. }
  2147. /**
  2148. * setCountry
  2149. * @since $dev
  2150. * @param String cc country code, must be defined in WiFiSetCountry, US, JP, CN
  2151. */
  2152. void WiFiManager::setCountry(String cc){
  2153. _wificountry = cc;
  2154. }
  2155. /**
  2156. * setClass
  2157. * @param String str body class string
  2158. */
  2159. void WiFiManager::setClass(String str){
  2160. _bodyClass = str;
  2161. }
  2162. // HELPERS
  2163. /**
  2164. * getWiFiSSID
  2165. * @since $dev
  2166. * @param bool persistent
  2167. * @return String
  2168. */
  2169. String WiFiManager::getWiFiSSID(bool persistent){
  2170. return WiFi_SSID(persistent);
  2171. }
  2172. /**
  2173. * getWiFiPass
  2174. * @since $dev
  2175. * @param bool persistent
  2176. * @return String
  2177. */
  2178. String WiFiManager::getWiFiPass(bool persistent){
  2179. return WiFi_psk(persistent);
  2180. }
  2181. // DEBUG
  2182. // @todo fix DEBUG_WM(0,0);
  2183. template <typename Generic>
  2184. void WiFiManager::DEBUG_WM(Generic text) {
  2185. DEBUG_WM(DEBUG_NOTIFY,text,"");
  2186. }
  2187. template <typename Generic>
  2188. void WiFiManager::DEBUG_WM(wm_debuglevel_t level,Generic text) {
  2189. if(_debugLevel >= level) DEBUG_WM(level,text,"");
  2190. }
  2191. template <typename Generic, typename Genericb>
  2192. void WiFiManager::DEBUG_WM(Generic text,Genericb textb) {
  2193. DEBUG_WM(DEBUG_NOTIFY,text,textb);
  2194. }
  2195. template <typename Generic, typename Genericb>
  2196. void WiFiManager::DEBUG_WM(wm_debuglevel_t level,Generic text,Genericb textb) {
  2197. if(!_debug || _debugLevel < level) return;
  2198. if(_debugLevel >= DEBUG_MAX){
  2199. uint32_t free;
  2200. uint16_t max;
  2201. uint8_t frag;
  2202. #ifdef ESP8266
  2203. ESP.getHeapStats(&free, &max, &frag);
  2204. _debugPort.printf("[MEM] free: %5d | max: %5d | frag: %3d%% \n", free, max, frag);
  2205. #elif defined ESP32
  2206. // total_free_bytes; ///< Total free bytes in the heap. Equivalent to multi_free_heap_size().
  2207. // total_allocated_bytes; ///< Total bytes allocated to data in the heap.
  2208. // largest_free_block; ///< Size of largest free block in the heap. This is the largest malloc-able size.
  2209. // minimum_free_bytes; ///< Lifetime minimum free heap size. Equivalent to multi_minimum_free_heap_size().
  2210. // allocated_blocks; ///< Number of (variable size) blocks allocated in the heap.
  2211. // free_blocks; ///< Number of (variable size) free blocks in the heap.
  2212. // total_blocks; ///< Total number of (variable size) blocks in the heap.
  2213. multi_heap_info_t info;
  2214. heap_caps_get_info(&info, MALLOC_CAP_INTERNAL);
  2215. free = info.total_free_bytes;
  2216. max = info.largest_free_block;
  2217. frag = 100 - (max * 100) / free;
  2218. _debugPort.printf("[MEM] free: %5d | max: %5d | frag: %3d%% \n", free, max, frag);
  2219. #endif
  2220. }
  2221. _debugPort.print("*WM: ");
  2222. if(_debugLevel == DEBUG_DEV) _debugPort.print("["+(String)level+"] ");
  2223. _debugPort.print(text);
  2224. if(textb){
  2225. _debugPort.print(" ");
  2226. _debugPort.print(textb);
  2227. }
  2228. _debugPort.println();
  2229. }
  2230. /**
  2231. * [debugSoftAPConfig description]
  2232. * @access public
  2233. * @return {[type]} [description]
  2234. */
  2235. void WiFiManager::debugSoftAPConfig(){
  2236. wifi_country_t country;
  2237. #ifdef ESP8266
  2238. softap_config config;
  2239. wifi_softap_get_config(&config);
  2240. wifi_get_country(&country);
  2241. #elif defined(ESP32)
  2242. wifi_config_t conf_config;
  2243. esp_wifi_get_config(WIFI_IF_AP, &conf_config); // == ESP_OK
  2244. wifi_ap_config_t config = conf_config.ap;
  2245. esp_wifi_get_country(&country);
  2246. #endif
  2247. DEBUG_WM(F("SoftAP Configuration"));
  2248. DEBUG_WM(FPSTR(D_HR));
  2249. DEBUG_WM(F("ssid: "),(char *) config.ssid);
  2250. DEBUG_WM(F("password: "),(char *) config.password);
  2251. DEBUG_WM(F("ssid_len: "),config.ssid_len);
  2252. DEBUG_WM(F("channel: "),config.channel);
  2253. DEBUG_WM(F("authmode: "),config.authmode);
  2254. DEBUG_WM(F("ssid_hidden: "),config.ssid_hidden);
  2255. DEBUG_WM(F("max_connection: "),config.max_connection);
  2256. DEBUG_WM(F("country: "),(String)country.cc);
  2257. DEBUG_WM(F("beacon_interval: "),(String)config.beacon_interval + "(ms)");
  2258. DEBUG_WM(FPSTR(D_HR));
  2259. }
  2260. /**
  2261. * [debugPlatformInfo description]
  2262. * @access public
  2263. * @return {[type]} [description]
  2264. */
  2265. void WiFiManager::debugPlatformInfo(){
  2266. #ifdef ESP8266
  2267. system_print_meminfo();
  2268. DEBUG_WM(F("getCoreVersion(): "),ESP.getCoreVersion());
  2269. DEBUG_WM(F("system_get_sdk_version(): "),system_get_sdk_version());
  2270. DEBUG_WM(F("system_get_boot_version():"),system_get_boot_version());
  2271. DEBUG_WM(F("getFreeHeap(): "),(String)ESP.getFreeHeap());
  2272. #elif defined(ESP32)
  2273. size_t freeHeap = heap_caps_get_free_size(MALLOC_CAP_8BIT);
  2274. DEBUG_WM("Free heap: ", freeHeap);
  2275. DEBUG_WM("ESP-IDF version: ", esp_get_idf_version());
  2276. #endif
  2277. }
  2278. int WiFiManager::getRSSIasQuality(int RSSI) {
  2279. int quality = 0;
  2280. if (RSSI <= -100) {
  2281. quality = 0;
  2282. } else if (RSSI >= -50) {
  2283. quality = 100;
  2284. } else {
  2285. quality = 2 * (RSSI + 100);
  2286. }
  2287. return quality;
  2288. }
  2289. /** Is this an IP? */
  2290. boolean WiFiManager::isIp(String str) {
  2291. for (size_t i = 0; i < str.length(); i++) {
  2292. int c = str.charAt(i);
  2293. if (c != '.' && (c < '0' || c > '9')) {
  2294. return false;
  2295. }
  2296. }
  2297. return true;
  2298. }
  2299. /** IP to String? */
  2300. String WiFiManager::toStringIp(IPAddress ip) {
  2301. String res = "";
  2302. for (int i = 0; i < 3; i++) {
  2303. res += String((ip >> (8 * i)) & 0xFF) + ".";
  2304. }
  2305. res += String(((ip >> 8 * 3)) & 0xFF);
  2306. return res;
  2307. }
  2308. boolean WiFiManager::validApPassword(){
  2309. // check that ap password is valid, return false
  2310. if (_apPassword == NULL) _apPassword = "";
  2311. if (_apPassword != "") {
  2312. if (_apPassword.length() < 8 || _apPassword.length() > 63) {
  2313. DEBUG_WM(F("AccessPoint set password is INVALID or <8 chars"));
  2314. _apPassword = "";
  2315. return false; // @todo FATAL or fallback to empty ?
  2316. }
  2317. DEBUG_WM(DEBUG_VERBOSE,F("AccessPoint set password is VALID"));
  2318. DEBUG_WM(_apPassword);
  2319. }
  2320. return true;
  2321. }
  2322. /**
  2323. * encode htmlentities
  2324. * @since $dev
  2325. * @param string str string to replace entities
  2326. * @return string encoded string
  2327. */
  2328. String WiFiManager::htmlEntities(String str) {
  2329. str.replace("&","&amp;");
  2330. str.replace("<","&lt;");
  2331. str.replace(">","&gt;");
  2332. // str.replace("'","&#39;");
  2333. // str.replace("\"","&quot;");
  2334. // str.replace("/": "&#x2F;");
  2335. // str.replace("`": "&#x60;");
  2336. // str.replace("=": "&#x3D;");
  2337. return str;
  2338. }
  2339. /**
  2340. * [getWLStatusString description]
  2341. * @access public
  2342. * @param {[type]} uint8_t status [description]
  2343. * @return {[type]} [description]
  2344. */
  2345. String WiFiManager::getWLStatusString(uint8_t status){
  2346. if(status <= 7) return WIFI_STA_STATUS[status];
  2347. return FPSTR(S_NA);
  2348. }
  2349. String WiFiManager::encryptionTypeStr(uint8_t authmode) {
  2350. // DEBUG_WM("enc_tye: ",authmode);
  2351. return AUTH_MODE_NAMES[authmode];
  2352. }
  2353. String WiFiManager::getModeString(uint8_t mode){
  2354. if(mode <= 3) return WIFI_MODES[mode];
  2355. return FPSTR(S_NA);
  2356. }
  2357. bool WiFiManager::WiFiSetCountry(){
  2358. if(_wificountry == "") return false; // skip not set
  2359. bool ret = false;
  2360. #ifdef ESP32
  2361. // @todo check if wifi is init, no idea how, doesnt seem to be exposed atm ( might be now! )
  2362. if(WiFi.getMode() == WIFI_MODE_NULL); // exception if wifi not init!
  2363. else if(_wificountry == "US") ret = esp_wifi_set_country(&WM_COUNTRY_US) == ESP_OK;
  2364. else if(_wificountry == "JP") ret = esp_wifi_set_country(&WM_COUNTRY_JP) == ESP_OK;
  2365. else if(_wificountry == "CN") ret = esp_wifi_set_country(&WM_COUNTRY_CN) == ESP_OK;
  2366. else DEBUG_WM(DEBUG_ERROR,"[ERROR] country code not found");
  2367. #elif defined(ESP8266)
  2368. // if(WiFi.getMode() == WIFI_OFF); // exception if wifi not init!
  2369. if(_wificountry == "US") ret = wifi_set_country((wifi_country_t*)&WM_COUNTRY_US);
  2370. else if(_wificountry == "JP") ret = wifi_set_country((wifi_country_t*)&WM_COUNTRY_JP);
  2371. else if(_wificountry == "CN") ret = wifi_set_country((wifi_country_t*)&WM_COUNTRY_CN);
  2372. else DEBUG_WM(DEBUG_ERROR,"[ERROR] country code not found");
  2373. #endif
  2374. if(ret) DEBUG_WM(DEBUG_VERBOSE,"esp_wifi_set_country: " + _wificountry);
  2375. else DEBUG_WM(DEBUG_ERROR,"[ERROR] esp_wifi_set_country failed");
  2376. return ret;
  2377. }
  2378. // set mode ignores WiFi.persistent
  2379. bool WiFiManager::WiFi_Mode(WiFiMode_t m,bool persistent) {
  2380. bool ret;
  2381. #ifdef ESP8266
  2382. if((wifi_get_opmode() == (uint8) m ) && !persistent) {
  2383. return true;
  2384. }
  2385. ETS_UART_INTR_DISABLE();
  2386. if(persistent) ret = wifi_set_opmode(m);
  2387. else ret = wifi_set_opmode_current(m);
  2388. ETS_UART_INTR_ENABLE();
  2389. return ret;
  2390. #elif defined(ESP32)
  2391. if(persistent && esp32persistent) WiFi.persistent(true);
  2392. ret = WiFi.mode(m); // @todo persistent check persistant mode , NI
  2393. if(persistent && esp32persistent) WiFi.persistent(false);
  2394. return ret;
  2395. #endif
  2396. }
  2397. bool WiFiManager::WiFi_Mode(WiFiMode_t m) {
  2398. return WiFi_Mode(m,false);
  2399. }
  2400. // sta disconnect without persistent
  2401. bool WiFiManager::WiFi_Disconnect() {
  2402. #ifdef ESP8266
  2403. if((WiFi.getMode() & WIFI_STA) != 0) {
  2404. bool ret;
  2405. DEBUG_WM(DEBUG_DEV,F("WIFI station disconnect"));
  2406. ETS_UART_INTR_DISABLE(); // @todo probably not needed
  2407. ret = wifi_station_disconnect();
  2408. ETS_UART_INTR_ENABLE();
  2409. return ret;
  2410. }
  2411. #elif defined(ESP32)
  2412. DEBUG_WM(DEBUG_DEV,F("WIFI station disconnect"));
  2413. return WiFi.disconnect(); // not persistent atm
  2414. #endif
  2415. return false;
  2416. }
  2417. // toggle STA without persistent
  2418. bool WiFiManager::WiFi_enableSTA(bool enable,bool persistent) {
  2419. DEBUG_WM(DEBUG_DEV,F("WiFi station enable"));
  2420. #ifdef ESP8266
  2421. WiFiMode_t newMode;
  2422. WiFiMode_t currentMode = WiFi.getMode();
  2423. bool isEnabled = (currentMode & WIFI_STA) != 0;
  2424. if(enable) newMode = (WiFiMode_t)(currentMode | WIFI_STA);
  2425. else newMode = (WiFiMode_t)(currentMode & (~WIFI_STA));
  2426. if((isEnabled != enable) || persistent) {
  2427. if(enable) {
  2428. if(persistent) DEBUG_WM(DEBUG_DEV,F("enableSTA PERSISTENT ON"));
  2429. return WiFi_Mode(newMode,persistent);
  2430. }
  2431. else {
  2432. return WiFi_Mode(newMode,persistent);
  2433. }
  2434. } else {
  2435. return true;
  2436. }
  2437. #elif defined(ESP32)
  2438. bool ret;
  2439. if(persistent && esp32persistent) WiFi.persistent(true);
  2440. ret = WiFi.enableSTA(enable); // @todo handle persistent when it is implemented in platform
  2441. if(persistent && esp32persistent) WiFi.persistent(false);
  2442. return ret;
  2443. #endif
  2444. }
  2445. bool WiFiManager::WiFi_enableSTA(bool enable) {
  2446. return WiFi_enableSTA(enable,false);
  2447. }
  2448. bool WiFiManager::WiFi_eraseConfig() {
  2449. #ifdef ESP8266
  2450. #ifndef WM_FIXERASECONFIG
  2451. return ESP.eraseConfig();
  2452. #else
  2453. // erase config BUG replacement
  2454. // https://github.com/esp8266/Arduino/pull/3635
  2455. const size_t cfgSize = 0x4000;
  2456. size_t cfgAddr = ESP.getFlashChipSize() - cfgSize;
  2457. for (size_t offset = 0; offset < cfgSize; offset += SPI_FLASH_SEC_SIZE) {
  2458. if (!ESP.flashEraseSector((cfgAddr + offset) / SPI_FLASH_SEC_SIZE)) {
  2459. return false;
  2460. }
  2461. }
  2462. return true;
  2463. #endif
  2464. #elif defined(ESP32)
  2465. bool ret;
  2466. WiFi.mode(WIFI_AP_STA); // cannot erase if not in STA mode !
  2467. WiFi.persistent(true);
  2468. ret = WiFi.disconnect(true,true);
  2469. WiFi.persistent(false);
  2470. return ret;
  2471. #endif
  2472. }
  2473. uint8_t WiFiManager::WiFi_softap_num_stations(){
  2474. #ifdef ESP8266
  2475. return wifi_softap_get_station_num();
  2476. #elif defined(ESP32)
  2477. return WiFi.softAPgetStationNum();
  2478. #endif
  2479. }
  2480. bool WiFiManager::WiFi_hasAutoConnect(){
  2481. return WiFi_SSID(true) != "";
  2482. }
  2483. String WiFiManager::WiFi_SSID(bool persistent) const{
  2484. #ifdef ESP8266
  2485. struct station_config conf;
  2486. if(persistent) wifi_station_get_config_default(&conf);
  2487. else wifi_station_get_config(&conf);
  2488. char tmp[33]; //ssid can be up to 32chars, => plus null term
  2489. memcpy(tmp, conf.ssid, sizeof(conf.ssid));
  2490. tmp[32] = 0; //nullterm in case of 32 char ssid
  2491. return String(reinterpret_cast<char*>(tmp));
  2492. #elif defined(ESP32)
  2493. if(persistent){
  2494. wifi_config_t conf;
  2495. esp_wifi_get_config(WIFI_IF_STA, &conf);
  2496. return String(reinterpret_cast<const char*>(conf.sta.ssid));
  2497. }
  2498. else {
  2499. if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
  2500. return String();
  2501. }
  2502. wifi_ap_record_t info;
  2503. if(!esp_wifi_sta_get_ap_info(&info)) {
  2504. return String(reinterpret_cast<char*>(info.ssid));
  2505. }
  2506. return String();
  2507. }
  2508. #endif
  2509. }
  2510. String WiFiManager::WiFi_psk(bool persistent) const {
  2511. #ifdef ESP8266
  2512. struct station_config conf;
  2513. if(persistent) wifi_station_get_config_default(&conf);
  2514. else wifi_station_get_config(&conf);
  2515. char tmp[65]; //psk is 64 bytes hex => plus null term
  2516. memcpy(tmp, conf.password, sizeof(conf.password));
  2517. tmp[64] = 0; //null term in case of 64 byte psk
  2518. return String(reinterpret_cast<char*>(tmp));
  2519. #elif defined(ESP32)
  2520. // only if wifi is init
  2521. if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
  2522. return String();
  2523. }
  2524. wifi_config_t conf;
  2525. esp_wifi_get_config(WIFI_IF_STA, &conf);
  2526. return String(reinterpret_cast<char*>(conf.sta.password));
  2527. #endif
  2528. }
  2529. #ifdef ESP32
  2530. void WiFiManager::WiFiEvent(WiFiEvent_t event,system_event_info_t info){
  2531. if(!_hasBegun){
  2532. // DEBUG_WM(DEBUG_VERBOSE,"[ERROR] WiFiEvent, not ready");
  2533. return;
  2534. }
  2535. // DEBUG_WM(DEBUG_VERBOSE,"[EVENT]",event);
  2536. if(event == SYSTEM_EVENT_STA_DISCONNECTED){
  2537. DEBUG_WM(DEBUG_VERBOSE,"[EVENT] WIFI_REASON:",info.disconnected.reason);
  2538. if(info.disconnected.reason == WIFI_REASON_AUTH_EXPIRE || info.disconnected.reason == WIFI_REASON_AUTH_FAIL){
  2539. _lastconxresulttmp = 7; // hack in wrong password internally, sdk emit WIFI_REASON_AUTH_EXPIRE on some routers on auth_fail
  2540. } else _lastconxresulttmp = WiFi.status();
  2541. if(info.disconnected.reason == WIFI_REASON_NO_AP_FOUND) DEBUG_WM(DEBUG_VERBOSE,"[EVENT] WIFI_REASON: NO_AP_FOUND");
  2542. #ifdef esp32autoreconnect
  2543. DEBUG_WM(DEBUG_VERBOSE,"[Event] SYSTEM_EVENT_STA_DISCONNECTED, reconnecting");
  2544. WiFi.reconnect();
  2545. #endif
  2546. }
  2547. else if(event == SYSTEM_EVENT_SCAN_DONE){
  2548. uint16_t scans = WiFi.scanComplete();
  2549. WiFi_scanComplete(scans);
  2550. }
  2551. }
  2552. #endif
  2553. void WiFiManager::WiFi_autoReconnect(){
  2554. #ifdef ESP8266
  2555. WiFi.setAutoReconnect(_wifiAutoReconnect);
  2556. #elif defined(ESP32)
  2557. // if(_wifiAutoReconnect){
  2558. // @todo move to seperate method, used for event listener now
  2559. DEBUG_WM(DEBUG_VERBOSE,"ESP32 event handler enabled");
  2560. using namespace std::placeholders;
  2561. WiFi.onEvent(std::bind(&WiFiManager::WiFiEvent,this,_1,_2));
  2562. // }
  2563. #endif
  2564. }
  2565. #endif