ESPAsyncWiFiManager.cpp 28 KB

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