Skip to content

Commit 1f0944e

Browse files
authored
Merge pull request #41 from CivicTechTO/21-access-point-dropdown
21 access point dropdown
2 parents 6319bfd + dbfa9e6 commit 1f0944e

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

noisemeter-device/access-point-html.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ font-size: 24px;
1919
padding: 5px;
2020
margin-top: 3px;
2121
}
22+
select, input{margin-bottom: 1em;min-width: 150px;}
2223
.meter {
2324
height: 5px;
2425
position: relative;
@@ -72,7 +73,25 @@ const char *HTML_FOOTER = R"html(
7273
</html>
7374
)html";
7475

75-
const char *HTML_BODY_FORM = R"html(
76+
const char *HTML_BODY_FORM_HEADER = R"html(
77+
<p>Select your home WiFi network below and enter its password to get the sensor online:<br><br></p>
78+
<form method='POST' action='/submit' enctype='multipart/form-data'>
79+
<label for='ssid'>Wifi network name:</label><br>
80+
<select name='ssid' id='ssid' required>
81+
)html";
82+
83+
const char *HTML_BODY_FORM_FOOTER = R"html(
84+
</select>
85+
<p style="font-size: 0.75em">(&#x1f512; = password required)<br>(Don't see your network? <a href="/manual">Enter it manually</a>)</p>
86+
<br><label for='psk'>Wifi network password:</label><br>
87+
<input type='password' name='psk' id='psk'/><br>
88+
<label for='email'>Your Email (also your username for logging into the tRacket portal):</label><br>
89+
<input type='email' name='email' id='email'/><br>
90+
<p><input type='submit' value='Connect'/></p>
91+
</form>
92+
)html";
93+
94+
const char *HTML_BODY_FORM_MANUAL = R"html(
7695
<p>Enter the wifi network name and password for your home network, which the sensor can connect to to get online:<br/><br/></p>
7796
<form method='POST' action='/submit' enctype='multipart/form-data'>
7897
<p>Wifi network name:</p>
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extern const char *HTML_HEADER;
22
extern const char *HTML_CONTAINER;
33
extern const char *HTML_FOOTER;
4-
extern const char *HTML_BODY_FORM;
5-
4+
extern const char *HTML_BODY_FORM_HEADER;
5+
extern const char *HTML_BODY_FORM_FOOTER;
6+
extern const char *HTML_BODY_FORM_MANUAL;

noisemeter-device/access-point.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ constexpr auto ACCESS_POINT_TIMEOUT_SEC = MIN_TO_SEC(30);
2727
const IPAddress AccessPoint::IP (8, 8, 4, 4);
2828
const IPAddress AccessPoint::Netmask (255, 255, 255, 0);
2929

30+
static int networkScanCount = 0;
31+
3032
AccessPoint::AccessPoint(SubmissionHandler func):
3133
timeout(ACCESS_POINT_TIMEOUT_SEC),
3234
server(80),
@@ -50,6 +52,8 @@ String AccessPoint::htmlFromMsg(const char *msg, const char *extra)
5052

5153
void AccessPoint::run()
5254
{
55+
networkScanCount = WiFi.scanNetworks();
56+
5357
WiFi.mode(WIFI_AP);
5458
WiFi.softAPConfig(IP, IP, Netmask);
5559
WiFi.softAP(SSID, Passkey);
@@ -157,9 +161,32 @@ bool AccessPoint::handle(WebServer& server, HTTPMethod method, String uri)
157161
response.reserve(2048);
158162
response += HTML_HEADER;
159163
response += HTML_CONTAINER;
160-
response += HTML_BODY_FORM;
164+
response += HTML_BODY_FORM_HEADER;
165+
166+
for (int i = 0; i < networkScanCount; ++i) {
167+
const auto ssid = WiFi.SSID(i);
168+
169+
response += "<option value=\"";
170+
response += ssid;
171+
response += "\">";
172+
response += ssid;
173+
if (WiFi.encryptionType(i) != WIFI_AUTH_OPEN)
174+
response += " &#x1f512;";
175+
response += "</option>";
176+
}
177+
178+
response += HTML_BODY_FORM_FOOTER;
161179
response += HTML_FOOTER;
162180

181+
timeout = DAY_TO_SEC(30);
182+
server.send_P(200, PSTR("text/html"), response.c_str());
183+
} else if (uri == "/manual") {
184+
String response;
185+
response.reserve(2048);
186+
response += HTML_HEADER;
187+
response += HTML_CONTAINER;
188+
response += HTML_BODY_FORM_MANUAL;
189+
response += HTML_FOOTER;
163190
timeout = DAY_TO_SEC(30);
164191
server.send_P(200, PSTR("text/html"), response.c_str());
165192
} else if (uri == "/connecttest.txt") {

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ build_unflags =
2626
build_flags =
2727
-std=gnu++17
2828
-DNO_GLOBAL_EEPROM
29-
-DNOISEMETER_VERSION=\"0.2.2\"
29+
-DNOISEMETER_VERSION=\"0.2.3\"
3030
-Wall -Wextra
3131

3232
# Optional build flags:

0 commit comments

Comments
 (0)