ESPAsyncWiFiManager.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /**************************************************************
  2. AsyncWiFiManager is a library for the ESP8266/Arduino platform
  3. (https://github.com/esp8266/Arduino) to enable easy
  4. configuration and reconfiguration of WiFi credentials using a Captive Portal
  5. inspired by:
  6. http://www.esp8266.com/viewtopic.php?f=29&t=2520
  7. https://github.com/chriscook8/esp-arduino-apboot
  8. https://github.com/esp8266/Arduino/tree/esp8266/hardware/esp8266com/esp8266/libraries/DNSServer/examples/CaptivePortalAdvanced
  9. Built by AlexT https://github.com/tzapu
  10. Ported to Async Web Server by https://github.com/alanswx
  11. Licensed under MIT license
  12. **************************************************************/
  13. #include "ESPAsyncWiFiManager.h"
  14. AsyncWiFiManagerParameter::AsyncWiFiManagerParameter(const char *custom) {
  15. _id = NULL;
  16. _placeholder = NULL;
  17. _length = 0;
  18. _value = NULL;
  19. _customHTML = custom;
  20. }
  21. AsyncWiFiManagerParameter::AsyncWiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length) {
  22. init(id, placeholder, defaultValue, length, "");
  23. }
  24. AsyncWiFiManagerParameter::AsyncWiFiManagerParameter(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom) {
  25. init(id, placeholder, defaultValue, length, custom);
  26. }
  27. void AsyncWiFiManagerParameter::init(const char *id, const char *placeholder, const char *defaultValue, int length, const char *custom) {
  28. _id = id;
  29. _placeholder = placeholder;
  30. _length = length;
  31. _value = new char[length + 1];
  32. for (int i = 0; i < length; i++) {
  33. _value[i] = 0;
  34. }
  35. if (defaultValue != NULL) {
  36. strncpy(_value, defaultValue, length);
  37. }
  38. _customHTML = custom;
  39. }
  40. const char* AsyncWiFiManagerParameter::getValue() {
  41. return _value;
  42. }
  43. const char* AsyncWiFiManagerParameter::getID() {
  44. return _id;
  45. }
  46. const char* AsyncWiFiManagerParameter::getPlaceholder() {
  47. return _placeholder;
  48. }
  49. int AsyncWiFiManagerParameter::getValueLength() {
  50. return _length;
  51. }
  52. const char* AsyncWiFiManagerParameter::getCustomHTML() {
  53. return _customHTML;
  54. }
  55. #ifdef USE_EADNS
  56. AsyncWiFiManager::AsyncWiFiManager(AsyncWebServer *server, AsyncDNSServer *dns) :server(server), dnsServer(dns) {
  57. #else
  58. AsyncWiFiManager::AsyncWiFiManager(AsyncWebServer *server, DNSServer *dns) :server(server), dnsServer(dns) {
  59. #endif
  60. wifiSSIDs = NULL;
  61. wifiSSIDscan=true;
  62. _modeless=false;
  63. shouldscan=true;
  64. }
  65. void AsyncWiFiManager::addParameter(AsyncWiFiManagerParameter *p) {
  66. _params[_paramsCount] = p;
  67. _paramsCount++;
  68. DEBUG_WM("Adding parameter");
  69. DEBUG_WM(p->getID());
  70. }
  71. void AsyncWiFiManager::setupConfigPortal() {
  72. // dnsServer.reset(new DNSServer());
  73. // server.reset(new ESP8266WebServer(80));
  74. server->reset();
  75. DEBUG_WM(F(""));
  76. _configPortalStart = millis();
  77. DEBUG_WM(F("Configuring access point... "));
  78. DEBUG_WM(_apName);
  79. if (_apPassword != NULL) {
  80. if (strlen(_apPassword) < 8 || strlen(_apPassword) > 63) {
  81. // fail passphrase to short or long!
  82. DEBUG_WM(F("Invalid AccessPoint password. Ignoring"));
  83. _apPassword = NULL;
  84. }
  85. DEBUG_WM(_apPassword);
  86. }
  87. //optional soft ip config
  88. if (_ap_static_ip) {
  89. DEBUG_WM(F("Custom AP IP/GW/Subnet"));
  90. WiFi.softAPConfig(_ap_static_ip, _ap_static_gw, _ap_static_sn);
  91. }
  92. if (_apPassword != NULL) {
  93. WiFi.softAP(_apName, _apPassword);//password option
  94. } else {
  95. WiFi.softAP(_apName);
  96. }
  97. delay(500); // Without delay I've seen the IP address blank
  98. DEBUG_WM(F("AP IP address: "));
  99. DEBUG_WM(WiFi.softAPIP());
  100. /* Setup the DNS server redirecting all the domains to the apIP */
  101. #ifdef USE_EADNS
  102. dnsServer->setErrorReplyCode(AsyncDNSReplyCode::NoError);
  103. #else
  104. dnsServer->setErrorReplyCode(DNSReplyCode::NoError);
  105. #endif
  106. if (!dnsServer->start(DNS_PORT, "*", WiFi.softAPIP())) {
  107. DEBUG_WM(F("Could not start Captive DNS Server!"));
  108. }
  109. setInfo();
  110. /* Setup web pages: root, wifi config pages, SO captive portal detectors and not found. */
  111. server->on("/", std::bind(&AsyncWiFiManager::handleRoot, this,std::placeholders::_1)).setFilter(ON_AP_FILTER);
  112. server->on("/wifi", std::bind(&AsyncWiFiManager::handleWifi, this, std::placeholders::_1,true)).setFilter(ON_AP_FILTER);
  113. server->on("/0wifi", std::bind(&AsyncWiFiManager::handleWifi, this,std::placeholders::_1, false)).setFilter(ON_AP_FILTER);
  114. server->on("/wifisave", std::bind(&AsyncWiFiManager::handleWifiSave,this,std::placeholders::_1)).setFilter(ON_AP_FILTER);
  115. server->on("/i", std::bind(&AsyncWiFiManager::handleInfo,this, std::placeholders::_1)).setFilter(ON_AP_FILTER);
  116. server->on("/r", std::bind(&AsyncWiFiManager::handleReset, this,std::placeholders::_1)).setFilter(ON_AP_FILTER);
  117. //server->on("/generate_204", std::bind(&AsyncWiFiManager::handle204, this)); //Android/Chrome OS captive portal check.
  118. server->on("/fwlink", std::bind(&AsyncWiFiManager::handleRoot, this,std::placeholders::_1)).setFilter(ON_AP_FILTER); //Microsoft captive portal. Maybe not needed. Might be handled by notFound handler.
  119. server->onNotFound (std::bind(&AsyncWiFiManager::handleNotFound,this,std::placeholders::_1));
  120. server->begin(); // Web server start
  121. DEBUG_WM(F("HTTP server started"));
  122. }
  123. static const char HEX_CHAR_ARRAY[17] = "0123456789ABCDEF";
  124. /**
  125. * convert char array (hex values) to readable string by seperator
  126. * buf: buffer to convert
  127. * length: data length
  128. * strSeperator seperator between each hex value
  129. * return: formated value as String
  130. */
  131. static String byteToHexString(uint8_t* buf, uint8_t length, String strSeperator="-") {
  132. String dataString = "";
  133. for (uint8_t i = 0; i < length; i++) {
  134. byte v = buf[i] / 16;
  135. byte w = buf[i] % 16;
  136. if (i>0) {
  137. dataString += strSeperator;
  138. }
  139. dataString += String(HEX_CHAR_ARRAY[v]);
  140. dataString += String(HEX_CHAR_ARRAY[w]);
  141. }
  142. dataString.toUpperCase();
  143. return dataString;
  144. } // byteToHexString
  145. #if !defined(ESP8266)
  146. String getESP32ChipID() {
  147. uint64_t chipid;
  148. chipid=ESP.getEfuseMac();//The chip ID is essentially its MAC address(length: 6 bytes).
  149. int chipid_size = 6;
  150. uint8_t chipid_arr[chipid_size];
  151. for (uint8_t i=0; i < chipid_size; i++) {
  152. chipid_arr[i] = (chipid >> (8 * i)) & 0xff;
  153. }
  154. return byteToHexString(chipid_arr, chipid_size, "");
  155. }
  156. #endif
  157. boolean AsyncWiFiManager::autoConnect(unsigned long maxConnectRetries, unsigned long retryDelayMs) {
  158. String ssid = "ESP";
  159. #if defined(ESP8266)
  160. ssid += String(ESP.getChipId());
  161. #else
  162. ssid += getESP32ChipID();
  163. #endif
  164. return autoConnect(ssid.c_str(), NULL);
  165. }
  166. boolean AsyncWiFiManager::autoConnect(char const *apName, char const *apPassword, unsigned long maxConnectRetries, unsigned long retryDelayMs) {
  167. DEBUG_WM(F(""));
  168. // read eeprom for ssid and pass
  169. //String ssid = getSSID();
  170. //String pass = getPassword();
  171. // attempt to connect; should it fail, fall back to AP
  172. WiFi.mode(WIFI_STA);
  173. for(unsigned long tryNumber = 0; tryNumber < maxConnectRetries; tryNumber++) {
  174. DEBUG_WM(F("AutoConnect Try No.:"));
  175. DEBUG_WM(tryNumber);
  176. if (connectWifi("", "") == WL_CONNECTED) {
  177. DEBUG_WM(F("IP Address:"));
  178. DEBUG_WM(WiFi.localIP());
  179. //connected
  180. return true;
  181. }
  182. if(tryNumber + 1 < maxConnectRetries) {
  183. // we might connect during the delay
  184. unsigned long restDelayMs = retryDelayMs;
  185. while(restDelayMs != 0) {
  186. if(WiFi.status() == WL_CONNECTED) {
  187. DEBUG_WM(F("IP Address (connected during delay):"));
  188. DEBUG_WM(WiFi.localIP());
  189. return true;
  190. }
  191. unsigned long thisDelay = std::min(restDelayMs, 100ul);
  192. delay(thisDelay);
  193. restDelayMs -= thisDelay;
  194. }
  195. }
  196. }
  197. return startConfigPortal(apName, apPassword);
  198. }
  199. String AsyncWiFiManager::networkListAsString()
  200. {
  201. String pager ;
  202. //display networks in page
  203. for (int i = 0; i < wifiSSIDCount; i++) {
  204. if (wifiSSIDs[i].duplicate == true) continue; // skip dups
  205. int quality = getRSSIasQuality(wifiSSIDs[i].RSSI);
  206. if (_minimumQuality == -1 || _minimumQuality < quality) {
  207. String item = FPSTR(HTTP_ITEM);
  208. String rssiQ;
  209. rssiQ += quality;
  210. item.replace("{v}", wifiSSIDs[i].SSID);
  211. item.replace("{r}", rssiQ);
  212. #if defined(ESP8266)
  213. if (wifiSSIDs[i].encryptionType != ENC_TYPE_NONE) {
  214. #else
  215. if (wifiSSIDs[i].encryptionType != WIFI_AUTH_OPEN) {
  216. #endif
  217. item.replace("{i}", "l");
  218. } else {
  219. item.replace("{i}", "");
  220. }
  221. pager += item;
  222. } else {
  223. DEBUG_WM(F("Skipping due to quality"));
  224. }
  225. }
  226. return pager;
  227. }
  228. String AsyncWiFiManager::scanModal()
  229. {
  230. shouldscan=true;
  231. scan();
  232. String pager=networkListAsString();
  233. return pager;
  234. }
  235. void AsyncWiFiManager::scan(boolean async)
  236. {
  237. if (!shouldscan) return;
  238. DEBUG_WM(F("About to scan()"));
  239. if (wifiSSIDscan)
  240. {
  241. wifi_ssid_count_t n = WiFi.scanNetworks(async);
  242. copySSIDInfo(n);
  243. }
  244. }
  245. void AsyncWiFiManager::copySSIDInfo(wifi_ssid_count_t n) {
  246. if(n == WIFI_SCAN_FAILED) {
  247. DEBUG_WM(F("scanNetworks returned: WIFI_SCAN_FAILED!"));
  248. } else if(n == WIFI_SCAN_RUNNING) {
  249. DEBUG_WM(F("scanNetworks returned: WIFI_SCAN_RUNNING!"));
  250. } else if(n < 0) {
  251. DEBUG_WM(F("scanNetworks failed with unknown error code!"));
  252. } else if (n == 0) {
  253. DEBUG_WM(F("No networks found"));
  254. // page += F("No networks found. Refresh to scan again.");
  255. } else {
  256. DEBUG_WM(F("Scan done"));
  257. }
  258. if (n > 0) {
  259. /* WE SHOULD MOVE THIS IN PLACE ATOMICALLY */
  260. if (wifiSSIDs) delete [] wifiSSIDs;
  261. wifiSSIDs = new WiFiResult[n];
  262. wifiSSIDCount = n;
  263. if (n>0)
  264. shouldscan=false;
  265. for (wifi_ssid_count_t i=0;i<n;i++)
  266. {
  267. wifiSSIDs[i].duplicate=false;
  268. #if defined(ESP8266)
  269. bool res=WiFi.getNetworkInfo(i, wifiSSIDs[i].SSID, wifiSSIDs[i].encryptionType, wifiSSIDs[i].RSSI, wifiSSIDs[i].BSSID, wifiSSIDs[i].channel, wifiSSIDs[i].isHidden);
  270. #else
  271. bool res=WiFi.getNetworkInfo(i, wifiSSIDs[i].SSID, wifiSSIDs[i].encryptionType, wifiSSIDs[i].RSSI, wifiSSIDs[i].BSSID, wifiSSIDs[i].channel);
  272. #endif
  273. }
  274. // RSSI SORT
  275. // old sort
  276. for (int i = 0; i < n; i++) {
  277. for (int j = i + 1; j < n; j++) {
  278. if (wifiSSIDs[j].RSSI > wifiSSIDs[i].RSSI) {
  279. std::swap(wifiSSIDs[i], wifiSSIDs[j]);
  280. }
  281. }
  282. }
  283. // remove duplicates ( must be RSSI sorted )
  284. if (_removeDuplicateAPs) {
  285. String cssid;
  286. for (int i = 0; i < n; i++) {
  287. if (wifiSSIDs[i].duplicate == true) continue;
  288. cssid = wifiSSIDs[i].SSID;
  289. for (int j = i + 1; j < n; j++) {
  290. if (cssid == wifiSSIDs[j].SSID) {
  291. DEBUG_WM("DUP AP: " +wifiSSIDs[j].SSID);
  292. wifiSSIDs[j].duplicate=true; // set dup aps to NULL
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }
  299. void AsyncWiFiManager::startConfigPortalModeless(char const *apName, char const *apPassword) {
  300. _modeless =true;
  301. _apName = apName;
  302. _apPassword = apPassword;
  303. /*
  304. AJS - do we want this?
  305. */
  306. //setup AP
  307. WiFi.mode(WIFI_AP_STA);
  308. DEBUG_WM("SET AP STA");
  309. // try to connect
  310. if (connectWifi("", "") == WL_CONNECTED) {
  311. DEBUG_WM(F("IP Address:"));
  312. DEBUG_WM(WiFi.localIP());
  313. //connected
  314. // call the callback!
  315. if ( _savecallback != NULL) {
  316. //todo: check if any custom parameters actually exist, and check if they really changed maybe
  317. _savecallback();
  318. }
  319. }
  320. //notify we entered AP mode
  321. if ( _apcallback != NULL) {
  322. _apcallback(this);
  323. }
  324. connect = false;
  325. setupConfigPortal();
  326. scannow= 0 ;
  327. }
  328. void AsyncWiFiManager::loop(){
  329. safeLoop();
  330. criticalLoop();
  331. }
  332. void AsyncWiFiManager::setInfo() {
  333. if (needInfo) {
  334. pager = infoAsString();
  335. wifiStatus = WiFi.status();
  336. needInfo = false;
  337. }
  338. }
  339. /**
  340. * Anything that accesses WiFi, ESP or EEPROM goes here
  341. */
  342. void AsyncWiFiManager::criticalLoop(){
  343. if (_modeless)
  344. {
  345. if (scannow==0 || millis() - scannow >= 60000)
  346. {
  347. scannow= millis() ;
  348. scan(true);
  349. }
  350. wifi_ssid_count_t n = WiFi.scanComplete();
  351. if(n >= 0)
  352. {
  353. copySSIDInfo(n);
  354. WiFi.scanDelete();
  355. }
  356. if (connect) {
  357. connect = false;
  358. //delay(2000);
  359. DEBUG_WM(F("Connecting to new AP"));
  360. // using user-provided _ssid, _pass in place of system-stored ssid and pass
  361. if (connectWifi(_ssid, _pass) != WL_CONNECTED) {
  362. DEBUG_WM(F("Failed to connect."));
  363. } else {
  364. //connected
  365. // alanswx - should we have a config to decide if we should shut down AP?
  366. // WiFi.mode(WIFI_STA);
  367. //notify that configuration has changed and any optional parameters should be saved
  368. if ( _savecallback != NULL) {
  369. //todo: check if any custom parameters actually exist, and check if they really changed maybe
  370. _savecallback();
  371. }
  372. return;
  373. }
  374. if (_shouldBreakAfterConfig) {
  375. //flag set to exit after config after trying to connect
  376. //notify that configuration has changed and any optional parameters should be saved
  377. if ( _savecallback != NULL) {
  378. //todo: check if any custom parameters actually exist, and check if they really changed maybe
  379. _savecallback();
  380. }
  381. }
  382. }
  383. }
  384. }
  385. /*
  386. * Anything that doesn't access WiFi, ESP or EEPROM can go here
  387. */
  388. void AsyncWiFiManager::safeLoop(){
  389. #ifndef USE_EADNS
  390. dnsServer->processNextRequest();
  391. #endif
  392. }
  393. boolean AsyncWiFiManager::startConfigPortal(char const *apName, char const *apPassword) {
  394. //setup AP
  395. WiFi.mode(WIFI_AP_STA);
  396. DEBUG_WM("SET AP STA");
  397. _apName = apName;
  398. _apPassword = apPassword;
  399. //notify we entered AP mode
  400. if ( _apcallback != NULL) {
  401. _apcallback(this);
  402. }
  403. connect = false;
  404. setupConfigPortal();
  405. scannow= 0 ;
  406. while (_configPortalTimeout == 0 || millis() - _configPortalStart < _configPortalTimeout) {
  407. //DNS
  408. #ifndef USE_EADNS
  409. dnsServer->processNextRequest();
  410. #endif
  411. //
  412. // we should do a scan every so often here and
  413. // try to reconnect to AP while we are at it
  414. //
  415. if ( scannow == 0 || millis() - scannow >= 10000)
  416. {
  417. DEBUG_WM(F("About to scan()"));
  418. shouldscan=true; // since we are modal, we can scan every time
  419. #if defined(ESP8266)
  420. // we might still be connecting, so that has to stop for scanning
  421. ETS_UART_INTR_DISABLE ();
  422. wifi_station_disconnect ();
  423. ETS_UART_INTR_ENABLE ();
  424. #else
  425. WiFi.disconnect (false);
  426. #endif
  427. scanModal();
  428. if(_tryConnectDuringConfigPortal) WiFi.begin(); // try to reconnect to AP
  429. scannow= millis() ;
  430. }
  431. // attempts to reconnect were successful
  432. if(WiFi.status() == WL_CONNECTED) {
  433. //connected
  434. WiFi.mode(WIFI_STA);
  435. //notify that configuration has changed and any optional parameters should be saved
  436. if ( _savecallback != NULL) {
  437. //todo: check if any custom parameters actually exist, and check if they really changed maybe
  438. _savecallback();
  439. }
  440. break;
  441. }
  442. if (connect) {
  443. connect = false;
  444. delay(2000);
  445. DEBUG_WM(F("Connecting to new AP"));
  446. // using user-provided _ssid, _pass in place of system-stored ssid and pass
  447. if (connectWifi(_ssid, _pass) == WL_CONNECTED) {
  448. //connected
  449. WiFi.mode(WIFI_STA);
  450. //notify that configuration has changed and any optional parameters should be saved
  451. if ( _savecallback != NULL) {
  452. //todo: check if any custom parameters actually exist, and check if they really changed maybe
  453. _savecallback();
  454. }
  455. break;
  456. } else {
  457. DEBUG_WM(F("Failed to connect."));
  458. }
  459. if (_shouldBreakAfterConfig) {
  460. //flag set to exit after config after trying to connect
  461. //notify that configuration has changed and any optional parameters should be saved
  462. if ( _savecallback != NULL) {
  463. //todo: check if any custom parameters actually exist, and check if they really changed maybe
  464. _savecallback();
  465. }
  466. break;
  467. }
  468. }
  469. yield();
  470. }
  471. server->reset();
  472. dnsServer->stop();
  473. return WiFi.status() == WL_CONNECTED;
  474. }
  475. int AsyncWiFiManager::connectWifi(String ssid, String pass) {
  476. DEBUG_WM(F("Connecting as wifi client..."));
  477. // check if we've got static_ip settings, if we do, use those.
  478. if (_sta_static_ip) {
  479. DEBUG_WM(F("Custom STA IP/GW/Subnet/DNS"));
  480. WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn, _sta_static_dns1, _sta_static_dns2);
  481. DEBUG_WM(WiFi.localIP());
  482. }
  483. //fix for auto connect racing issue
  484. // if (WiFi.status() == WL_CONNECTED) {
  485. // DEBUG_WM("Already connected. Bailing out.");
  486. // return WL_CONNECTED;
  487. // }
  488. //check if we have ssid and pass and force those, if not, try with last saved values
  489. if (ssid != "") {
  490. #if defined(ESP8266)
  491. //trying to fix connection in progress hanging
  492. ETS_UART_INTR_DISABLE();
  493. wifi_station_disconnect();
  494. ETS_UART_INTR_ENABLE();
  495. #else
  496. WiFi.disconnect(false);
  497. #endif
  498. WiFi.begin(ssid.c_str(), pass.c_str());
  499. } else {
  500. if (WiFi.SSID().length() > 0) {
  501. DEBUG_WM("Using last saved values, should be faster");
  502. #if defined(ESP8266)
  503. //trying to fix connection in progress hanging
  504. ETS_UART_INTR_DISABLE();
  505. wifi_station_disconnect();
  506. ETS_UART_INTR_ENABLE();
  507. #else
  508. WiFi.disconnect(false);
  509. #endif
  510. WiFi.begin();
  511. } else {
  512. DEBUG_WM("Try to connect with saved credentials");
  513. WiFi.begin();
  514. }
  515. }
  516. int connRes = waitForConnectResult();
  517. DEBUG_WM ("Connection result: ");
  518. DEBUG_WM ( connRes );
  519. //not connected, WPS enabled, no pass - first attempt
  520. #ifdef NO_EXTRA_4K_HEAP
  521. if (_tryWPS && connRes != WL_CONNECTED && pass == "") {
  522. startWPS();
  523. //should be connected at the end of WPS
  524. connRes = waitForConnectResult();
  525. }
  526. #endif
  527. needInfo = true;
  528. setInfo();
  529. return connRes;
  530. }
  531. uint8_t AsyncWiFiManager::waitForConnectResult() {
  532. if (_connectTimeout == 0) {
  533. return WiFi.waitForConnectResult();
  534. } else {
  535. DEBUG_WM (F("Waiting for connection result with time out"));
  536. unsigned long start = millis();
  537. boolean keepConnecting = true;
  538. uint8_t status;
  539. while (keepConnecting) {
  540. status = WiFi.status();
  541. if (millis() > start + _connectTimeout) {
  542. keepConnecting = false;
  543. DEBUG_WM (F("Connection timed out"));
  544. }
  545. if (status == WL_CONNECTED || status == WL_CONNECT_FAILED) {
  546. keepConnecting = false;
  547. }
  548. delay(100);
  549. }
  550. return status;
  551. }
  552. }
  553. #ifdef NO_EXTRA_4K_HEAP
  554. void AsyncWiFiManager::startWPS() {
  555. DEBUG_WM("START WPS");
  556. #if defined(ESP8266)
  557. WiFi.beginWPSConfig();
  558. #else
  559. //esp_wps_config_t config = WPS_CONFIG_INIT_DEFAULT(ESP_WPS_MODE);
  560. esp_wps_config_t config = {};
  561. config.wps_type = ESP_WPS_MODE;
  562. config.crypto_funcs = &g_wifi_default_wps_crypto_funcs;
  563. strcpy(config.factory_info.manufacturer,"ESPRESSIF");
  564. strcpy(config.factory_info.model_number, "ESP32");
  565. strcpy(config.factory_info.model_name, "ESPRESSIF IOT");
  566. strcpy(config.factory_info.device_name,"ESP STATION");
  567. esp_wifi_wps_enable(&config);
  568. esp_wifi_wps_start(0);
  569. #endif
  570. DEBUG_WM("END WPS");
  571. }
  572. #endif
  573. /*
  574. String AsyncWiFiManager::getSSID() {
  575. if (_ssid == "") {
  576. DEBUG_WM(F("Reading SSID"));
  577. _ssid = WiFi.SSID();
  578. DEBUG_WM(F("SSID: "));
  579. DEBUG_WM(_ssid);
  580. }
  581. return _ssid;
  582. }
  583. String AsyncWiFiManager::getPassword() {
  584. if (_pass == "") {
  585. DEBUG_WM(F("Reading Password"));
  586. _pass = WiFi.psk();
  587. DEBUG_WM("Password: " + _pass);
  588. //DEBUG_WM(_pass);
  589. }
  590. return _pass;
  591. }
  592. */
  593. String AsyncWiFiManager::getConfigPortalSSID() {
  594. return _apName;
  595. }
  596. void AsyncWiFiManager::resetSettings() {
  597. DEBUG_WM(F("settings invalidated"));
  598. DEBUG_WM(F("THIS MAY CAUSE AP NOT TO START UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA."));
  599. WiFi.disconnect(true);
  600. //delay(200);
  601. }
  602. void AsyncWiFiManager::setTimeout(unsigned long seconds) {
  603. setConfigPortalTimeout(seconds);
  604. }
  605. void AsyncWiFiManager::setConfigPortalTimeout(unsigned long seconds) {
  606. _configPortalTimeout = seconds * 1000;
  607. }
  608. void AsyncWiFiManager::setConnectTimeout(unsigned long seconds) {
  609. _connectTimeout = seconds * 1000;
  610. }
  611. void AsyncWiFiManager::setTryConnectDuringConfigPortal(boolean v) {
  612. _tryConnectDuringConfigPortal = v;
  613. }
  614. void AsyncWiFiManager::setDebugOutput(boolean debug) {
  615. _debug = debug;
  616. }
  617. void AsyncWiFiManager::setAPStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn) {
  618. _ap_static_ip = ip;
  619. _ap_static_gw = gw;
  620. _ap_static_sn = sn;
  621. }
  622. void AsyncWiFiManager::setSTAStaticIPConfig(IPAddress ip, IPAddress gw, IPAddress sn, IPAddress dns1, IPAddress dns2) {
  623. _sta_static_ip = ip;
  624. _sta_static_gw = gw;
  625. _sta_static_sn = sn;
  626. _sta_static_dns1 = dns1;
  627. _sta_static_dns2 = dns2;
  628. }
  629. void AsyncWiFiManager::setMinimumSignalQuality(int quality) {
  630. _minimumQuality = quality;
  631. }
  632. void AsyncWiFiManager::setBreakAfterConfig(boolean shouldBreak) {
  633. _shouldBreakAfterConfig = shouldBreak;
  634. }
  635. /** Handle root or redirect to captive portal */
  636. void AsyncWiFiManager::handleRoot(AsyncWebServerRequest *request) {
  637. // AJS - maybe we should set a scan when we get to the root???
  638. // and only scan on demand? timer + on demand? plus a link to make it happen?
  639. shouldscan=true;
  640. scannow= 0 ;
  641. DEBUG_WM(F("Handle root"));
  642. if (captivePortal(request)) { // If captive portal redirect instead of displaying the page.
  643. return;
  644. }
  645. DEBUG_WM(F("Sending Captive Portal"));
  646. String page = FPSTR(WFM_HTTP_HEAD);
  647. page.replace("{v}", "Options");
  648. page += FPSTR(HTTP_SCRIPT);
  649. page += FPSTR(HTTP_STYLE);
  650. page += _customHeadElement;
  651. page += FPSTR(HTTP_HEAD_END);
  652. page += "<h1>";
  653. page += _apName;
  654. page += "</h1>";
  655. page += F("<h3>AsyncWiFiManager</h3>");
  656. page += FPSTR(HTTP_PORTAL_OPTIONS);
  657. page += _customOptionsElement;
  658. page += FPSTR(HTTP_END);
  659. request->send(200, "text/html", page);
  660. DEBUG_WM(F("Sent..."));
  661. }
  662. /** Wifi config page handler */
  663. void AsyncWiFiManager::handleWifi(AsyncWebServerRequest *request,boolean scan) {
  664. shouldscan=true;
  665. scannow= 0 ;
  666. DEBUG_WM(F("Handle wifi"));
  667. String page = FPSTR(WFM_HTTP_HEAD);
  668. page.replace("{v}", "Config ESP");
  669. page += FPSTR(HTTP_SCRIPT);
  670. page += FPSTR(HTTP_STYLE);
  671. page += _customHeadElement;
  672. page += FPSTR(HTTP_HEAD_END);
  673. if (scan) {
  674. wifiSSIDscan=false;
  675. DEBUG_WM(F("Scan done"));
  676. if (wifiSSIDCount==0) {
  677. DEBUG_WM(F("No networks found"));
  678. page += F("No networks found. Refresh to scan again.");
  679. } else {
  680. //display networks in page
  681. String pager = networkListAsString();
  682. page += pager;
  683. page += "<br/>";
  684. }
  685. }
  686. wifiSSIDscan=true;
  687. page += FPSTR(HTTP_FORM_START);
  688. char parLength[2];
  689. // add the extra parameters to the form
  690. for (int i = 0; i < _paramsCount; i++) {
  691. if (_params[i] == NULL) {
  692. break;
  693. }
  694. String pitem = FPSTR(HTTP_FORM_PARAM);
  695. if (_params[i]->getID() != NULL) {
  696. pitem.replace("{i}", _params[i]->getID());
  697. pitem.replace("{n}", _params[i]->getID());
  698. pitem.replace("{p}", _params[i]->getPlaceholder());
  699. snprintf(parLength, 2, "%d", _params[i]->getValueLength());
  700. pitem.replace("{l}", parLength);
  701. pitem.replace("{v}", _params[i]->getValue());
  702. pitem.replace("{c}", _params[i]->getCustomHTML());
  703. } else {
  704. pitem = _params[i]->getCustomHTML();
  705. }
  706. page += pitem;
  707. }
  708. if (_params[0] != NULL) {
  709. page += "<br/>";
  710. }
  711. if (_sta_static_ip) {
  712. String item = FPSTR(HTTP_FORM_PARAM);
  713. item.replace("{i}", "ip");
  714. item.replace("{n}", "ip");
  715. item.replace("{p}", "Static IP");
  716. item.replace("{l}", "15");
  717. item.replace("{v}", _sta_static_ip.toString());
  718. page += item;
  719. item = FPSTR(HTTP_FORM_PARAM);
  720. item.replace("{i}", "gw");
  721. item.replace("{n}", "gw");
  722. item.replace("{p}", "Static Gateway");
  723. item.replace("{l}", "15");
  724. item.replace("{v}", _sta_static_gw.toString());
  725. page += item;
  726. item = FPSTR(HTTP_FORM_PARAM);
  727. item.replace("{i}", "sn");
  728. item.replace("{n}", "sn");
  729. item.replace("{p}", "Subnet");
  730. item.replace("{l}", "15");
  731. item.replace("{v}", _sta_static_sn.toString());
  732. page += item;
  733. item = FPSTR(HTTP_FORM_PARAM);
  734. item.replace("{i}", "dns1");
  735. item.replace("{n}", "dns1");
  736. item.replace("{p}", "DNS1");
  737. item.replace("{l}", "15");
  738. item.replace("{v}", _sta_static_dns1.toString());
  739. page += item;
  740. item = FPSTR(HTTP_FORM_PARAM);
  741. item.replace("{i}", "dns2");
  742. item.replace("{n}", "dns2");
  743. item.replace("{p}", "DNS2");
  744. item.replace("{l}", "15");
  745. item.replace("{v}", _sta_static_dns2.toString());
  746. page += item;
  747. page += "<br/>";
  748. }
  749. page += FPSTR(HTTP_FORM_END);
  750. page += FPSTR(HTTP_SCAN_LINK);
  751. page += FPSTR(HTTP_END);
  752. request->send(200, "text/html", page);
  753. DEBUG_WM(F("Sent config page"));
  754. }
  755. /** Handle the WLAN save form and redirect to WLAN config page again */
  756. void AsyncWiFiManager::handleWifiSave(AsyncWebServerRequest *request) {
  757. DEBUG_WM(F("WiFi save"));
  758. //SAVE/connect here
  759. needInfo = true;
  760. _ssid = request->arg("s").c_str();
  761. _pass = request->arg("p").c_str();
  762. //parameters
  763. for (int i = 0; i < _paramsCount; i++) {
  764. if (_params[i] == NULL) {
  765. break;
  766. }
  767. //read parameter
  768. String value = request->arg(_params[i]->getID()).c_str();
  769. //store it in array
  770. value.toCharArray(_params[i]->_value, _params[i]->_length);
  771. DEBUG_WM(F("Parameter"));
  772. DEBUG_WM(_params[i]->getID());
  773. DEBUG_WM(value);
  774. }
  775. if (request->hasArg("ip")) {
  776. DEBUG_WM(F("static ip"));
  777. DEBUG_WM(request->arg("ip"));
  778. //_sta_static_ip.fromString(request->arg("ip"));
  779. String ip = request->arg("ip");
  780. optionalIPFromString(&_sta_static_ip, ip.c_str());
  781. }
  782. if (request->hasArg("gw")) {
  783. DEBUG_WM(F("static gateway"));
  784. DEBUG_WM(request->arg("gw"));
  785. String gw = request->arg("gw");
  786. optionalIPFromString(&_sta_static_gw, gw.c_str());
  787. }
  788. if (request->hasArg("sn")) {
  789. DEBUG_WM(F("static netmask"));
  790. DEBUG_WM(request->arg("sn"));
  791. String sn = request->arg("sn");
  792. optionalIPFromString(&_sta_static_sn, sn.c_str());
  793. }
  794. if (request->hasArg("dns1")) {
  795. DEBUG_WM(F("static DNS 1"));
  796. DEBUG_WM(request->arg("dns1"));
  797. String dns1 = request->arg("dns1");
  798. optionalIPFromString(&_sta_static_dns1, dns1.c_str());
  799. }
  800. if (request->hasArg("dns2")) {
  801. DEBUG_WM(F("static DNS 2"));
  802. DEBUG_WM(request->arg("dns2"));
  803. String dns2 = request->arg("dns2");
  804. optionalIPFromString(&_sta_static_dns2, dns2.c_str());
  805. }
  806. String page = FPSTR(WFM_HTTP_HEAD);
  807. page.replace("{v}", "Credentials Saved");
  808. page += FPSTR(HTTP_SCRIPT);
  809. page += FPSTR(HTTP_STYLE);
  810. page += _customHeadElement;
  811. page += F("<meta http-equiv=\"refresh\" content=\"5; url=/i\">");
  812. page += FPSTR(HTTP_HEAD_END);
  813. page += FPSTR(HTTP_SAVED);
  814. page += FPSTR(HTTP_END);
  815. request->send(200, "text/html", page);
  816. DEBUG_WM(F("Sent wifi save page"));
  817. connect = true; //signal ready to connect/reset
  818. }
  819. /** Handle the info page */
  820. String AsyncWiFiManager::infoAsString()
  821. {
  822. String page;
  823. page += F("<dt>Chip ID</dt><dd>");
  824. #if defined(ESP8266)
  825. page += ESP.getChipId();
  826. #else
  827. page += getESP32ChipID();
  828. #endif
  829. page += F("</dd>");
  830. page += F("<dt>Flash Chip ID</dt><dd>");
  831. #if defined(ESP8266)
  832. page += ESP.getFlashChipId();
  833. #else
  834. page += F("N/A for ESP32");
  835. #endif
  836. page += F("</dd>");
  837. page += F("<dt>IDE Flash Size</dt><dd>");
  838. page += ESP.getFlashChipSize();
  839. page += F(" bytes</dd>");
  840. page += F("<dt>Real Flash Size</dt><dd>");
  841. #if defined(ESP8266)
  842. page += ESP.getFlashChipRealSize();
  843. #else
  844. page += F("N/A for ESP32");
  845. #endif
  846. page += F(" bytes</dd>");
  847. page += F("<dt>Soft AP IP</dt><dd>");
  848. page += WiFi.softAPIP().toString();
  849. page += F("</dd>");
  850. page += F("<dt>Soft AP MAC</dt><dd>");
  851. page += WiFi.softAPmacAddress();
  852. page += F("</dd>");
  853. page += F("<dt>Station SSID</dt><dd>");
  854. page += WiFi.SSID();
  855. page += F("</dd>");
  856. page += F("<dt>Station IP</dt><dd>");
  857. page += WiFi.localIP().toString();
  858. page += F("</dd>");
  859. page += F("<dt>Station MAC</dt><dd>");
  860. page += WiFi.macAddress();
  861. page += F("</dd>");
  862. page += F("</dl>");
  863. return page;
  864. }
  865. void AsyncWiFiManager::handleInfo(AsyncWebServerRequest *request) {
  866. DEBUG_WM(F("Info"));
  867. String page = FPSTR(WFM_HTTP_HEAD);
  868. page.replace("{v}", "Info");
  869. page += FPSTR(HTTP_SCRIPT);
  870. page += FPSTR(HTTP_STYLE);
  871. page += _customHeadElement;
  872. if (connect==true)
  873. page += F("<meta http-equiv=\"refresh\" content=\"5; url=/i\">");
  874. page += FPSTR(HTTP_HEAD_END);
  875. page += F("<dl>");
  876. if (connect==true)
  877. {
  878. page += F("<dt>Trying to connect</dt><dd>");
  879. page += wifiStatus;
  880. page += F("</dd>");
  881. }
  882. page +=pager;
  883. page += FPSTR(HTTP_END);
  884. request->send(200, "text/html", page);
  885. DEBUG_WM(F("Sent info page"));
  886. }
  887. /** Handle the reset page */
  888. void AsyncWiFiManager::handleReset(AsyncWebServerRequest *request) {
  889. DEBUG_WM(F("Reset"));
  890. String page = FPSTR(WFM_HTTP_HEAD);
  891. page.replace("{v}", "Info");
  892. page += FPSTR(HTTP_SCRIPT);
  893. page += FPSTR(HTTP_STYLE);
  894. page += _customHeadElement;
  895. page += FPSTR(HTTP_HEAD_END);
  896. page += F("Module will reset in a few seconds.");
  897. page += FPSTR(HTTP_END);
  898. request->send(200, "text/html", page);
  899. DEBUG_WM(F("Sent reset page"));
  900. delay(5000);
  901. #if defined(ESP8266)
  902. ESP.reset();
  903. #else
  904. ESP.restart();
  905. #endif
  906. delay(2000);
  907. }
  908. //removed as mentioned here https://github.com/tzapu/AsyncWiFiManager/issues/114
  909. /*void AsyncWiFiManager::handle204(AsyncWebServerRequest *request) {
  910. DEBUG_WM(F("204 No Response"));
  911. request->sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
  912. request->sendHeader("Pragma", "no-cache");
  913. request->sendHeader("Expires", "-1");
  914. request->send ( 204, "text/plain", "");
  915. }*/
  916. void AsyncWiFiManager::handleNotFound(AsyncWebServerRequest *request) {
  917. DEBUG_WM(F("Handle not found"));
  918. if (captivePortal(request)) { // If captive portal redirect instead of displaying the error page.
  919. return;
  920. }
  921. String message = "File Not Found\n\n";
  922. message += "URI: ";
  923. message += request->url();
  924. message += "\nMethod: ";
  925. message += ( request->method() == HTTP_GET ) ? "GET" : "POST";
  926. message += "\nArguments: ";
  927. message += request->args();
  928. message += "\n";
  929. for ( uint8_t i = 0; i < request->args(); i++ ) {
  930. message += " " + request->argName ( i ) + ": " + request->arg ( i ) + "\n";
  931. }
  932. AsyncWebServerResponse *response = request->beginResponse(404,"text/plain",message);
  933. response->addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
  934. response->addHeader("Pragma", "no-cache");
  935. response->addHeader("Expires", "-1");
  936. request->send (response );
  937. }
  938. /** Redirect to captive portal if we got a request for another domain. Return true in that case so the page handler do not try to handle the request again. */
  939. boolean AsyncWiFiManager::captivePortal(AsyncWebServerRequest *request) {
  940. if (!isIp(request->host()) ) {
  941. DEBUG_WM(F("Request redirected to captive portal"));
  942. AsyncWebServerResponse *response = request->beginResponse(302,"text/plain","");
  943. response->addHeader("Location", String("http://") + toStringIp(request->client()->localIP()));
  944. request->send ( response);
  945. return true;
  946. }
  947. return false;
  948. }
  949. //start up config portal callback
  950. void AsyncWiFiManager::setAPCallback( void (*func)(AsyncWiFiManager* myAsyncWiFiManager) ) {
  951. _apcallback = func;
  952. }
  953. //start up save config callback
  954. void AsyncWiFiManager::setSaveConfigCallback( void (*func)(void) ) {
  955. _savecallback = func;
  956. }
  957. //sets a custom element to add to head, like a new style tag
  958. void AsyncWiFiManager::setCustomHeadElement(const char* element) {
  959. _customHeadElement = element;
  960. }
  961. //sets a custom element to add to options page
  962. void AsyncWiFiManager::setCustomOptionsElement(const char* element) {
  963. _customOptionsElement = element;
  964. }
  965. //if this is true, remove duplicated Access Points - defaut true
  966. void AsyncWiFiManager::setRemoveDuplicateAPs(boolean removeDuplicates) {
  967. _removeDuplicateAPs = removeDuplicates;
  968. }
  969. template <typename Generic>
  970. void AsyncWiFiManager::DEBUG_WM(Generic text) {
  971. if (_debug) {
  972. Serial.print("*WM: ");
  973. Serial.println(text);
  974. }
  975. }
  976. int AsyncWiFiManager::getRSSIasQuality(int RSSI) {
  977. int quality = 0;
  978. if (RSSI <= -100) {
  979. quality = 0;
  980. } else if (RSSI >= -50) {
  981. quality = 100;
  982. } else {
  983. quality = 2 * (RSSI + 100);
  984. }
  985. return quality;
  986. }
  987. /** Is this an IP? */
  988. boolean AsyncWiFiManager::isIp(String str) {
  989. for (int i = 0; i < str.length(); i++) {
  990. int c = str.charAt(i);
  991. if (c != '.' && (c < '0' || c > '9')) {
  992. return false;
  993. }
  994. }
  995. return true;
  996. }
  997. /** IP to String? */
  998. String AsyncWiFiManager::toStringIp(IPAddress ip) {
  999. String res = "";
  1000. for (int i = 0; i < 3; i++) {
  1001. res += String((ip >> (8 * i)) & 0xFF) + ".";
  1002. }
  1003. res += String(((ip >> 8 * 3)) & 0xFF);
  1004. return res;
  1005. }