ESPAsyncWiFiManager.cpp 26 KB

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