-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathESP8266.cpp
939 lines (697 loc) · 18.9 KB
/
ESP8266.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
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
#include "ESP8266.h"
bool ESP8266::begin()
{
return initialize();
}
/****************************************/
/****** Basic commands ******/
/****************************************/
ESP8266CommandStatus ESP8266::test()
{
clear();
_serial->println(F("AT"));
return readStatus(20);
}
bool ESP8266::restart()
{
clear();
_serial->println(F("AT+RST"));
if (readStatus(20) != ESP8266_COMMAND_OK)
return false;
return initialize();
}
ESP8266CommandStatus ESP8266::getVersion(char* buffer, int length)
{
clear();
_serial->println(F("AT+GMR"));
if (!readUntil(buffer, length, F("\r"), 20))
return ESP8266_COMMAND_TIMEOUT;
return readStatus(20);
}
ESP8266CommandStatus ESP8266::deepSleep(unsigned long time)
{
clear();
_serial->print(F("AT+GSLP="));
_serial->println(time);
return readStatus(20);
}
void ESP8266::setBaudrate(unsigned long baudrate)
{
clear();
_serial->print(F("AT+IPR="));
_serial->println(baudrate);
}
/****************************************/
/****** WiFi commands ******/
/****************************************/
ESP8266CommandStatus ESP8266::setMode(ESP8266WifiMode mode)
{
clear();
_serial->print(F("AT+CWMODE="));
_serial->println(mode);
return readStatus(200);
}
ESP8266CommandStatus ESP8266::getMode(ESP8266WifiMode* mode)
{
int c;
clear();
_serial->println(F("AT+CWMODE?"));
if (!find(F("+CWMODE:"), 20))
return ESP8266_COMMAND_TIMEOUT;
c = timedRead(20);
if (c == -1)
return ESP8266_COMMAND_TIMEOUT;
c -= '0';
if (c < 1 || c > 3)
return ESP8266_COMMAND_INVALID;
*mode = (ESP8266WifiMode)c;
return readStatus(20);
}
ESP8266CommandStatus ESP8266::joinAP(char* ssid, char* password)
{
clear();
_serial->print(F("AT+CWJAP=\""));
_serial->print(ssid);
_serial->print(F("\",\""));
_serial->print(password);
_serial->println(F("\""));
return readStatus(16000);
}
ESP8266CommandStatus ESP8266::getAP(char* ssid)
{
clear();
_serial->println(F("AT+CWJAP?"));
if (!find(F("+CWJAP:\""), 20))
return ESP8266_COMMAND_TIMEOUT;
if (!readUntil(ssid, 32, F("\""), 20))
return ESP8266_COMMAND_TIMEOUT;
return readStatus(20);
}
ESP8266CommandStatus ESP8266::quitAP()
{
clear();
_serial->println(F("AT+CWQAP"));
return readStatus(20);
}
ESP8266CommandStatus ESP8266::setAPConfiguration(char* ssid, char* password, uint8_t channel, ESP8266Encryption encryption)
{
clear();
_serial->print(F("AT+CWSAP=\""));
_serial->print(ssid);
_serial->print(F("\",\""));
_serial->print(password);
_serial->print(F("\","));
_serial->print(channel);
_serial->print(F(","));
_serial->print(encryption);
_serial->println();
return readStatus(1000);
}
ESP8266CommandStatus ESP8266::getAPConfiguration(char* ssid, char* password, uint8_t& channel, ESP8266Encryption& encryption)
{
clear();
_serial->println(F("AT+CWSAP?"));
if (!find(F("+CWSAP:\""), 2000))
return ESP8266_COMMAND_TIMEOUT;
if (!readUntil(ssid, 32, F("\""), 2000))
return ESP8266_COMMAND_TIMEOUT;
if (!find(F(",\""), 2000))
return ESP8266_COMMAND_TIMEOUT;
if (!readUntil(password, 63, F("\""), 2000))
return ESP8266_COMMAND_TIMEOUT;
if (!find(F(","), 2000))
return ESP8266_COMMAND_TIMEOUT;
channel = (uint8_t)parseInt(2000);
if (!find(F(","), 2000))
return ESP8266_COMMAND_TIMEOUT;
encryption = (ESP8266Encryption)parseInt(2000);
return readStatus(2000);
}
ESP8266CommandStatus ESP8266::getConnectedStations(ESP8266Station* stations, unsigned int& count, const unsigned int max)
{
int c;
clear();
_serial->println(F("AT+CWLIF"));
count = 0;
while (count < max) {
if (count > 0) {
if (!find(F("\r\n"), 20))
return ESP8266_COMMAND_TIMEOUT;
}
c = timedPeek(20);
if (c < '0' || c > '9')
return readStatus(20);
stations[count].ip = parseIPAddress(20);
if (!find(F(","), 20))
return ESP8266_COMMAND_TIMEOUT;
parseMACAddress(stations[count].mac, 20);
count++;
}
}
ESP8266CommandStatus ESP8266::setDHCP(ESP8266WifiMode mode, bool enable)
{
clear();
_serial->print(F("AT+CWDHCP="));
switch (mode) {
case ESP8266_WIFI_STATION:
_serial->print(1);
break;
case ESP8266_WIFI_ACCESSPOINT:
_serial->print(0);
break;
case ESP8266_WIFI_BOTH:
_serial->print(2);
break;
default:
break;
}
_serial->print(F(","));
_serial->println((int)enable);
return readStatus(20);
}
ESP8266CommandStatus ESP8266::setMAC(ESP8266WifiMode mode, byte mac[6])
{
clear();
_serial->print(F("AT+CIP"));
switch (mode) {
case ESP8266_WIFI_STATION:
_serial->print(F("STA"));
break;
case ESP8266_WIFI_ACCESSPOINT:
_serial->print(F("AP"));
default:
break;
}
_serial->print(F("MAC=\""));
for (uint8_t i = 0; i < 6; i++) {
_serial->print(mac[i], HEX);
if (i < 5)
_serial->print(F(":"));
}
_serial->println(F("\""));
return readStatus(20);
}
ESP8266CommandStatus ESP8266::getMAC(ESP8266WifiMode mode, byte mac[6])
{
clear();
switch (mode) {
case ESP8266_WIFI_STATION:
_serial->println(F("AT+CIPSTAMAC?"));
if (!find(F("+CIPSTAMAC:\""), 20))
return ESP8266_COMMAND_TIMEOUT;
break;
case ESP8266_WIFI_ACCESSPOINT:
_serial->println(F("AT+CIPAPMAC?"));
if (!find(F("+CIPAPMAC:\""), 20))
return ESP8266_COMMAND_TIMEOUT;
break;
default:
break;
}
parseMACAddress(mac, 20);
return readStatus(20);
}
ESP8266CommandStatus ESP8266::setIP(ESP8266WifiMode mode, IPAddress& ip)
{
clear();
_serial->print(F("AT+CIP"));
switch (mode) {
case ESP8266_WIFI_STATION:
_serial->print(F("STA"));
break;
case ESP8266_WIFI_ACCESSPOINT:
_serial->print(F("AP"));
default:
break;
}
_serial->print(F("=\""));
_serial->print(ip);
_serial->println(F("\""));
return readStatus(20);
}
ESP8266CommandStatus ESP8266::getIP(ESP8266WifiMode mode, IPAddress& ip)
{
clear();
switch (mode) {
case ESP8266_WIFI_STATION:
_serial->println(F("AT+CIPSTA?"));
if (!find(F("+CIPSTA:\""), 20))
return ESP8266_COMMAND_TIMEOUT;
break;
case ESP8266_WIFI_ACCESSPOINT:
_serial->println(F("AT+CIPAP?"));
if (!find(F("+CIPAP:\""), 20))
return ESP8266_COMMAND_TIMEOUT;
break;
default:
break;
}
ip = parseIPAddress(20);
return readStatus(20);
}
/****************************************/
/****** TCP/IP commands ******/
/****************************************/
ESP8266CommandStatus ESP8266::getConnectionStatus(ESP8266ConnectionStatus& status, ESP8266Connection* connection, unsigned int& count)
{
int c;
clear();
_serial->println(F("AT+CIPSTATUS"));
if (!find(F("STATUS:"), 20))
return ESP8266_COMMAND_TIMEOUT;
status = (ESP8266ConnectionStatus)parseInt(20);
count = 0;
while (true) {
if (!find(F("\r\n"), 20))
return ESP8266_COMMAND_TIMEOUT;
c = timedPeek(20);
if (c == -1)
return ESP8266_COMMAND_TIMEOUT;
if (c != '+')
return readStatus(20);
if (!find(F("+CIPSTATUS:"), 20))
return ESP8266_COMMAND_TIMEOUT;
//TODO: check in case of single connection mode
connection[count].id = parseInt(20);
if (!find(F(",\""), 20))
return ESP8266_COMMAND_TIMEOUT;
connection[count].protocol = parseProtocol(20);
if (!find(F("\","), 20))
return ESP8266_COMMAND_TIMEOUT;
connection[count].ip = parseIPAddress(20);
if (!find(F(","), 20))
return ESP8266_COMMAND_TIMEOUT;
connection[count].port = parseInt(20);
if (!find(F(","), 20))
return ESP8266_COMMAND_TIMEOUT;
connection[count].role = (ESP8266Role)parseInt(20);
count++;
}
}
ESP8266CommandStatus ESP8266::connect(ESP8266Protocol protocol, IPAddress ip, unsigned int port)
{
return connect(ESP8266_SINGLE_CLIENT, protocol, ip, port);
}
ESP8266CommandStatus ESP8266::connect(ESP8266Protocol protocol, const char* host, unsigned int port)
{
return connect(ESP8266_SINGLE_CLIENT, protocol, host, port);
}
ESP8266CommandStatus ESP8266::connect(unsigned int id, ESP8266Protocol protocol, IPAddress ip, unsigned int port)
{
pre_connect(id, protocol);
_serial->print(ip);
return post_connect(port);
}
ESP8266CommandStatus ESP8266::connect(unsigned int id, ESP8266Protocol protocol, const char* host, unsigned int port)
{
pre_connect(id, protocol);
_serial->print(host);
return post_connect(port);
}
ESP8266CommandStatus ESP8266::close(unsigned int id)
{
clear();
_serial->print(F("AT+CIPCLOSE"));
_serial->print(F("="));
_serial->println(id);
return readStatus(500);
}
ESP8266CommandStatus ESP8266::setMultipleConnections(bool enable)
{
clear();
_serial->print(F("AT+CIPMUX="));
_serial->println((int)enable);
return readStatus(20);
}
ESP8266CommandStatus ESP8266::getMultipleConnections(bool& enable)
{
int c;
clear();
_serial->println(F("AT+CIPMUX?"));
if (!find(F("+CIPMUX:"), 20))
return ESP8266_COMMAND_TIMEOUT;
c = timedRead(20);
if (c == -1)
return ESP8266_COMMAND_TIMEOUT;
c -= '0';
enable = (bool)c;
return readStatus(20);
}
ESP8266CommandStatus ESP8266::createServer()
{
return configureServer(1, 0);
}
ESP8266CommandStatus ESP8266::createServer(unsigned int port)
{
return configureServer(1, port);
}
ESP8266CommandStatus ESP8266::deleteServer()
{
return configureServer(0, 0);
}
ESP8266CommandStatus ESP8266::setServerTimeout(unsigned int timeout)
{
clear();
_serial->print(F("AT+CIPSTO="));
_serial->println(timeout);
return readStatus(20);
}
ESP8266CommandStatus ESP8266::getServerTimeout(unsigned int& timeout)
{
clear();
_serial->println(F("AT+CIPSTO?"));
if (!find(F("+CIPSTO:"), 20))
return ESP8266_COMMAND_TIMEOUT;
timeout = parseInt(20);
return readStatus(20);
}
int ESP8266::available()
{
int tmp;
int c;
// check if already receiving
if (_available > 0)
return _available;
// incoming sequence is +IPD,id,length:data or +IPD,length:data
// non-blocking search of '+'
do {
c = _serial->read();
if (c == -1)
return _available = 0;
} while (c != '+');
// find the rest slower
if (!find(F("IPD,"), 20))
return _available = 0;
// read first int, id or length
tmp = parseInt(20);
// read next char, ',' or ':'
c = timedRead(20);
// check for timeouts and store _available and _id
if (c == -1)
_available = -1;
else if (c == ',') {
_id = tmp;
_available = parseInt(20);
// skip ':'
if (timedRead(20) == -1)
_available = -1;
} else if (c == ':') {
_id = ESP8266_SINGLE_CLIENT;
_available = tmp;
} else
_available = -2;
return _available;
}
unsigned int ESP8266::getId()
{
return _id;
}
int ESP8266::read()
{
if (available() > 0) {
_available--;
while (!_serial->available()) {}
return _serial->read();
}
return -1;
}
int ESP8266::read(char* buffer, size_t size)
{
return read((uint8_t*)buffer, size);
}
int ESP8266::read(uint8_t* buffer, size_t size)
{
if (available() > 0) {
int readable = min(size, _available);
size_t count = 0;
while (count < readable) {
int c = timedRead(_timeout);
if (c == -1)
break;
*buffer++ = (uint8_t)c;
count++;
}
_available -= count;
return count;
}
return -1;
}
int ESP8266::peek()
{
if (available() > 0) {
while (!_serial->available()) {}
return _serial->peek();
}
return -1;
}
void ESP8266::flush()
{
_serial->flush();
}
size_t ESP8266::write(uint8_t b)
{
if (send(_id, &b, 1) != ESP8266_COMMAND_OK)
return 0;
return 1;
}
/****************************************/
/****** Private ******/
/****************************************/
void ESP8266::clear()
{
while (_serial->read() != -1) {}
}
bool ESP8266::initialize()
{
unsigned long startMillis = millis();
while (millis() - startMillis < 3000) {
if (test() == ESP8266_COMMAND_OK && setEcho(false) == ESP8266_COMMAND_OK && setUnvarnishedMode(false) == ESP8266_COMMAND_OK)
return true;
delay(100);
}
return false;
}
ESP8266CommandStatus ESP8266::setEcho(bool enable)
{
clear();
_serial->print(F("ATE"));
_serial->println((int)enable);
return readStatus(20);
}
ESP8266CommandStatus ESP8266::setUnvarnishedMode(bool enable)
{
clear();
_serial->print(F("AT+CIPMODE="));
_serial->println((int)enable);
return readStatus(20);
}
ESP8266CommandStatus ESP8266::configureServer(int mode, unsigned int port)
{
clear();
_serial->print(F("AT+CIPSERVER="));
_serial->print(mode);
if (port == 0)
_serial->println();
else
_serial->print(F(","));
_serial->println(port);
return readStatus(20);
}
int ESP8266::timedRead(unsigned int timeout)
{
unsigned long startMillis = millis();
int c;
do {
c = _serial->read();
// return the byte if valid
if (c >= 0) {
#ifdef ESP8266_DEBUG
if (millis() - startMillis > 20) {
Serial.print(F("==> Read: "));
Serial.print(millis() - startMillis);
Serial.println(F("ms"));
}
#endif
return c;
}
} while(millis() - startMillis < timeout);
return -1;
}
void ESP8266::pre_connect(unsigned int id, ESP8266Protocol protocol)
{
int c;
clear();
_serial->print(F("AT+CIPSTART="));
if (id != ESP8266_SINGLE_CLIENT) {
_serial->print(id);
_serial->print(F(","));
}
_serial->print(F("\""));
switch (protocol) {
case ESP8266_PROTOCOL_TCP:
_serial->print(F("TCP"));
break;
case ESP8266_PROTOCOL_UDP:
_serial->print(F("UDP"));
break;
}
_serial->print(F("\",\""));
}
ESP8266CommandStatus ESP8266::post_connect(unsigned int port)
{
_serial->print(F("\","));
_serial->println(port);
return readStatus(10000);
}
int ESP8266::timedPeek(unsigned int timeout)
{
unsigned long startMillis = millis();
int c;
do {
c = _serial->peek();
// return the byte if valid
if (c >= 0) {
#ifdef ESP8266_DEBUG
if (millis() - startMillis > 20) {
Serial.print(F("==> Peek: "));
Serial.print(millis() - startMillis);
Serial.println(F("ms"));
}
#endif
return c;
}
} while(millis() - startMillis < timeout);
// timeout
return -1;
}
int ESP8266::parseInt(unsigned int timeout)
{
_serial->setTimeout(timeout);
return _serial->parseInt();
}
byte ESP8266::parseHex(unsigned int timeout)
{
int c;
byte b = 0;
for (uint8_t i = 0; i < 2; i++) {
c = timedRead(timeout);
if (c == -1)
return 0;
if (c >= '0' && c <= '9')
b += c - '0';
if (c >= 'a' && c <= 'z')
b += 10 + c - 'a';
if (c >= 'A' && c <= 'Z')
b += 10 + c - 'A';
if (i == 0)
b = b << 4;
}
return b;
}
ESP8266Protocol ESP8266::parseProtocol(unsigned int timeout)
{
const char* protocols[] = {"TCP", "UDP"};
return (ESP8266Protocol)findStrings(protocols, 2, true, timeout);
}
IPAddress ESP8266::parseIPAddress(unsigned int timeout)
{
IPAddress ip;
for (uint8_t i = 0; i < 4; i++)
ip[i] = (uint8_t)parseInt(timeout);
return ip;
}
void ESP8266::parseMACAddress(byte* mac, unsigned int timeout)
{
for (uint8_t i = 0; i < 6; i++) {
mac[i] = (uint8_t)parseHex(timeout);
if (i < 5)
timedRead(timeout);
}
}
ESP8266CommandStatus ESP8266::readStatus(unsigned int timeout)
{
const char* statuses[] = {"OK\r\n", "no change\r\n", "ERROR\r\n", "link is not\r\n", "too long\r\n", "FAIL\r\n", "ALREAY CONNECT\r\n"};
return (ESP8266CommandStatus)findStrings(statuses, 7, false, timeout);
}
bool ESP8266::find(const __FlashStringHelper* target)
{
return find(target, 0);
}
bool ESP8266::find(const __FlashStringHelper* target, unsigned int timeout)
{
PGM_P p = reinterpret_cast<PGM_P>(target);
size_t index = 0;
int c;
// read until timeout
while ((c = timedRead(timeout)) >= 0) {
#ifdef ESP8266_DEBUG
Serial.write(c);
#endif
// match magic
if(c == pgm_read_byte(p + index)) {
if (pgm_read_byte(p + ++index) == 0)
return true;
} else
index = 0;
}
return false;
}
size_t ESP8266::readUntil(char* buffer, size_t length, const __FlashStringHelper* target, unsigned int timeout)
{
PGM_P p;
size_t index = 0;
size_t bufferIndex = 0;
int c;
if (target != NULL) // target is specified
p = reinterpret_cast<PGM_P>(target);
// read until timeout
while ((c = timedRead(timeout)) >= 0) {
// skip first carriage return and new line characters
if (bufferIndex == 0 && (c == 13 || c == 10))
continue;
if (target != NULL) { // target is specified
// match magic
if(c == pgm_read_byte(p + index)) {
if (pgm_read_byte(p + ++index) == 0)
return bufferIndex;
} else
index = 0;
}
// store in buffer if char does not match any of the target
if (index == 0) {
bufferIndex++;
*buffer++ = (char)c;
// return if length is reached
if (bufferIndex >= length)
return bufferIndex;
}
}
return bufferIndex;
}
int ESP8266::findStrings(const char** strings, unsigned int count, bool strict, unsigned int timeout)
{
unsigned int indexes[10] = {};
bool match;
int c;
// read until match or timeout
while ((c = timedRead(timeout)) >= 0) {
match = false;
#ifdef ESP8266_DEBUG
Serial.write(c);
#endif
// loop over possible strings
for (int i = 0; i < count; i++) {
// match magic
if(c == strings[i][indexes[i]]) {
match = true;
if (strings[i][++indexes[i]] == 0)
return i;
} else
indexes[i] = 0;
}
// strict mode
if (strict && !match)
return -2;
}
return -1;
}