1616 * @author Naguissa
1717 * @see <a href="https://github.com/Naguissa/uEspConfigLib">https://github.com/Naguissa/uEspConfigLib</a>
181819- * @version 1.1 .0
19+ * @version 1.2 .0
2020 */
2121#include < Arduino.h>
2222#include " uEspConfigLib.h"
@@ -77,13 +77,14 @@ void uEspConfigLib::_copyDescription(uEspConfigLibList * slot, const char * valu
7777 * @param description Description of the configuration option
7878 * @param defaultValue Default value of the configuration option
7979 */
80- void uEspConfigLib::addOption (const char * name, const char * description, const char * defaultValue) {
80+ void uEspConfigLib::addOption (const char * name, const char * description, const char * defaultValue, const uint8_t option ) {
8181 if (list == 0 ) {
8282 list = new uEspConfigLibList;
8383 _copyName (list, name);
8484 _copyDescription (list, description);
8585 _copyDefault (list, defaultValue);
8686 _copyValue (list, defaultValue);
87+ list->option = option;
8788 list->next = 0 ;
8889 return ;
8990 }
@@ -94,6 +95,7 @@ void uEspConfigLib::addOption(const char * name, const char * description, const
9495 _copyDescription (slot, description);
9596 _copyDefault (slot, defaultValue);
9697 _copyValue (slot, defaultValue);
98+ list->option = option;
9799 return ;
98100 }
99101 prev = slot;
@@ -104,6 +106,7 @@ void uEspConfigLib::addOption(const char * name, const char * description, const
104106 _copyDescription (prev->next , description);
105107 _copyDefault (prev->next , defaultValue);
106108 _copyValue (prev->next , defaultValue);
109+ prev->next ->option = option;
107110 prev->next ->next = 0 ;
108111}
109112
@@ -170,6 +173,11 @@ bool uEspConfigLib::clear(const char *name) {
170173 * @param path Path where the form will be sent
171174 */
172175void uEspConfigLib::handleConfigRequestHtml (uEspConfigLib_WebServer * server, const char *path) {
176+ if (server->arg (" option" ) == " scan" && server->arg (" field" ).length () > 0 ) {
177+ _handleWifiScan (server, server->arg (" field" ));
178+ return ;
179+ }
180+
173181 server->setContentLength (CONTENT_LENGTH_UNKNOWN);
174182 yield ();
175183 server->send (200 , " text/html" , " " );
@@ -198,11 +206,25 @@ void uEspConfigLib::handleConfigRequestHtml(uEspConfigLib_WebServer * server, co
198206 yield ();
199207 uEspConfigLib_WebServer_sendContent (slot->name );
200208 yield ();
209+ if (slot->option == uEspConfigLib_OPTION_SCANNER) {
210+ server->sendContent (" \" id=\" " );
211+ yield ();
212+ uEspConfigLib_WebServer_sendContent (slot->name );
213+ yield ();
214+ }
201215 server->sendContent (" \" value=\" " );
202216 yield ();
203217 uEspConfigLib_WebServer_sendContent (slot->value );
204218 yield ();
205- server->sendContent (" \" ></td></tr>" );
219+ server->sendContent (" \" >" );
220+ yield ();
221+ if (slot->option == uEspConfigLib_OPTION_SCANNER) {
222+ server->sendContent (" <a href=\" javascript:window.open('?option=scan&field=" );
223+ uEspConfigLib_WebServer_sendContent (slot->name );
224+ server->sendContent (" ')\" >Scan</a>" );
225+ yield ();
226+ }
227+ server->sendContent (" </td></tr>" );
206228 yield ();
207229 }
208230 server->sendContent (" <tr><td colspan=\" 2\" ><br><center><button type=\" submit\" >Send</button></center></td></tr></table></body>" );
@@ -411,3 +433,36 @@ bool uEspConfigLib::saveConfigFile() {
411433 return true ;
412434}
413435
436+ void uEspConfigLib::_handleWifiScan (uEspConfigLib_WebServer * server, const String field) {
437+ server->setContentLength (CONTENT_LENGTH_UNKNOWN);
438+ yield ();
439+ server->send (200 , " text/html" , " " );
440+ yield ();
441+ server->sendContent (" <html><head><title>IoT device config - uConfigLib</title></head><body><p><b>WiFi networks:</b></p>" );
442+ yield ();
443+
444+ int n = WiFi.scanNetworks ();
445+ if (n == 0 ) {
446+ server->sendContent (" <p><i>No networks found</i></p>" );
447+ yield ();
448+ } else {
449+ server->sendContent (" <ul>" );
450+ for (int i = 0 ; i < n; i++) {
451+ server->sendContent (" <li><a href=\" javascript:window.opener.document.getElementById('" + field + " ').value='" + WiFi.SSID (i) + " ';window.close();\" >" + WiFi.SSID (i) + " - Channel: " + WiFi.channel (i) + " - RSSI: " + WiFi.RSSI (i) + " - Encription: " );
452+ yield ();
453+ switch (WiFi.encryptionType (i)) {
454+ case ENC_TYPE_WEP: server->sendContent (" WEP" ); break ;
455+ case ENC_TYPE_TKIP: server->sendContent (" WPA/PSK" ); break ;
456+ case ENC_TYPE_CCMP: server->sendContent (" WPA2/PSK" ); break ;
457+ case ENC_TYPE_NONE: server->sendContent (" NONE" ); break ;
458+ case ENC_TYPE_AUTO: server->sendContent (" AUTO (WPA/WPA2/PSK)" ); break ;
459+ default : server->sendContent (" Unknown" ); break ;
460+ }
461+ server->sendContent (" </a></li>" );
462+ yield ();
463+ }
464+ server->sendContent (" </ul>" );
465+ }
466+ }
467+
468+
0 commit comments