forked from r-downing/PersWiFiManager
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPersWiFiManager.cpp
More file actions
404 lines (362 loc) · 17.4 KB
/
PersWiFiManager.cpp
File metadata and controls
404 lines (362 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* PersWiFiManager
version 3.0.1
https://r-downing.github.io/PersWiFiManager/
*/
#include "PersWiFiManager.h"
#ifdef WIFI_HTM_PROGMEM
const char wifi_htm[] PROGMEM = R"=====(<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/><title>ESP WiFi</title><script>function g(i){return document.getElementById(i);};function p(t,l){if(confirm(t)) window.location=l;};function E(s){return document.createElement(s)};var S="setAttribute",A="appendChild",H="innerHTML",X,wl;function scan(){if(X) return;X=new XMLHttpRequest(),wl=document.getElementById('wl');wl[H]="Scanning...";X.onreadystatechange=function(){if (this.readyState==4&&this.status==200){X=0;wl[H]="";this.responseText.split("\n").forEach(function (e){let t=e.split(","), s=t.slice(2).join(',');var d=E('div'),i=E('a'),c=E('a');i[S]('class','s'); c[S]('class','q');i.onclick=function(){g('s').value=s;g('p').focus();};i[A](document.createTextNode(s));c[H]=t[0]+"%"+(parseInt(t[1])?"\uD83D\uDD12":"\u26A0");wl[A](i); wl[A](c);wl[A](document.createElement('br'));});}};X.open("GET","wifi/list",true);X.send();};</script><style>input{padding:5px;font-size:1em;width:95%;}body{text-align:center;font-family:verdana;background-color:black;color:white;}a{color:#1fa3ec;}button{border:0;border-radius:0.3em;background-color:#1fa3ec;color:#fff;line-height:2.4em;font-size:1.2em;width:100%;display:block;}.q{float:right;}.s{display:inline-block;width:14em;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;}#wl{line-height:1.5em;}</style></head><body><div style='text-align:left;display:inline-block;width:320px;padding:5px'><button onclick="scan()">↻ Scan</button><p id='wl'></p><form method='post' action='/wifi/connect'><input id='s' name='n' length=32 placeholder='SSID'><br><input id='p' name='p' length=64 type='password' placeholder='password'><br><br><button type='submit'>Connect</button></form><br><br><button onclick="p('Reboot device?','/wifi/rst')">Reboot</button><br><a href="javascript:history.back()">Back</a> |<a href="/">Home</a></div></body></html>)=====";
#endif
#ifdef WIFI_HTM_GZ_PROGMEM
#define wifi_htm_gz_len 1103
static const uint8_t wifi_htm_gz[] PROGMEM = {
0x1f, 0x8b, 0x08, 0x08, 0x3b, 0xdf, 0x37, 0x5c, 0x04, 0x00, 0x69, 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0xe4, 0x65, 0x1a, 0xef, 0xb2, 0xcc, 0xa1,
0x79, 0x37, 0x5c, 0x1d, 0xda, 0x7a, 0xfd, 0xeb, 0xc0, 0x53, 0x1e, 0x91,
0xa0, 0x96, 0x78, 0xff, 0xdf, 0x9e, 0x10, 0x24, 0x9d, 0x41, 0xa9, 0x25,
0xb6, 0x99, 0x79, 0xf3, 0xf9, 0xf3, 0xb3, 0x96, 0x9c, 0x39, 0xc5, 0x55,
0x07, 0x5c, 0x75, 0xc4, 0xdd, 0xbb, 0x43, 0xdc, 0xa6, 0x6a, 0x55, 0xb8,
0xc9, 0x0d, 0xce, 0x88, 0x77, 0xea, 0xe7, 0x97, 0xba, 0xf3, 0xd5, 0x7e,
0x0f, 0x09, 0xef, 0x61, 0xb6, 0x5e, 0xac, 0x04, 0xb2, 0x7b, 0xd2, 0xd3,
0xfc, 0x95, 0xe7, 0xb0, 0xcd, 0xec, 0x00, 0xfa, 0xbb, 0x4a, 0x95, 0xef,
0xbd, 0x85, 0x85, 0xd6, 0x8e, 0xf4, 0x6f, 0xae, 0x87, 0x5e, 0xb8, 0x4f,
0xd8, 0x60, 0x0d, 0xb0, 0x7c, 0xdd, 0xbf, 0x47, 0x7f, 0xbd, 0x13, 0x4e,
0x4a, 0x03, 0x05, 0xa3, 0x9f, 0xf9, 0x9a, 0xf7, 0x8b, 0x17, 0x97, 0xb8,
0x98, 0xda, 0x6c, 0xa2, 0x76, 0x94, 0xdb, 0xaa, 0x3f, 0xc6, 0xdf, 0x74,
0xc8, 0xa7, 0xe4, 0xc7, 0xd1, 0x7a, 0x48, 0xa7, 0xe7, 0x7a, 0x05, 0xa8,
0x45, 0x56, 0xd8, 0x77, 0x7c, 0xf6, 0x33, 0x30, 0xec, 0x5e, 0xb2, 0x57,
0xaf, 0xfc, 0x04, 0x35, 0x76, 0x39, 0x90, 0x7c, 0x07, 0x00, 0x00
};
#endif
PersWiFiManager::PersWiFiManager(AsyncWebServer &s, AsyncDNSServer &d, const fs::FS& fs):_fs(fs)
{
_aserver = &s;
_adnsServer = &d;
_apPass = "";
} //PersWiFiManager
bool PersWiFiManager::attemptConnection(const String &ssid, const String &pass)
{
//attempt to connect to wifi
WiFi.mode(WIFI_STA);
if (ssid.length())
{
if(Serial) Serial.printf("Connecting to SSID: %s\n", ssid.c_str());
if (pass.length())
WiFi.begin(ssid.c_str(), pass.c_str());
else
WiFi.begin(ssid.c_str());
}
else
{
if(Serial) Serial.printf("Connecting to previously stored WiFi credentials.\n");
WiFi.begin();
}
_connectStartTime = millis();
_tkWiFiH.attach_ms(10, std::bind(&PersWiFiManager::handleWiFi, this));
return (WiFi.status() == WL_CONNECTED);
} //attemptConnection
void PersWiFiManager::handleWiFi()
{
if (!_connectStartTime)
return;
if (WiFi.status() == WL_CONNECTED)
{
_connectStartTime = 0;
if (_connectHandler)
_connectHandler();
return;
if (Serial) {
Serial.print("Router IP: ");
Serial.println(WiFi.localIP());
}
}
//if failed or not connected and time is up
if ((WiFi.status() == WL_CONNECT_FAILED) || ((WiFi.status() != WL_CONNECTED) && ((millis() - _connectStartTime) > (1000 * WIFI_CONNECT_TIMEOUT))))
{
startApMode();
_connectStartTime = 0; //reset connect start time
}
} //handleWiFi
void PersWiFiManager::startApMode()
{
//start AP mode
IPAddress apIP(192, 168, 1, 1);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
_apPass.length() ? WiFi.softAP(getApSsid().c_str(), _apPass.c_str()) : WiFi.softAP(getApSsid().c_str());
if(Serial) {
Serial.print("Started AP mode, IP: ");
Serial.println(WiFi.softAPIP());
Serial.println("--------------");
}
if (_apHandler)
_apHandler();
} //startApMode
void PersWiFiManager::setConnectNonBlock(bool b)
{
if(Serial) Serial.println("\n>>>>>>>>>>Does not do anything, non-blocking mode is default<<<<<<<<<<<<");
_connectNonBlock = b;
} //setConnectNonBlock
void PersWiFiManager::setupWiFiHandlers()
{
if (Serial)
{
Serial.println("\n\n---------------------------");
Serial.println("Starting SPIFFs...");
}
if (_fs.begin())
{
Dir dir = _fs.openDir("/");
while (dir.next())
{
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
if (Serial)
Serial.printf("FS File: %s, size: %dB\n", fileName.c_str(), fileSize);
}
FSInfo fs_info;
_fs.info(fs_info);
if (Serial)
{
Serial.printf("FS Usage: %d/%d bytes\n\n", fs_info.usedBytes, fs_info.totalBytes);
Serial.println("SPIFFs started");
}
}
IPAddress apIP(192, 168, 1, 1);
_adnsServer->setErrorReplyCode(AsyncDNSReplyCode::NoError);
_adnsServer->start((byte)53, "*", apIP); //used for captive portal in AP mode
_aserver->on("/wifi/list", HTTP_GET, [](AsyncWebServerRequest *request) {
//scan for wifi networks
String s = "Scan again after 3-5s";
s.reserve(2050);
int n = WiFi.scanComplete();
if (n == -2)
{
WiFi.scanNetworks(true);
}
else if (n)
{
//build array of indices
int ix[n];
for (int i = 0; i < n; i++)
ix[i] = i;
//sort by signal strength
for (int i = 0; i < n; i++)
for (int j = 1; j < n - i; j++)
if (WiFi.RSSI(ix[j]) > WiFi.RSSI(ix[j - 1]))
std::swap(ix[j], ix[j - 1]);
//remove duplicates
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (WiFi.SSID(ix[i]).equals(WiFi.SSID(ix[j])) && WiFi.encryptionType(ix[i]) == WiFi.encryptionType(ix[j]))
ix[j] = -1;
//build plain text string of wifi info
//format [signal%]:[encrypted 0 or 1]:SSID
s="";
for (int i = 0; i < n && s.length() < 2000; i++)
{ //check s.length to limit memory usage
if (ix[i] != -1)
{
s += String(i ? "\n" : "") + ((constrain(WiFi.RSSI(ix[i]), -100, -50) + 100) * 2) + "," + ((WiFi.encryptionType(ix[i]) == ENC_TYPE_NONE) ? 0 : 1) + "," + WiFi.SSID(ix[i]);
}
}
WiFi.scanDelete();
if (WiFi.scanComplete() == -2)
{
WiFi.scanNetworks(true);
}
}
if (Serial)
{
Serial.print("Sending >>>>>>>>>>>>>\n");
Serial.println(s);
Serial.println("<<<<<<<<<<<<<<<<<<<<<");
}
//send string to client
request->send(200, "text/plain", s);
s = String();
}); //_aserver->on /wifi/list
_aserver->on("/wifi/connect", HTTP_POST, [&](AsyncWebServerRequest *request) {
request->send(200, "text/html", "<META http-equiv='refresh' content='15;URL=/'> Connecting...");
if (request->hasArg("n") and request->hasArg("p"))
attemptConnection(request->arg("n"), request->arg("p"));
}); //_aserver->on /wifi/connect
_aserver->on("/wifi/ap", HTTP_GET, [&](AsyncWebServerRequest *request) {
request->send(200, "text/html", "<META http-equiv='refresh' content='15;URL=/'> Access point: " + getApSsid());
startApMode();
}); //_aserver->on /wifi/ap
_aserver->on("/wifi/restart", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/html", "<META http-equiv='refresh' content='15;URL=/'> Rebooting...");
//delay(100);
//ESP.restart();
// Adding Safer Restart method
ESP.wdtDisable();
ESP.reset();
}); //_aserver->on /wifi/restart
_aserver->on("/wifi/reset", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/html", "<META http-equiv='refresh' content='15;URL=/'> Erasing WiFi credentials and Rebooting...");
WiFi.disconnect(); //erases wifi credentials
ESP.wdtDisable();
ESP.reset();
}); //_aserver->on /wifi/reset
_aserver->addHandler(new SPIFFSEditor(_fsUser, _fsPass));
_aserver->onNotFound([&](AsyncWebServerRequest *request) {
String filename = request->url();
String ContentType = "text/plain";
if (filename.endsWith(".htm"))
ContentType = "text/html";
else if (filename.endsWith(".html"))
ContentType = "text/html";
else if (filename.endsWith(".css"))
ContentType = "text/css";
else if (filename.endsWith(".js"))
ContentType = "application/javascript";
else if (filename.endsWith(".png"))
ContentType = "image/png";
else if (filename.endsWith(".gif"))
ContentType = "image/gif";
else if (filename.endsWith(".jpg"))
ContentType = "image/jpeg";
else if (filename.endsWith(".ico"))
ContentType = "image/x-icon";
else if (filename.endsWith(".xml"))
ContentType = "text/xml";
else if (filename.endsWith(".pdf"))
ContentType = "application/x-pdf";
else if (filename.endsWith(".zip"))
ContentType = "application/x-zip";
else if (filename.endsWith(".gz"))
ContentType = "application/x-gzip";
else if (filename.endsWith("ico.gz"))
ContentType = "image/x-icon";
if (_fs.exists(filename + ".gz") || _fs.exists(filename))
{
if (_fs.exists(filename + ".gz"))
filename += ".gz";
AsyncWebServerResponse *response = request->beginResponse(SPIFFS, filename, ContentType);
if (filename.endsWith(".gz"))
response->addHeader("Content-Encoding", "gzip");
request->send(response);
return;
}
request->redirect("/wifi"); // send all DNS requests to wifi
});
#ifdef WIFI_HTM_PROGMEM
_aserver->on("/wifi", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", wifi_htm);
});
#endif
#ifdef WIFI_HTM_GZ_PROGMEM
_aserver->on("/wifi", HTTP_GET, [](AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", wifi_htm_gz, wifi_htm_gz_len);
response->addHeader("Content-Encoding", "gzip");
request->send(response);
});
#endif
} //setupWiFiHandlers
void PersWiFiManager::setFSCredentials(const String &http_user, const String &http_pass)
{
if (http_user.length())
_fsUser = http_user;
if (http_pass.length() >= 8)
_fsPass = http_pass;
} //setApCredentials
bool PersWiFiManager::begin(const String &ssid, const String &pass)
{
setupWiFiHandlers();
return attemptConnection(ssid, pass); //switched order of these two for return
} //begin
String PersWiFiManager::getApSsid()
{
return _apSsid.length() ? _apSsid : "ESP8266";
} //getApSsid
void PersWiFiManager::setApCredentials(const String &apSsid, const String &apPass)
{
if (apSsid.length())
_apSsid = apSsid;
if (apPass.length() >= 8)
_apPass = apPass;
} //setApCredentials
void PersWiFiManager::onConnect(WiFiChangeHandlerFunction fn)
{
_connectHandler = fn;
}
void PersWiFiManager::onAp(WiFiChangeHandlerFunction fn)
{
_apHandler = fn;
}