ESPAsyncWiFiManager.cpp 28 KB

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