Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit43c6a3c

Browse files
committed
Added network scanning to config page.
1 parent7581ba5 commit43c6a3c

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
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
namespacething {
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_([](constchar*){}) {}
10+
11+
voidPortal::SetDebugHandler(std::function<void(constchar* message)> handler) {
12+
debug_ = handler;
13+
}
714

815
voidPortal::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
staticconst PROGMEMchar 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
}elseif (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
voidPortal::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
voidLoop();
1515
voidNotifyOnUpdate(std::function<void(const Config& config)> cb);
1616

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

2225
};
2326

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp