-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWifiManager.cpp
More file actions
1612 lines (924 loc) · 31 KB
/
WifiManager.cpp
File metadata and controls
1612 lines (924 loc) · 31 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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// WifiManager.cpp
// Glowdeck
//
// Created by Justin Kaufman on 8/17/17.
// Copyright © 2017 Justin Kaufman. All rights reserved.
//
#include "WifiManager.h"
extern GlowdeckManager glowdeckManager;
extern BootloaderManager bootloaderManager;
extern SerialManager serialManager;
extern BluetoothManager bluetoothManager;
extern LEDManager ledManager;
extern DisplayManager displayManager;
WifiManager::WifiManager(HardwareSerial1 *ser) {
this->wi = wifi;
recvPrefixLength = 9;
wi.begin(9600);
}
void WifiManager::setup() {
wifiBuffer = "";
clear();
while (wi.available()) { wi.read(); }
commandMode();
wi.print("AT+E\r"); getResponse(0);
while (wi.available()) { wi.read(); }
// GIVE USR GLOWDECK MODEL AND UDID VALUES FOR WEB CLIENT
wi.print("AT+GDMODEL=Beta\r");
getResponse(0);
String udid = "00000"; // glowdeckManager.udid;
String longName = "Glowdeck"; // glowdeckManager.longName;
wi.print("AT+GDUDID=GD" + udid + "\r");
getResponse(0);
wi.print("AT+GDLGN=" + longName + "\r");
getResponse(0);
wi.print(F("AT+GDSSID="));
if ((accessPointName != "0") && (accessPointName != "MyRouter")) {
wi.print(accessPointName);
}
else {
wi.print("MyRouter");
}
wi.print("\r");
getResponse(0);
wi.print(F("AT+TCPDISB=off\r"));
getResponse(0);
// SET HTTP COMMAND CONSTANTS HERE
wi.print(F("AT+TCPDIS=off\r"));
getResponse(0);
wi.print(F("AT+HTTPURL=streams.io,443\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
wi.print(F("AT+HTTPTP=POST\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
wi.print(F("AT+HTTPCN=keep-alive\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
wi.print(F("AT+HTTPUA=lwip1.3.2\r")); //TO DO: CHANGE UA TO UDID
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
wi.print(F("AT+TCPDIS=on\r"));
getResponse(0);
// ssid();
// ip();
}
void WifiManager::loop() {
if (wi.available()) {
unsigned long st = millis();
while (wi.available()) {
char inChar = wifi.read();
wifiBuffer += inChar;
if ((wifiBuffer.contains("\r")) || (wifiBuffer.contains("\n"))) {
handleMessage(wifiBuffer);
wifiBuffer = "";
break;
}
if ((millis() - st) > 5000) {
wifiBuffer = "";
break;
}
}
}
}
void WifiManager::clear() {
wi.flush();
if (wi.available()) {
while (wi.available()) {
wi.read();
}
}
}
void WifiManager::handleMessage(String cmd) {
cmd = cmd.replace("\r\n\r\n", "");
cmd = cmd.trim();
// #if defined DEBUG
main.println("[WI] " + cmd);
// #endif
if (!(cmd.length() > 1)) return;
if ((cmd == "+ok") || (cmd == "+ERR")) return;
if (cmd.startsWith(F("+ok="))) {
int tmp_len = cmd.length();
cmd = cmd.substring(4, tmp_len);
}
if (cmd.indexOf(F("Ch,SSID,BSSID,")) != -1) { // AT+WSCAN RESPONSE DATA (SSIDs AND AUTH/ENCRYPTION MODES)
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 4; j++) {
wiscan[i][j] = F("");
}
}
ssid_count = -1;
return;
}
else if ((cmd.indexOf(F("WPA2PSK/AES")) != -1) || (cmd.indexOf(F("OPEN/NONE")) != -1) || (cmd.indexOf(F("WPAPSK/TKIPAES")) != -1) || (cmd.indexOf(F("WPAPSKWPA2PSK/TKIPAES")) != -1)) { // SSID IN-RANGE LINE
logSsid(cmd);
return;
}
else if (cmd.indexOf(F("RF Off")) != -1) { // USR MODULE OFF
if (wifiOn == 1) {
wifiOn = 0;
displayManager.printWifi(0);
}
wifiOn = 0;
return;
}
else if (cmd.indexOf(F("Disconnected")) != -1) { // NOT CONNECTED
if (wifiOn == 1) {
wifiOn = 0; displayManager.printWifi(0);
}
wifiOn = 0;
main.println(F("STA LINK DISCONNECTED"));
return;
}
else if (cmd.indexOf(accessPointName + F("(")) != -1) { // STA LINK SUCCESSFULLY ESTABLISHED
if (wifiOn == 0) {
wifiOn = 1;
displayManager.printWifi(1);
wi.print(F("AT+WANN\r"));
handleMessage(getResponse(0));
}
wifiOn = 1;
main.println(F("STA LINK ESTABLISHED"));
return;
}
//else if (cmd.indexOf(F("DHCP,")) != -1) {
// int indA = cmd.indexOf(F(",")); int indB = cmd.indexOf(F(","), indA+1);
// if (indB != -1)
// {
// sta_ip = cmd.substring(indA+1, indB);
// }
// if (sta_ip == F("0.0.0.0"))
// {
// sta_ip = F("10.10.100.254");
// }
// main.print(F("STA_IP: ")); main.println(sta_ip); main.println();
// return;
//}
else if (cmd.endsWith(F("^"))) {
if (cmd.startsWith(F("LGN:"))) {
String tmp_lnm = cmd.substring((cmd.indexOf(F(":"))+1), cmd.indexOf(F("^")));
//if (glowdeckManager.longName != tmp_lnm) {
//glowdeckManager.longName = tmp_lnm;
tmp_lnm = F("");
bluetoothManager.clear();
if ((bluetoothManager.bleConnected == 1) || (bluetoothManager.sppConnected == 1)) {
bluetoothManager.send(F("SEND LGN:OK^\n\r"));
delay(100);
}
/*
bluetoothManager.send(F("SET NAME="));
bluetoothManager.send(glowdeckManager.longName);
bluetoothManager.send(F("\r"));
delay(150);
bluetoothManager.send(F("WRITE\r"));
delay(150);
// bluetoothManager.send(F("RESET\r"));
*/
//}
//if (PRV == 0)
//{
// PRV = 1; EEPROM.write(1, '1');
//}
//wi.print(F("AT+GDLGN=") + glowdeckManager.longName + F("\r")); getResponse(0);
return;
}
else if (cmd.startsWith(F("SSID:"))) { // USER SSID
accessPointName = cmd.substring((cmd.indexOf(F(":"))+1), cmd.indexOf(F("^")));
//wi.print(F("AT+GDSglowdeckManager.udid=") + accessPointName + F("\r")); getResponse(0);
return;
}
else if (cmd.startsWith(F("PASS:"))) { //USER SSID
password = cmd.substring((cmd.indexOf(F(":"))+1), cmd.indexOf(F("^")));
status(); //sync_counter = 1;
//while (wi.available()) { wi.read(); }
//wi.print(F("AT+Z\r")); delay(1250); wifiSetup(); delay(600);
//wi.print(F("AT+GDSSID=") + accessPointName + F("\r")); getResponse(0);
//wi.print(F("AT+GDKEY=") + password + F("\r")); getResponse(0);
//wi.print(F("AT+GDLGN=") + glowdeckManager.longName + F("\r")); getResponse(0);
//wi.print(F("AT+Z\r")); delay(500);
//TEST WIFI CONNECTION
//wi.print(F("AT+WMODE=STA\r")); getResponse(0);
//wi.print(F("AT+Z\r")); delay(500);
//while (wi.available()) { wi.read(); }
//wi.print(F("AT+E\r")); getResponse(0);
//boolean wifi_success = false;
//wi.print(F("AT+WSLK\r")); String resp = getResponse(0);
//if (resp.indexOf(F("Disconnected")) != -1)
//{
// int wifi_timeout = 6000; int wifi_start = millis();
// while (1)
// {
// wi.print(F("AT+WSLK\r")); String resp = getResponse(0);
// if ( (resp.indexOf(F("(")) != -1) && (resp.indexOf(F(":")) != -1) )
// {
// wifi_success = true;
// }
// if (millis() - wifi_start > wifi_timeout)
// {
// break;
// }
// delay(200);
// }
//}
//else if ( (resp.indexOf(F("(")) != -1) && (resp.indexOf(F(":")) != -1) )
//{
// wifi_success = true;
//}
//
//if (wifi_success == true)
//{
// wifiOn = 1;
// displayManager.printWifi(3);
//}
//else
//{
// wifiOn = 1;
// displayManager.printWifi(0);
// wi.print(F("AT+WMODE=AP\r")); getResponse(0);
// wi.print(F("AT+Z\r")); delay(100);
// while (wi.available()) { wi.read(); }
// //wi.print(F("AT+E\r")); getResponse(0);
//}
return;
}
else if (cmd.startsWith(F("UNM:"))) { // STREAMS USERNAME
// UNM = cmd.substring((cmd.indexOf(F(":"))+1), cmd.indexOf(F("^")));
return;
}
/*
else if (cmd.startsWith(F("PSW:"))) { //STREAMS PASSWORD
PSW = cmd.substring((cmd.indexOf(F(":"))+1), cmd.indexOf(F("^")));
//SPECIAL CODE FOR MANUALLY ADDING UID
if (PSW == F("!!!")) {
if ((UNM.length() >= 1) && (UNM.length() <= 5)) {
UID = UNM;
PRV = 1;
EEPROM.write(1, '1');
if (UNM.length() == 1) {
UNM = F("0000") + UNM;
}
else if (UNM.length() == 2) {
UNM = F("000") + UNM;
}
else if (UNM.length() == 3) {
UNM = F("00") + UNM;
}
else if (UNM.length() == 4) {
UNM = F("0") + UNM;
}
char UID_CHR[10];
UID.toCharArray(UID_CHR, 6);
eeprom_write_string(7, UID_CHR);
print_info(F("Streams Enabled!"));
}
}
else {
busy_icon(1);
get_stream(F("a"));
if ((UID != F("")) && (UID != F("0")) && (UID != F("00000"))) {
get_stream(F("p"));
print_info(F("Streams Enabled!"));
get_stream(F("w"));
}
busy_icon(0);
}
return;
}
*/
else if (cmd.startsWith(F("DL:"))) { // DISPLAY BRIGHTNESS SLIDER
int user_val = (cmd.substring((cmd.indexOf(F(":"))+1), cmd.indexOf(F("^")))).toInt();
if (user_val == 11) { // USER ACTUALLY PRESSED DFU BUTTON
bootloaderManager.enterBootloaderMode();
return;
}
else if ((user_val >= 0) && (user_val <= 10)) {
displayManager.brightness = user_val;
}
else {
displayManager.brightness = 10;
}
/// analogWrite(LCD_BL, scaleBrightness(displayManager.brightness));
if (bluetoothManager.sppConnected == 1) {
bluetoothManager.send("DBR:" + String(displayManager.brightness) + "^");
if (displayManager.autoBrightness == 1) {
bluetoothManager.send("DBA:0^");
}
}
else if (bluetoothManager.bleConnected == 1) {
bluetoothManager.send("DBR:" + String(displayManager.brightness) + "^");
if (displayManager.autoBrightness == 1) {
bluetoothManager.send("DBA:0^");
}
}
displayManager.autoBrightness = 0;
return;
}
/*
else if (cmd.startsWith(F("PB:"))) { // NIGHT MODE SWITCH
int nt_mode = (cmd.substring((cmd.indexOf(F(":"))+1), cmd.indexOf(F("^")))).toInt();
if (nt_mode == 1) {
if (SND == 1) {
digitalWrite(AMP, LOW);
SND = 0;
leds_off();
}
}
if (NGT == 0) {
NGT = 1;
if (bluetoothManager.bleConnected == 1) {
bluetoothManager.send(F("SEND NGT:1^\n\r"));
}
return;
}
else if (NGT == 1) {
NGT = 0;
if (bluetoothManager.bleConnected == 1) {
bluetoothManager.send(F("SEND NGT:0^\n\r"));
}
return;
}
return;
}
else if (cmd.startsWith(F("CLR:"))) { // COLOR PICKER COMMAND
int ind_a = cmd.indexOf(F(":"));
int ind_b = cmd.indexOf(F(","), ind_a+1);
int ind_c = cmd.indexOf(F(","), ind_b+1);
int ind_d = cmd.indexOf(F("^"));
if (ind_c != -1) {
COL[0] = (cmd.substring(ind_a+1, ind_b)).toInt();
COL[1] = (cmd.substring(ind_b+1, ind_c)).toInt();
COL[2] = (cmd.substring(ind_c+1, ind_d)).toInt();
FastLED.showColor(CRGB(COL[0], COL[1], COL[2]));
}
return;
}
*/
}
}
String WifiManager::send(String raw) {
raw = raw.trim();
if (!raw.contains("AT+")) {
raw = "AT+" + raw + "\r";
}
else {
raw = raw + "\r";
}
wi.print(raw);
wi.flush();
return "SENT";
}
String WifiManager::getResponse(int contentLength) {
// 0 = cmd, 1 = header, 2 = body
String buffer1 = F("");
char inByte;
int rCount = 0;
int nCount = 0;
int curLength = 0;
bool end = false;
wi.flush();
unsigned long st = millis();
while (!end) {
if (wi.available()) {
inByte = wi.read();
curLength++;
if (contentLength == 0) {
if ((inByte == '\n') && (rCount == 2) && (nCount == 1)) {
end = true;
int strLength = buffer1.length() - 3;
buffer1 = buffer1.substring(0, strLength);
}
else if (inByte == '\r') {
rCount++;
}
else if (inByte == '\n') {
nCount++;
}
else {
rCount = 0;
nCount = 0;
}
}
else if (curLength >= contentLength) {
end = true;
}
buffer1 += inByte;
}
else {
if ((millis() - st) > (5500)) {
break;
}
}
}
if (contentLength != 0) {
if (wi.available()) {
unsigned long st = millis();
bool end = false;
while (!end) {
while (wi.available()) {
wi.read();
}
delay(50);
if ((!wi.available()) || ((millis() - st) > 1000)) {
end = true;
}
}
}
}
return buffer1;
}
String WifiManager::sendMessage(String msg, uint8_t timeout) {
// main.println("[sendMessage] " + msg);
wi.print(msg);
wi.flush();
return "SENT";
}
void WifiManager::enterCommandMode() {
bool success = false;
int retries = 0;
int bothbaud = 0;
do {
if (retries > 2) {
if (bothbaud == 0) {
wi.end();
wi.begin(9600);
retries = 0;
bothbaud = 1;
}
else {
return;
}
}
success = startATSequence();
retries += 1;
} while (!success);
wi.print(F("a"));
getResponse(0);
wi.print("AT+E\r");
getResponse(0);
}
boolean WifiManager::startATSequence() {
delay(50);
while (wi.available()) { wi.read(); }
wi.write("+++");
unsigned long timeout = millis() + (4 * 1000);
while (!wi.available()) {
delay(1);
if (millis() > timeout) {
return false;
}
}
char resp = wi.read();
if (resp == 'a') {
return true;
}
return false;
}
void WifiManager::config() {
//wi.print(F("AT+E\r"));
//getResponse(0);
//wi.print(F("AT+WSLK\r"));
//getResponse(0);
//if (rspn.length() <= 2)
//{
// baud_change = 1;
// wi.end(); wi.begin(115200); delay(500);
// while (wi.available()) { wi.read(); }
// wi.print(F("AT+E\r")); getResponse(0);
//}
//CREATE TEMPORARY NAME STRING
String tmp_name = glowdeckManager.longName; // F("Glowdeck") + glowdeckManager.udid;
//SET MODULE LANGUAGE TO ENGLISH
wi.print(F("AT+PLANG=EN\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
//SET MODULE ID TO "Glowdeck + glowdeckManager.udid" (Ex: Glowdeck15FC8)
//wi.print(F("AT+WRMID=")); wi.print(tmp_name); wi.print(F("\r"));
//getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
//SET MODULE AP TO "Glowdeck + glowdeckManager.udid" (Ex: Glowdeck15FC8),
//AND SET MODULE AP TO 11BGN, CH1
wi.print("AT+WAP=11BGN," + glowdeckManager.longName + ",AUTO\r");
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
//SET MODULE AP USERNAME AND PASSWORD TO EMPTY/EMPTY
// empty not allowed, make u/p "glowdeck/glowdeck" for now!
wi.print(F("AT+WEBU=none\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
//SET MODULE TO TCP CLIENT MODE
//SET MODULE TO CONNECT TO OUR SERVER (glowdeck.com:80)
wi.print(F("AT+NETP=TCP,CLIENT,443,streams.io\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
//SET MODULE TCP TIMEOUT TO 500MS
wi.print(F("AT+TCPTO=600\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
//SET MODULE TO DUAL AP+STA MODES
wi.print(F("AT+WMODE=APSTA\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
wi.print(F("AT+WSSSID=MyRouter\r")); // + tmp_name + F("\r"));
getResponse(0);
wi.print(F("AT+TCPDISB=off\r"));
getResponse(0);
wi.print(F("AT+HTTPURL=glowdeck.com,80\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
wi.print(F("AT+HTTPTP=POST\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
wi.print(F("AT+HTTPCN=keep-alive\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
wi.print(F("AT+HTTPUA=lwip1.3.2\r")); //TO DO: CHANGE UA TO UDID
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
//SET MODULE UART TO 9600 BAUD
wi.print(F("AT+UART=9600,8,1,NONE,NFC\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
//SET MODULE TO AUTO-SWITCH TO AP MODE ON BAD/NO STA CONNECTION
wi.print(F("AT+MDCH=auto\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
//MAKE NEW SETTINGS MODULE FACTORY DEFAULT SETTINGS
wi.print(F("AT+CFGTF\r"));
getResponse(0); //CLEAR RESPONSE (Expecting: +ok)
//RESET WIFI MODULE
wi.print(F("AT+Z\r")); //delay(200);
getResponse(0);
delay(100); //WAIT 100MS?
//CLOSE UART AT 115200, OPEN NEW UART AT 9600
//wi.end(); wi.begin(9600);
//delay(450); //WAIT 1150MS (IS THIS DELAY NECESSARY?)
//REMOVE ANY LEFTOVER MODULE DATA FROM UART TO AVOID ERRORS
//while (wi.available()) { wi.read(); }
//RESET WIFI MODULE
//wi.print(F("AT+E\r"));
//getResponse(0);
wifiSetup();
}
int WifiManager::wifiSetup() {
int result = 0; // 0 = DO NOT CONFIG, 1 = CONFIG, -1 = ERROR ENTERING CMD MODE
bool usr_success = false; //E NTER CMD MODE RESULT FLAG (=TRUE IF SUCCESS)
int retries = 0; // COUNTER FOR # OF FAILED ATTEMPTS TO INITIALIZE USR MODULE
int bothbaud = 0; // =0 IF WE DID NOT TRY 115200 YET, =1 IF WEVE TRIED 115200
char usr_done = '1';
/*
if (EEPROM.read(41) == usr_done) {
result = 1;
return result;
}
*/
wi.begin(115200);
delay(5);
// displayManager.printStream(F("starting wifi"), F(""));
int startClock = millis();
do {
if (retries > 3) {
if (bothbaud == 0) {
main.println(F("USR Baud: 9600!"));
wi.end();
delay(5);
wi.begin(9600);
delay(5);
retries = 0;
bothbaud = 1;
result = 1;
}
else {
main.println(F("USR CMD Mode Fail!"));
wi.end();
result = -1;
return result;
}
}
if (millis() - startClock > 6500) {
wi.end();
result = -1;
return result;
}
usr_success = commandMode();
retries += 1;
} while (!usr_success);
wi.print(F("a"));
getResponse(0);
wi.print(F("AT+E\r"));
getResponse(0);
delay(75);
while (wi.available()) { wi.read(); }