ESPAsyncWiFiManager.cpp 28 KB

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