3
3
4
4
namespace thing {
5
5
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
+ }
7
14
8
15
void Portal::Start () {
9
16
server_.on (" /" , [&] () {
@@ -50,13 +57,44 @@ void Portal::Start() {
50
57
}
51
58
}
52
59
}
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
+ }
53
89
</script>
54
90
</head>
55
91
<body>
56
92
<div>Host: <input id='host'></div>
57
93
<div>Auth: <input id='auth'></div>
58
94
<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>
60
98
<div>Wifi Key: <input id='key'></div>
61
99
<div><button onclick='send_config();'>Save</button></div>
62
100
<div id='loading'>Loading....</div>
@@ -67,6 +105,7 @@ void Portal::Start() {
67
105
static const PROGMEM char type[] = " text/html" ;
68
106
69
107
server_.send_P (200 , type, page);
108
+ debug_ (" served root page." );
70
109
});
71
110
72
111
server_.on (" /config" , [&] () {
@@ -82,9 +121,11 @@ void Portal::Start() {
82
121
String buffer;
83
122
root.printTo (buffer);
84
123
server_.send (200 , " application/json" , buffer);
124
+ debug_ (" config retrieved" );
85
125
} else if (server_.method () == HTTP_POST) {
86
126
if (!server_.hasArg (" config" )) {
87
127
server_.send (500 , " text/plain" , " Missing config.\r\n " );
128
+ debug_ (" Config updated called without param." );
88
129
return ;
89
130
}
90
131
String config = server_.arg (" config" );
@@ -96,9 +137,28 @@ void Portal::Start() {
96
137
config_.wifi_key = root[" wifi_key" ].asString ();
97
138
callback_ (config_);
98
139
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);
99
152
}
153
+
154
+ String buffer;
155
+ data.printTo (buffer);
156
+ server_.send (200 , " application/json" , buffer);
157
+ debug_ (" served networks." );
100
158
});
159
+
101
160
server_.begin ();
161
+ debug_ (" Portal started." );
102
162
}
103
163
104
164
void Portal::Loop () {
0 commit comments