-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWemosSetup.cpp
556 lines (486 loc) · 19.2 KB
/
WemosSetup.cpp
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include "Arduino.h"
#include "WemosSetup.h"
ESP8266WebServer WemosSetup::server=ESP8266WebServer(80);
char ap_ssid[] = "configure";
char ap_pass[] = "configurenow";
bool WemosSetup::runAccessPoint=false;
bool WemosSetup::showFailureOnWeb=false;
bool WemosSetup::showSuccessOnWeb=false;
bool WemosSetup::webServerRunning=false;
bool WemosSetup::accessPointStarted=false;
bool WemosSetup::stationStarted=false;
bool WemosSetup::tryingToConnect=false;
char WemosSetup::html[]="";
char WemosSetup::body[]="";
char WemosSetup::onload[]="";
char WemosSetup::afterConnectionUrl[]="";
const char WemosSetup::htmlstart[] = "<!doctype html>\r\n<html><head><meta charset='UTF-8'><style>body {font-family:sans-serif}</style><title>Connect</title></head><body onload=\"";
const char WemosSetup::htmlmid[] = "\">";
const char WemosSetup::htmlend[] = "</body></html>";
String WemosSetup::networks;
char WemosSetup::WiFiSSID[WFS_MAXSSIDLENGTH];
char WemosSetup::WiFiPSK[WFS_MAXPASSKEYLENGTH];
WiFiMode WemosSetup::wifimode;
byte WemosSetup::ledStatus;
const byte WemosSetup::ON = LOW;
const byte WemosSetup::OFF = HIGH;
unsigned long WemosSetup::timeToChangeToSTA = 0; //when is it time time to change to STA (station) in ms. Zero means never
unsigned long WemosSetup::timeToChangeToSTAAfterConnection = 0;
int WemosSetup::_led_pin;
WemosSetup::WemosSetup() {
wifimode = WIFI_STA;
}
void WemosSetup::begin(WiFiMode startmode, unsigned long activeTime, int led_pin) {
/*
default values defined in header file:
WiFiMode startmode = WIFI_STA
activeTime = 0
led_pin = -1
startmode can be WIFI_STA or WIFI_AP_STA
if startmode is WIFI_STA and no connection can be made, it changes to AP mode
activeTime is how long in ms it stays in AP mode before switching to STA.
activeTime = 0 means:
- stay in access point forever if startmode is WIFI_AP_STA
- stay forever in station mode if startmode is WIFI_STA
led_pin is pin number of status led. -1 means don't use led.
*/
_led_pin=led_pin;
if (_led_pin != -1) {
pinMode(_led_pin, OUTPUT);
}
ledStatus = ON;
ledWrite(ledStatus);
if (startmode == WIFI_AP_STA) {
startAP_STA(activeTime);
} else if (startmode == WIFI_STA) {
startSTA(activeTime);
} else {
startSTA(activeTime); //start in station mode if no valid mode is given
}
}
bool WemosSetup::connected() {
return (WiFi.status() == WL_CONNECTED);
}
void WemosSetup::switchToSTA() {
if (!stationStarted) {
//Should original ssid and psk be read here?
startSTA(0);
} else {
wifimode = WIFI_STA;
WiFi.mode(wifimode);
}
timeToChangeToSTA = 0;
}
void WemosSetup::switchToAP_STA() {
if (!accessPointStarted) {
startAP_STA(0);
} else {
wifimode = WIFI_AP_STA;
WiFi.mode(wifimode);
}
timeToChangeToSTA = 0;
}
void WemosSetup::startSTA(unsigned long activeTime) {
//activeTime is how long it stays in AP mode in ms if it swithces to AP after unsuccessful connection attempt. 0 means stay forever in station mode
//start in station mode and try to connect to last known ssid
wifimode = WIFI_STA;
WiFi.mode(wifimode);
if (WiFi.status() != WL_CONNECTED) {
if (WiFi.SSID().length() == 0) {
WiFi.begin(WiFiSSID, WiFiPSK);
} else {
WiFi.begin(); //reconnectes to previous SSID and PASSKEY if it has been in AP-mode
}
}
delay(7000);
if (WiFi.status() == WL_CONNECTED) {
wfs_debugprint("Successfully connected to ");
wfs_debugprintln(WiFi.SSID());
ledStatus = OFF;
ledWrite(ledStatus);
} else {
wfs_debugprintln("!!! Could not connect to ");
wfs_debugprintln(WiFi.SSID());
ledStatus = ON;
ledWrite(ledStatus);
if (activeTime!=0) {
startAP_STA(activeTime); //start accesspoint for activeTime minutes if it could not connect
}
}
stationStarted = true;
}
void WemosSetup::startAP_STA(unsigned long activeTime) {
//activeTime is how long it stays in AP_STA mode in ms before switching to STA. 0 means stay in AP forever
//remember old ssid and psk for STATION as they might be cleared by WiFi.disconnect() or WiFi.scanNetworks
WiFi.SSID().toCharArray(WiFiSSID, WFS_MAXSSIDLENGTH);
WiFi.psk().toCharArray(WiFiPSK, WFS_MAXPASSKEYLENGTH);
delay(500); //these delays seems to prevent occasional crashes when scanning an already running access point
wifimode = WIFI_AP_STA; //used to be WIFI_AP but sometimes computer lost connection when changing form WIFI_AP to WIFI_AP_STA
WiFi.mode(wifimode);
delay(200); //these delays seems to prevent occasional crashes when changing to access point. Maybe not needed after updated ESP8266WiFi library
//WiFi.disconnect(); //Should it connect to previous network? Probably the best
networks="<option value=''>enter ssid above or select here</option>";
wfs_debugprintln("start scan");
int n=WiFi.scanNetworks();
wfs_debugprintln("scanning complete");
if (n>0) {
for (int i = 0; i < n; ++i) {
if (networks.length()+17+WiFi.SSID(i).length()<WFS_MAXNETWORKCHLENGTH) {
//if more networks than what fits in networksch char array are found, skip them
networks=networks+"<option>"+WiFi.SSID(i)+"</option>";
}
}
}
if (activeTime==0) {
timeToChangeToSTA = 0;
} else {
timeToChangeToSTA = millis()+activeTime*1000;
}
timeToChangeToSTAAfterConnection = activeTime;
if (!accessPointStarted) {
IPAddress stationip(192, 168, 4, 1);//this is the default but added manually just to be sure it doesn't change in the future
IPAddress NMask(255, 255, 255, 0);
WiFi.softAPConfig(stationip, stationip, NMask);
WiFi.softAP(ap_ssid,ap_pass);
IPAddress apip = WiFi.softAPIP();
wfs_debugprint("Starting access point mode with IP address ");
wfs_debugprintln(apip);
wfs_debugprint("Connect to ");
wfs_debugprint("configure");
wfs_debugprint(" network and browse to ");
wfs_debugprintln(apip);
accessPointStarted = true;
}
if (!webServerRunning) {
startWebServer();
}
}
void WemosSetup::handleFrm() {
//this page is shown as an iframe in root
wfs_debugprintln("handleFrm");
//we have the following possibilites:
//1) this page is called after successful connection
//2) this page is called after failed connection
//3) this page is called during connection
//4) this page is called before connection attemp-redirect to form
sprintf(onload,"");
if (showSuccessOnWeb) {
//sprintf(body, "Successfully connected to %s <script>parent.clearInterval(parent.i1)</script><script>window.parent.location.href='/test';</script>", WiFiSSID);
if (strlen(afterConnectionUrl)>0) {
sprintf(onload,"setTimeout(function(){window.parent.location.href='%s'},2000)",afterConnectionUrl);
}
sprintf(body, "Successfully connected to %s <script>parent.clearInterval(parent.i1)</script>", WiFiSSID);
} else if (showFailureOnWeb) {
sprintf(body, "Could not connect to %s. Maybe wrong ssid or password. <a target='_parent' href='/'>Try again</a>", WiFiSSID);
} else if (tryingToConnect) {
//show a "progress bar" of dots
char n[5];
if (server.hasArg("n")) {
server.arg("n").toCharArray(n,5);
} else {
sprintf(n,"3");
}
sprintf(body,"<script>document.write(new Array(1+%s).join('.'))</script>",n);
} else {
sprintf(body, "<script>window.parent.location.href='/';</script>");
}
sendHtml(body,onload);
}
void WemosSetup::sendHtml(const char *body, const char *onload) {
sprintf(html, "%s%s%s%s%s", htmlstart, onload, htmlmid, body, htmlend);
server.send(200, "text/html", html);
}
void WemosSetup::afterConnection(const char *url) {
//webpage to redirect to after a new wifi network has been chosen
sprintf(afterConnectionUrl,url);
}
void WemosSetup::handleRoot() {
wfs_debugprintln("handleroot");
char networkch[WFS_MAXNETWORKCHLENGTH];
sprintf(onload,"");
bool initiateConnection=false;
if (!server.hasArg("ssid") && WiFi.status() != WL_CONNECTED) {
networks.toCharArray(networkch,WFS_MAXNETWORKCHLENGTH);
//note that html element arguments may be double qouted, single quoted or unquoted. Unquoted is usually not recommended but used here to save memory
sprintf(body, "<h2>Wifi connection</h2><p>Not connected</p><form method=post action='/'><input type=text name=ssid value='%s' size=32 id=s1 onchange='document.getElementById(\"s2\").value=this.value'> SSID (network name)<br><select name=s2 id=s2 onchange='document.getElementById(\"s1\").value=this.value'>%s</select><br><input type=password name=pass size=32> PASSWORD<br><input type=submit value=connect></form>", WiFiSSID,networkch);
} else if (!server.hasArg("ssid")) {
networks.toCharArray(networkch,WFS_MAXNETWORKCHLENGTH);
sprintf(body,"<h2>Wifi connection</h2><p>Connected to %s</p><form method=post action='/'><input type=text name=ssid value='%s' size=32 id=s1 onchange='document.getElementById(\"s2\").value=this.value'> SSID (network name)<br><select name=s2 id=s2 onchange='document.getElementById(\"s1\").value=this.value'>%s</select><br><input type=password name=pass size=32> PASSWORD<br><input type=submit value=connect></form>",WiFiSSID,WiFiSSID,networkch);
} else { //server has arg ssid
showFailureOnWeb = false;
showSuccessOnWeb = false;
initiateConnection = true;
server.arg("ssid").toCharArray(WiFiSSID, WFS_MAXSSIDLENGTH);
wfs_debugprintln("has arg ssid");
if (server.hasArg("pass")) {
server.arg("pass").toCharArray(WiFiPSK, WFS_MAXPASSKEYLENGTH);
} else {
sprintf(WiFiPSK,"");
}
sprintf(onload,"var n=0;i1=setInterval(function() {n++;document.getElementById('frm').src='frm?r='+Math.random()+'&n='+n},2000)");
sprintf(body,"<h2>Wifi connection</h2><script>var i1</script><p>Connecting to %s</p><iframe frameborder=0 id=frm width=480 height=240 src=''></iframe>",WiFiSSID);
}
//Don't change the content on any of these variables without checking their size limits!
sendHtml(body,onload);
if (initiateConnection) {
if (connectWiFi()) {
wfs_debugprintln("Connection successful");
showSuccessOnWeb = true;
ledStatus = OFF;
ledWrite(ledStatus);
} else {
wfs_debugprintln("Connection failed");
ledStatus = ON;
ledWrite(ledStatus);
startAP_STA(0);
showFailureOnWeb = true;
}
}
}
bool WemosSetup::connectWiFi() {
delay(400);
WiFi.disconnect(); //it sometimes crashes if you don't disconnect before new connection
delay(100);
tryingToConnect = true;
bool timeout = false;
//wifimode = WIFI_AP_STA; should already be in AP_STA, but used to be AP
//WiFi.mode(wifimode);
//The connection sometimes fails if the previous ssid is the same as the new ssid.
//A temporary connection attempt to a non existing network seems to fix this.
WiFi.begin("hopefullynonexistingssid", "");
delay(500);
wfs_debugprint(F("Connecting to "));
wfs_debugprintln(WiFiSSID);
WiFi.begin(WiFiSSID, WiFiPSK);
// Use the WiFi.status() function to check if the ESP8266
// is connected to a WiFi network. Timeout after one minute.
unsigned long timelimit = 60000;
unsigned long starttimer = millis();
int delayTime=200;
int period = 600; //blink period and check if connected period. should be multiple of delayTime
bool connected=false;
unsigned long lastCheck=0;
while (!connected && !timeout) {
unsigned long loopStart = millis();
if (lastCheck + period <= loopStart) {
lastCheck = loopStart;
wfs_debugprint(".");
// Blink the LED
ledStatus = !ledStatus;
ledWrite(ledStatus); // Write LED high/low
}
if (wifimode != WIFI_STA && webServerRunning) {
server.handleClient();
}
delay(delayTime); //a delay is needed when connecting and doing other stuff, otherwise it will crash
if ((loopStart - starttimer) > timelimit) {
wfs_debugprintln("");
wfs_debugprint("timed out");
timeout = true;
}
connected = (WiFi.status() == WL_CONNECTED);
}
if (!timeout) {
if (timeToChangeToSTAAfterConnection==0) {
timeToChangeToSTA = 0;
} else {
timeToChangeToSTA = millis()+timeToChangeToSTAAfterConnection*1000;
}
}
wfs_debugprintln("");
tryingToConnect=false;
return !timeout;
}
void WemosSetup::startWebServer() {
wfs_debugprintln("Trying to start webserver");
if (!webServerRunning) {
wfs_debugprintln("Starting webserver");
server.on("/", handleRoot);
server.on("/frm", handleFrm);
server.begin();
webServerRunning = true;
}
}
void WemosSetup::stopWebServer() {
//WARNING - maybe crash if stopping already stopped server
wfs_debugprintln("Trying to stop webserver");
if (webServerRunning) {
wfs_debugprintln("Stopping webserver");
server.close(); //stop or close? what is the difference?
webServerRunning = false;
}
}
/*
void WemosSetup::handleClient() {
server.handleClient();
}
*/
void WemosSetup::inLoop() {
unsigned long loopStart=millis();
//check if it is time to change from configure mode (access point) to normal mode (station)
yield();
if (timeToChangeToSTA != 0 && timeToChangeToSTA < loopStart) {
//change to station mode
if (wifimode != WIFI_STA) {
wifimode = WIFI_STA;
WiFi.mode(wifimode);
if (WiFi.status() != WL_CONNECTED) {
wfs_debugprintln("trying to reconnect to prev ssid");
if (WiFi.SSID().length() == 0) {
WiFi.begin(WiFiSSID, WiFiPSK);
} else {
WiFi.begin(); //reconnectes to previous SSID and PASSKEY if it has been in AP-mode
}
}
wfs_debugprintln("changing to STA");
}
timeToChangeToSTA = 0; // Zero means don't change to STA
}
if (wifimode != WIFI_STA && webServerRunning) {
//only handle web requests in access point mode and if webserver is running
server.handleClient();
delay(2);
}
if ((wifimode != WIFI_AP) && (lastCheck + checkRate <= loopStart)) {
//turn led on or off to indicate connection (off = connected)
lastCheck=loopStart;
if (WiFi.status() == WL_CONNECTED) {
ledStatus = OFF;
ledWrite(ledStatus);
} else {
ledStatus = ON;
ledWrite(ledStatus);
}
shortBlink();
}
if ((wifimode != WIFI_STA) && WiFi.status() != WL_CONNECTED && (lastApBlink + apBlinkRate <= loopStart)) {
//blink led to indicate access point mode
lastApBlink = loopStart;
ledStatus = !ledStatus;
ledWrite(ledStatus);
}
}
void WemosSetup::toggleAccessPoint() {
if (wifimode == WIFI_STA) {
switchToAP_STA();
} else {
switchToSTA();
}
}
void WemosSetup::ledWrite(int status) {
if (_led_pin != -1) {
digitalWrite(_led_pin, status);
}
}
void WemosSetup::shortBlink() {
ledStatus = !ledStatus;
ledWrite(ledStatus);
if (ledStatus == OFF) {
delay(30); //longer pulse needed when going ON-OFF-ON
} else {
delay(5); //these delays are not needed if the led is not used, but delays now and then seem to do good to the ESP. However, for time critical applications, delays may be omitted if _led_pin==-1
}
ledStatus = !ledStatus;
ledWrite(ledStatus);
}
void WemosSetup::printInfo() {
//this function is only needed for debugging/testing and can be deleted
wfs_debugprintln(F("CURRENT STATUS"));
wfs_debugprint(F("Running for: "));
unsigned long now=millis();
unsigned long h=now/1000/3600;
byte m=(now-h*1000*3600)/1000/60;
byte s=(now-h*1000*3600-m*1000*60)/1000;
wfs_debugprint(h);
wfs_debugprint(F(":"));
wfs_debugprint(m);
wfs_debugprint(F(":"));
wfs_debugprintln(s);
wfs_debugprint(F("Change to STA in: "));
if (timeToChangeToSTA==0) {
wfs_debugprintln("don't change");
} else {
wfs_debugprint(timeToChangeToSTA-now);
wfs_debugprintln(" ms");
}
wfs_debugprint("WIFI MODE: ");
if (wifimode==WIFI_STA) {
wfs_debugprintln(F("WIFI_STA"));
} else if (wifimode==WIFI_AP) {
wfs_debugprintln(F("WIFI_AP"));
} else if (wifimode==WIFI_AP_STA) {
wfs_debugprintln(F("WIFI_AP_STA"));
} else {
wfs_debugprintln(wifimode);
}
wfs_debugprint(F("runAccessPoint: "));
wfs_debugprintln(runAccessPoint);
wfs_debugprint(F("showFailureOnWeb: "));
wfs_debugprintln(showFailureOnWeb);
wfs_debugprint(F("webServerRunning: "));
wfs_debugprintln(webServerRunning);
wfs_debugprint(F("accessPointStarted: "));
wfs_debugprintln(accessPointStarted);
wfs_debugprint(F("stationStarted: "));
wfs_debugprintln(stationStarted);
wfs_debugprint(F("showSuccessOnWeb: "));
wfs_debugprintln(showSuccessOnWeb);
wfs_debugprint(F("WiFi Status: "));
switch(WiFi.status()){
case WL_NO_SHIELD:
wfs_debugprintln(F("WL_NO_SHIELD"));
break;
case WL_IDLE_STATUS:
wfs_debugprintln(F("WL_IDLE_STATUS"));
break;
case WL_NO_SSID_AVAIL:
wfs_debugprintln(F("WL_NO_SSID_AVAIL"));
break;
case WL_SCAN_COMPLETED:
wfs_debugprintln(F("WL_SCAN_COMPLETED"));
break;
case WL_CONNECTED:
wfs_debugprintln(F("WL_CONNECTED"));
break;
case WL_CONNECT_FAILED:
wfs_debugprintln(F("WL_CONNECT_FAILED"));
break;
case WL_CONNECTION_LOST:
wfs_debugprintln(F("WL_CONNECTION_LOST"));
break;
case WL_DISCONNECTED:
wfs_debugprintln(F("WL_DISCONNECTED"));
break;
default:
wfs_debugprintln(WiFi.status());
}
long rssi = WiFi.RSSI();
wfs_debugprint(F("RSSI: "));
wfs_debugprint(rssi);
wfs_debugprintln(F(" dBm"));
wfs_debugprint(F("SSID: "));
wfs_debugprintln(WiFi.SSID());
wfs_debugprint(F("WiFiSSID: "));
wfs_debugprintln(WiFiSSID);
IPAddress ip = WiFi.localIP();
wfs_debugprint(F("Station IP Address: "));
wfs_debugprintln(ip);
ip = WiFi.softAPIP();
wfs_debugprint(F("AP IP Address: "));
wfs_debugprintln(ip);
byte mac[6];
WiFi.macAddress(mac);
wfs_debugprint(F("MAC address: "));
wfs_debugprint(mac[0], HEX);
wfs_debugprint(":");
wfs_debugprint(mac[1], HEX);
wfs_debugprint(":");
wfs_debugprint(mac[2], HEX);
wfs_debugprint(":");
wfs_debugprint(mac[3], HEX);
wfs_debugprint(":");
wfs_debugprint(mac[4], HEX);
wfs_debugprint(":");
wfs_debugprintln(mac[5], HEX);
wfs_debugprintln("");
}