Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit de2ef09

Browse files
authored
Merge pull request #223 from ed7coyne/firethings
Added network scanning to config portal.
2 parents c353aaa + 2dd655f commit de2ef09

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

src/thing/Portal.cpp

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
namespace thing {
55

6-
Portal::Portal(const Config& config) : config_(config), callback_([](const Config&){}) {}
6+
Portal::Portal(const Config& config)
7+
: config_(config),
8+
callback_([](const Config&){}),
9+
debug_([](const char*){}) {}
10+
11+
void Portal::SetDebugHandler(std::function<void(const char* message)> handler) {
12+
debug_ = handler;
13+
}
714

815
void Portal::Start() {
916
server_.on("/", [&] () {
@@ -50,13 +57,44 @@ void Portal::Start() {
5057
}
5158
}
5259
}
60+
function scan() {
61+
document.getElementById('scanning').style.display='inline';
62+
var xhr = new XMLHttpRequest();
63+
xhr.open("GET", "/wifi/scan", true);
64+
xhr.onreadystatechange = function () {
65+
if (xhr.readyState==4 && xhr.status==200) {
66+
load_networks(JSON.parse(xhr.responseText));
67+
document.getElementById('scanning').style.display='none';
68+
}
69+
}
70+
xhr.send();
71+
}
72+
function load_networks(networks) {
73+
select = document.getElementById('networks');
74+
select.innerHtml = '';
75+
networks.sort(function(a, b) {
76+
return b.rssi - a.rssi;
77+
});
78+
networks.forEach(function(network) {
79+
option = document.createElement('option');
80+
option.value = network.ssid;
81+
option.text = network.ssid + ' (' + network.rssi + ')';
82+
select.add(option);
83+
});
84+
select.style.display='inline';
85+
}
86+
function set_network(select) {
87+
document.getElementById('ssid').value = select[select.selectedIndex].value;
88+
}
5389
</script>
5490
</head>
5591
<body>
5692
<div>Host: <input id='host'></div>
5793
<div>Auth: <input id='auth'></div>
5894
<div>Path: <input id='path'></div>
59-
<div>Wifi SSID: <input id='ssid'></div>
95+
<div>Wifi SSID: <input id='ssid'><button onclick='scan();'>scan</button></div>
96+
<div id='scanning' style='display:none'>Scanning...</div>
97+
<div><select size=10 id='networks' style='display:none' onchange='set_network(this);'></select></div>
6098
<div>Wifi Key: <input id='key'></div>
6199
<div><button onclick='send_config();'>Save</button></div>
62100
<div id='loading'>Loading....</div>
@@ -67,6 +105,7 @@ void Portal::Start() {
67105
static const PROGMEM char type[] = "text/html";
68106

69107
server_.send_P(200, type, page);
108+
debug_("served root page.");
70109
});
71110

72111
server_.on("/config", [&] () {
@@ -82,9 +121,11 @@ void Portal::Start() {
82121
String buffer;
83122
root.printTo(buffer);
84123
server_.send(200, "application/json", buffer);
124+
debug_("config retrieved");
85125
} else if (server_.method() == HTTP_POST) {
86126
if (!server_.hasArg("config")) {
87127
server_.send(500, "text/plain", "Missing config.\r\n");
128+
debug_("Config updated called without param.");
88129
return;
89130
}
90131
String config = server_.arg("config");
@@ -96,9 +137,28 @@ void Portal::Start() {
96137
config_.wifi_key = root["wifi_key"].asString();
97138
callback_(config_);
98139
server_.send(200, "text/plain", "");
140+
debug_("config updated.");
141+
}
142+
});
143+
144+
server_.on("/wifi/scan", [&] () {
145+
int net_count = WiFi.scanNetworks();
146+
DynamicJsonBuffer json_buffer;
147+
JsonArray& data = json_buffer.createArray();
148+
for (int i=0; i < net_count; i++) {
149+
JsonObject& entry = data.createNestedObject();
150+
entry["ssid"] = WiFi.SSID(i);
151+
entry["rssi"] = WiFi.RSSI(i);
99152
}
153+
154+
String buffer;
155+
data.printTo(buffer);
156+
server_.send(200, "application/json", buffer);
157+
debug_("served networks.");
100158
});
159+
101160
server_.begin();
161+
debug_("Portal started.");
102162
}
103163

104164
void Portal::Loop() {

src/thing/Portal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ class Portal {
1414
void Loop();
1515
void NotifyOnUpdate(std::function<void(const Config& config)> cb);
1616

17+
void SetDebugHandler(std::function<void(const char* message)> handler);
18+
1719
private:
1820
Config config_;
1921
ESP8266WebServer server_;
2022
std::function<void(const Config& config)> callback_;
23+
std::function<void(const char* message)> debug_;
2124

2225
};
2326

src/thing/Transcriber.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
21
#include "thing/Transcriber.h"
2+
#include <ESP8266WiFi.h>
33

44
namespace thing {
55
namespace {
@@ -37,6 +37,10 @@ void Transcriber::SetValue(const std::string& path, const std::string& value) {
3737
}
3838

3939
void Transcriber::Loop() {
40+
if (WiFi.status() != WL_CONNECTED) {
41+
return;
42+
}
43+
4044
// Read incoming data
4145
if (stream_->available()) {
4246
std::string event_str;

0 commit comments

Comments
 (0)