Skip to content

Commit

Permalink
Merge pull request #41 from CivicTechTO/21-access-point-dropdown
Browse files Browse the repository at this point in the history
21 access point dropdown
  • Loading branch information
tcsullivan authored Jan 18, 2025
2 parents 6319bfd + dbfa9e6 commit 1f0944e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
21 changes: 20 additions & 1 deletion noisemeter-device/access-point-html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ font-size: 24px;
padding: 5px;
margin-top: 3px;
}
select, input{margin-bottom: 1em;min-width: 150px;}
.meter {
height: 5px;
position: relative;
Expand Down Expand Up @@ -72,7 +73,25 @@ const char *HTML_FOOTER = R"html(
</html>
)html";

const char *HTML_BODY_FORM = R"html(
const char *HTML_BODY_FORM_HEADER = R"html(
<p>Select your home WiFi network below and enter its password to get the sensor online:<br><br></p>
<form method='POST' action='/submit' enctype='multipart/form-data'>
<label for='ssid'>Wifi network name:</label><br>
<select name='ssid' id='ssid' required>
)html";

const char *HTML_BODY_FORM_FOOTER = R"html(
</select>
<p style="font-size: 0.75em">(&#x1f512; = password required)<br>(Don't see your network? <a href="/manual">Enter it manually</a>)</p>
<br><label for='psk'>Wifi network password:</label><br>
<input type='password' name='psk' id='psk'/><br>
<label for='email'>Your Email (also your username for logging into the tRacket portal):</label><br>
<input type='email' name='email' id='email'/><br>
<p><input type='submit' value='Connect'/></p>
</form>
)html";

const char *HTML_BODY_FORM_MANUAL = R"html(
<p>Enter the wifi network name and password for your home network, which the sensor can connect to to get online:<br/><br/></p>
<form method='POST' action='/submit' enctype='multipart/form-data'>
<p>Wifi network name:</p>
Expand Down
5 changes: 3 additions & 2 deletions noisemeter-device/access-point-html.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern const char *HTML_HEADER;
extern const char *HTML_CONTAINER;
extern const char *HTML_FOOTER;
extern const char *HTML_BODY_FORM;

extern const char *HTML_BODY_FORM_HEADER;
extern const char *HTML_BODY_FORM_FOOTER;
extern const char *HTML_BODY_FORM_MANUAL;
29 changes: 28 additions & 1 deletion noisemeter-device/access-point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ constexpr auto ACCESS_POINT_TIMEOUT_SEC = MIN_TO_SEC(30);
const IPAddress AccessPoint::IP (8, 8, 4, 4);
const IPAddress AccessPoint::Netmask (255, 255, 255, 0);

static int networkScanCount = 0;

AccessPoint::AccessPoint(SubmissionHandler func):
timeout(ACCESS_POINT_TIMEOUT_SEC),
server(80),
Expand All @@ -50,6 +52,8 @@ String AccessPoint::htmlFromMsg(const char *msg, const char *extra)

void AccessPoint::run()
{
networkScanCount = WiFi.scanNetworks();

WiFi.mode(WIFI_AP);
WiFi.softAPConfig(IP, IP, Netmask);
WiFi.softAP(SSID, Passkey);
Expand Down Expand Up @@ -157,9 +161,32 @@ bool AccessPoint::handle(WebServer& server, HTTPMethod method, String uri)
response.reserve(2048);
response += HTML_HEADER;
response += HTML_CONTAINER;
response += HTML_BODY_FORM;
response += HTML_BODY_FORM_HEADER;

for (int i = 0; i < networkScanCount; ++i) {
const auto ssid = WiFi.SSID(i);

response += "<option value=\"";
response += ssid;
response += "\">";
response += ssid;
if (WiFi.encryptionType(i) != WIFI_AUTH_OPEN)
response += " &#x1f512;";
response += "</option>";
}

response += HTML_BODY_FORM_FOOTER;
response += HTML_FOOTER;

timeout = DAY_TO_SEC(30);
server.send_P(200, PSTR("text/html"), response.c_str());
} else if (uri == "/manual") {
String response;
response.reserve(2048);
response += HTML_HEADER;
response += HTML_CONTAINER;
response += HTML_BODY_FORM_MANUAL;
response += HTML_FOOTER;
timeout = DAY_TO_SEC(30);
server.send_P(200, PSTR("text/html"), response.c_str());
} else if (uri == "/connecttest.txt") {
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ build_unflags =
build_flags =
-std=gnu++17
-DNO_GLOBAL_EEPROM
-DNOISEMETER_VERSION=\"0.2.2\"
-DNOISEMETER_VERSION=\"0.2.3\"
-Wall -Wextra

# Optional build flags:
Expand Down

0 comments on commit 1f0944e

Please sign in to comment.