ESPAsyncWiFiManager.cpp 31 KB

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