Skip to content

Commit d71dd54

Browse files
committed
v.1.2.0 - Add WiFi scanner
1 parent b08fa85 commit d71dd54

File tree

6 files changed

+91
-23
lines changed

6 files changed

+91
-23
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ config = new uEspConfigLib(configFs);
4444
Finally you need to define your desired configuration options:
4545

4646
```
47-
config->addOption("name", "Description", "Default value");
47+
config->addOption("name", "Description", "Default value"[, Options]);
4848
```
4949

50+
Options can be:
51+
- uEspConfigLib_OPTION_NONE - Regular behaviour
52+
- uEspConfigLib_OPTION_SCANNER - Adds WiFi scanner window to select desired SSID
53+
54+
5055
A typical example could be:
5156

5257
```

examples/uEspConfigLib_example/uEspConfigLib_example.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @author Naguissa
1616
* @see <a href="https://github.com/Naguissa/uEspConfigLib">https://github.com/Naguissa/uEspConfigLib</a>
1717
* @see <a href="mailto:[email protected]">[email protected]</a>
18-
* @version 1.1.0
18+
* @version 1.2.0
1919
*/
2020

2121
#include "Arduino.h"
@@ -90,7 +90,7 @@ void setup() {
9090
Serial.println("\n - configure -");
9191
// 1st step is to define your variables: Name, Description, Default value
9292
config->addOption("wifi_mode", "WiFi mode (C=Client, other=Access Point)", "");
93-
config->addOption("wifi_ssid", "SSID of your WiFi", "Unconfigured_device");
93+
config->addOption("wifi_ssid", "SSID of your WiFi", "Unconfigured_device", uEspConfigLib_OPTION_SCANNER);
9494
config->addOption("wifi_password", "Password of your WiFi", "wifi_password");
9595

9696

examples/uEspConfigLib_example_EEPROM/uEspConfigLib_example_EEPROM.ino

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,9 @@
1515
* @author Naguissa
1616
* @see <a href="https://github.com/Naguissa/uEspConfigLib">https://github.com/Naguissa/uEspConfigLib</a>
1717
* @see <a href="mailto:[email protected]">[email protected]</a>
18-
* @version 1.1.0
18+
* @version 1.2.0
1919
*/
2020

21-
22-
/**************************************************
23-
* WARNING: This example needs uEEPROMLib. *
24-
* You can find it at Library Manager *
25-
**************************************************/
26-
27-
2821
#include "Arduino.h"
2922

3023
#include "uEspConfigLibFSEEPROM.h"
@@ -71,7 +64,7 @@ void setup() {
7164
Serial.println(" - SETUP -");
7265

7366
configFs = new uEspConfigLibFSEEPROM("", true);
74-
if (configFs->status() == uEspConfigLibFS_STATUS_FATAL) {
67+
if (configFsE->status() == uEspConfigLibFS_STATUS_FATAL) {
7568
Serial.println(" * Error initializing FS EEPROM");
7669
}
7770

@@ -82,7 +75,7 @@ void setup() {
8275
Serial.println("\n - configure -");
8376
// 1st step is to define your variables: Name, Description, Default value
8477
config->addOption("wifi_mode", "WiFi mode (C=Client, other=Access Point)", "");
85-
config->addOption("wifi_ssid", "SSID of your WiFi", "Unconfigured_device");
78+
config->addOption("wifi_ssid", "SSID of your WiFi", "Unconfigured_device", uEspConfigLib_OPTION_SCANNER);
8679
config->addOption("wifi_password", "Password of your WiFi", "wifi_password");
8780

8881

library.properties

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
name=uEspConfigLib
2-
version=1.1.0
2+
version=1.2.0
33
author=Naguissa <[email protected]>
44
maintainer=Naguissa <[email protected]>
55
sentence=The deffinitive ESP32 and ESP8266 configuration Arduino library, uEspConfigLib
66
paragraph=Supports ESP32 and ESP8266 microcontrollers. Manages configurations, web requests and storage persistence.
77
category=Data Processing
88
url=https://github.com/Naguissa/uEspConfigLib
99
architectures=esp32,esp8266
10-
includes=uEspConfigLib.h
11-
depends=uEEPROMLib
10+
includes=uEspConfigLib.h,uEspConfigLibFSInterface.h,uEspConfigLibFSNone.h
11+
12+

src/uEspConfigLib.cpp

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @author Naguissa
1717
* @see <a href="https://github.com/Naguissa/uEspConfigLib">https://github.com/Naguissa/uEspConfigLib</a>
1818
* @see <a href="mailto:[email protected]">[email protected]</a>
19-
* @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
*/
172175
void 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+

src/uEspConfigLib.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @author Naguissa
1717
* @see <a href="https://github.com/Naguissa/uEspConfigLib">https://github.com/Naguissa/uEspConfigLib</a>
1818
* @see <a href="mailto:[email protected]">[email protected]</a>
19-
* @version 1.1.0
19+
* @version 1.2.0
2020
*/
2121
#pragma once
2222

@@ -38,18 +38,30 @@
3838
#endif
3939

4040

41+
/**
42+
* \brief Regular field
43+
*/
44+
#define uEspConfigLib_OPTION_NONE 0
45+
/**
46+
* \brief Adds WiFi scanner window to select desired SSID
47+
*/
48+
#define uEspConfigLib_OPTION_SCANNER 1
49+
50+
4151
struct uEspConfigLibList {
42-
uEspConfigLibList() : next(0), name(0), description(0), defaultValue(0), value(0) {};
52+
uEspConfigLibList() : next(0), name(0), description(0), defaultValue(0), value(0), option(uEspConfigLib_OPTION_NONE) {};
4353
uEspConfigLibList *next;
4454
char * name;
4555
char * description;
4656
char * defaultValue;
4757
char * value;
58+
uint8_t option;
4859
};
4960

5061
#define uEspConfigLib_free(field) if (field != 0) { free(field); field = 0; }
5162
#define uEspConfigLib_WebServer_sendContent(data) if (*data != 0) { server->sendContent(data); }
5263

64+
5365
class uEspConfigLib {
5466
public:
5567
/**
@@ -66,8 +78,9 @@ class uEspConfigLib {
6678
* @param name Name of configuration option
6779
* @param description Description of the configuration option
6880
* @param defaultValue Default value of the configuration option
81+
* @param option Optional. Special option, uEspConfigLib_OPTION_NONE by default
6982
*/
70-
void addOption(const char *, const char *, const char *);
83+
void addOption(const char *, const char *, const char *, const uint8_t = uEspConfigLib_OPTION_NONE);
7184

7285
/**
7386
* \brief Changes a configuration option current value
@@ -178,8 +191,9 @@ class uEspConfigLib {
178191
void _copyValue(uEspConfigLibList *, const String);
179192
void _copyDescription(uEspConfigLibList *, const char *);
180193
void _parseConfigLine(String);
194+
void _handleWifiScan(uEspConfigLib_WebServer *, const String);
195+
void handleWifiScanResult();
181196
uEspConfigLibList *list;
182197
uEspConfigLibFSInterface * _fs;
183-
184198
};
185199

0 commit comments

Comments
 (0)