ESPAsyncWiFiManager.cpp 30 KB

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