-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESP8266_AT_Client.cpp
3022 lines (2642 loc) · 82.2 KB
/
ESP8266_AT_Client.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
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
#include <ESP8266_AT_Client.h>
#include <Print.h>
#include <string.h>
// #define ESP8266_AT_CLIENT_ENABLE_DEBUG
// #define ESP8266_AT_CLIENT_DEBUG_ECHO_EVERYTHING
// #define ESP8266_AT_CLIENT_DEBUG_OUTGOING
// #define ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES
// #define ESP8266_AT_CLIENT_DEBUG_INCOMING
static uint16_t ESP8266_AT_Client::bytesAvailableMax = 0;
#if defined(ESP8266_AT_CLIENT_DEBUG_INCOMING)
#define DEBUG_WINDOW_SIZE (32)
static uint8_t debug_read_window_data[DEBUG_WINDOW_SIZE] = {};
uint8_t* ESP8266_AT_Client::debug_read_window = debug_read_window_data;
static uint16_t ESP8266_AT_Client::debug_read_window_index = 0;
static uint8_t debug_write_window_data[DEBUG_WINDOW_SIZE] = {};
uint8_t* ESP8266_AT_Client::debug_write_window = debug_write_window_data;
static uint16_t ESP8266_AT_Client::debug_write_window_index = 0;
static void ESP8266_AT_Client::printDebugWindow() {
Serial.println("::BEGIN HISTORY::");
Serial.print("MAX STREAM AVAILABLE: ");
Serial.println(bytesAvailableMax);
Serial.println("::READ HISTORY::");
uint16_t idx = debug_read_window_index;
uint16_t count = 0;
while(count < DEBUG_WINDOW_SIZE){
uint8_t b = debug_read_window[idx] & 0xff;
if(isprint(b) || isspace(b)){
Serial.print((char) b);
}
else{
Serial.print(" 0x");
if(b < 0x10) Serial.print('0');
Serial.print(b, HEX);
}
idx++;
if(idx >= DEBUG_WINDOW_SIZE){
idx = 0;
}
count++;
}
delay(1000);
Serial.println("\n::UNREAD HISTORY::");
while(Serial1.available()){
uint8_t b = Serial1.read() & 0xff;
if(isprint(b) || isspace(b)){
Serial.print((char) b);
}
else{
Serial.print(" 0x");
if(b < 0x10) Serial.print('0');
Serial.print(b, HEX);
}
}
Serial.println("\n::WRITE HISTORY::");
idx = debug_write_window_index;
count = 0;
while(count < DEBUG_WINDOW_SIZE){
uint8_t b = debug_write_window[idx] & 0xff;
if(isprint(b) || isspace(b)){
Serial.print((char) b);
}
else{
Serial.print(" 0x");
if(b < 0x10) Serial.print('0');
Serial.print(b, HEX);
}
idx++;
if(idx >= DEBUG_WINDOW_SIZE){
idx = 0;
}
count++;
}
Serial.println("\n::END HISTORY::");
for(;;){
// service the watchdog
digitalWrite(14, LOW);
delay(3);
digitalWrite(14, HIGH);
delay(500);
} // die
}
static void ESP8266_AT_Client::addToDebugReadWindow(uint8_t b){
debug_read_window[debug_read_window_index++] = b;
if(debug_read_window_index >= DEBUG_WINDOW_SIZE){
debug_read_window_index = 0;
}
}
static void ESP8266_AT_Client::addToDebugWriteWindow(uint8_t b){
debug_write_window[debug_write_window_index++] = b;
if(debug_write_window_index >= DEBUG_WINDOW_SIZE){
debug_write_window_index = 0;
}
}
#else
static void ESP8266_AT_Client::printDebugWindow() { }
static void ESP8266_AT_Client::addToDebugReadWindow(uint8_t b){ }
static void ESP8266_AT_Client::addToDebugWriteWindow(uint8_t b){ }
#endif
ESP8266_AT_Client::ESP8266_AT_Client(uint8_t enable_pin){
this->stream = &Serial; // default assumption
this->streamAsPrint = (Print *) this->stream;
this->socket_connected = false;
this->wifi_is_connected = false;
this->ok_flag = false;
this->error_flag = false;
this->ready_flag = false;
this->send_ok_flag = false;
this->listener_started = false;
this->input_buffer = NULL;
this->input_buffer_length = 0;
this->input_buffer_read_idx = 0;
this->input_buffer_write_idx = 0;
this->enable_pin = enable_pin;
this->num_consumed_bytes_in_input_buffer = 0;
this->num_free_bytes_in_input_buffer = 0;
this->debugEnabled = false;
this->debugStream = NULL;
this->tcp_keep_alive_interval_seconds = 120;
}
ESP8266_AT_Client::ESP8266_AT_Client(uint8_t enable_pin, Stream * stream){
this->stream = stream;
this->streamAsPrint = (Print *) this->stream;
this->socket_connected = false;
this->wifi_is_connected = false;
this->ok_flag = false;
this->error_flag = false;
this->ready_flag = false;
this->send_ok_flag = false;
this->listener_started = false;
this->input_buffer = NULL;
this->input_buffer_length = 0;
this->input_buffer_read_idx = 0;
this->input_buffer_write_idx = 0;
this->enable_pin = enable_pin;
this->num_consumed_bytes_in_input_buffer = 0;
this->num_free_bytes_in_input_buffer = 0;
this->debugEnabled = false;
this->debugStream = NULL;
this->tcp_keep_alive_interval_seconds = 120;
}
ESP8266_AT_Client::ESP8266_AT_Client(uint8_t enable_pin, Stream * stream, uint8_t * buf, uint16_t buf_length){
this->stream = stream;
this->streamAsPrint = (Print *) this->stream;
this->socket_connected = false;
this->wifi_is_connected = false;
this->ok_flag = false;
this->error_flag = false;
this->ready_flag = false;
this->send_ok_flag = false;
this->listener_started = false;
this->input_buffer = buf;
this->input_buffer_length = buf_length;
this->input_buffer_read_idx = 0;
this->input_buffer_write_idx = 0;
this->enable_pin = enable_pin;
this->num_consumed_bytes_in_input_buffer = 0;
this->num_free_bytes_in_input_buffer = 0;
this->debugEnabled = false;
this->debugStream = NULL;
this->tcp_keep_alive_interval_seconds = 120;
}
void ESP8266_AT_Client::setDebugStream(Stream * ds){
debugStream = ds;
}
boolean ESP8266_AT_Client::reset(void){
// Serial.print("Available For Write");
// Serial.println(this->streamAsPrint->availableForWrite());
// reset the buffer state pointers
input_buffer_read_idx = 0;
input_buffer_write_idx = 0;
num_consumed_bytes_in_input_buffer = 0;
num_free_bytes_in_input_buffer = input_buffer_length;
numIncomingBytesPending = 0;
socket_connected = false;
wifi_is_connected = false;
listener_started = false;
ok_flag = false;
error_flag = false;
ready_flag = false;
send_ok_flag = false;
pinMode(enable_pin, OUTPUT);
digitalWrite(enable_pin, LOW);
delay(50);
digitalWrite(enable_pin, HIGH);
ESP8266_DEBUG("ESP8266 Hello World.");
// wait for ok or error or timeout
// because 'ready' triggers the ok_flag
const int32_t interval = 10000;
uint32_t current_millis = millis();
uint32_t previous_millis = current_millis;
boolean timeout_flag = false;
while(!error_flag && !ready_flag && !timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
}
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC6");
printDebugWindow();
#endif
}
}
return ready_flag;
}
/** Set the stream where AT commands are sent and responses received
@param stream the AT stream (e.g. &Serial1)
@return returns void
*/
void ESP8266_AT_Client::setStream(Stream * stream){
this->stream = stream;
this->streamAsPrint = (Print *) this->stream;
}
void ESP8266_AT_Client::setInputBuffer(uint8_t * buf, uint16_t buf_length){
input_buffer = buf;
input_buffer_length = buf_length;
num_consumed_bytes_in_input_buffer = 0;
num_free_bytes_in_input_buffer = buf_length;
input_buffer_read_idx = 0;
input_buffer_write_idx = 0;
}
int ESP8266_AT_Client::connect(IPAddress ip){
return connect(ip, 80);
}
int ESP8266_AT_Client::connect(const char *host){
return connect(host, 80);
}
/** Connect to server by IP address
@param ip the IP address to connect to
@param port the port to connect to
@return returns 0 if last command is still executing, 1 success, 2 if there are no resources
*/
int ESP8266_AT_Client::connect(IPAddress ip, uint16_t port, esp8266_connect_proto_t proto){
char host[16] = {0}; // worst case 111.111.111.111 + the null terminator = 16 characters
snprintf(host, 15, "%d.%d.%d.%d", ip[3], ip[2], ip[1], ip[0]);
return connect(host, port, proto);
}
int ESP8266_AT_Client::connect(uint32_t ip, uint16_t port, esp8266_connect_proto_t proto){
char host[16] = {0}; // worst case 111.111.111.111 + the null terminator = 16 characters
IpUint32ToString(ip, (char *) host);
return connect(host, port, proto);
}
/** Connect to server by hostname
@param host the host name to resolve to an IP address and connect to
@param port the port to connect to
@return returns 0 if last command is still executing, 1 success, 2 if there are no resources
*/
int ESP8266_AT_Client::connect(const char *host, uint16_t port, esp8266_connect_proto_t proto){
// only implementing a blocking API - never return 0
// set up an AT command and send it
// then return whether or not it succeeded
socket_connected = false;
listener_started = false;
ESP8266_DEBUG("Connecting to ", (char *) host);
waitForIncomingDataToComplete();
flushInput();
ok_flag = false;
error_flag = false;
streamWrite("AT+CIPSTART=\"");
if(proto == ESP8266_TCP){
streamWrite("TCP");
}
else if(proto == ESP8266_UDP){
streamWrite("UDP");
}
streamWrite("\",\"");
streamWrite(host);
streamWrite("\",");
streamWrite((uint32_t) port);
if(proto == ESP8266_TCP){
streamWrite(",");
streamWrite((uint32_t) tcp_keep_alive_interval_seconds); // keep alive interval in units of seconds
}
streamWrite("\r\n");
const int32_t interval = 5000;
uint32_t current_millis = millis();
uint32_t previous_millis = current_millis;
boolean timeout_flag = false;
while(!error_flag && !ok_flag && !timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
}
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC7");
printDebugWindow();
#endif
}
}
if(ok_flag){
socket_connected = true;
socket_type = proto;
}
return ok_flag;
}
int ESP8266_AT_Client::connect(IPAddress ip, uint16_t port){
return connect(ip, port, ESP8266_TCP);
}
int ESP8266_AT_Client::connect(const char *host, uint16_t port){
return connect(host, port, ESP8266_TCP);
}
boolean ESP8266_AT_Client::connectUDP(uint32_t ip, uint16_t port){
boolean ret = false;
if(connect(ip, port, ESP8266_UDP)){
ret = true;
}
return ret;
}
boolean ESP8266_AT_Client::connectUDP(const char *host, uint16_t port){
boolean ret = false;
if(connect(host, port, ESP8266_UDP)){
ret = true;
}
return ret;
}
boolean ESP8266_AT_Client::setMacAddress(uint8_t * mac_address){
boolean ret = false;
char mac_str[18] = {0};
macArrayToString(mac_address, (char *) mac_str);
boolean timeout_flag = false;
if(strlen(mac_str) == 17){ // e.g. 00:04:4a:23:11:7b
waitForIncomingDataToComplete();
flushInput();
ok_flag = false;
error_flag = false;
streamWrite("AT+CIPSTAMAC_CUR=\"");
streamWrite(mac_str);
streamWrite("\"\r\n");
const int32_t interval = 5000;
uint32_t current_millis = millis();
uint32_t previous_millis = current_millis;
while(!error_flag && !ok_flag && !timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
}
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC7");
printDebugWindow();
#endif
}
if(ok_flag){
ret = true;
}
}
}
return ret;
}
boolean ESP8266_AT_Client::listen(uint16_t port){
boolean ret = false;
flushInput();
// setup tcp server
waitForIncomingDataToComplete();
ok_flag = false;
error_flag = false;
streamWrite("AT+CIPMUX=1");
streamWrite("\r\n");
const int32_t interval = 500;
uint32_t current_millis = millis();
uint32_t previous_millis = current_millis;
boolean timeout_flag = false;
while(!error_flag && !ok_flag && !timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
}
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC11");
printDebugWindow();
#endif
}
}
if(ok_flag){ // so far so good
delay(100);
flushInput();
ret = false;
waitForIncomingDataToComplete();
ok_flag = false;
error_flag = false;
streamWrite("AT+CIPSERVER=1,");
streamWrite((uint32_t) port);
streamWrite("\r\n");
current_millis = millis();
previous_millis = current_millis;
timeout_flag = false;
while(!error_flag && !ok_flag && !timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
}
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC14");
printDebugWindow();
#endif
}
if(ok_flag){
ret = true;
}
}
}
return ret;
}
boolean ESP8266_AT_Client::configureSoftAP(const char *ssid, const char *pwd, uint8_t channel, uint8_t sec){
waitForIncomingDataToComplete();
flushInput();
ok_flag = false;
error_flag = false;
streamWrite("AT+CWSAP_CUR=\"");
streamWrite(ssid);
streamWrite("\",\"");
streamWrite(pwd);
streamWrite("\",");
streamWrite((uint32_t) channel);
streamWrite(",");
streamWrite((uint32_t) sec);
streamWrite("\r\n");
const int32_t interval = 10000;
uint32_t current_millis = millis();
uint32_t previous_millis = current_millis;
boolean timeout_flag = false;
while(!error_flag && !ok_flag && !timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
}
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC15");
printDebugWindow();
#endif
}
}
return ok_flag;
}
// 0 : disable sleep mode
// 1 : light-sleep mode
// 2 : modem-sleep mode
boolean ESP8266_AT_Client::sleep(uint8_t mode){
waitForIncomingDataToComplete();
ok_flag = false;
error_flag = false;
streamWrite("AT+SLEEP=");
streamWrite((uint32_t) mode);
streamWrite("\r\n");
const int32_t interval = 100;
uint32_t current_millis = millis();
uint32_t previous_millis = current_millis;
boolean timeout_flag = false;
while(!error_flag && !ok_flag && !timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
}
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC17");
printDebugWindow();
#endif
}
}
return ok_flag;
}
// note dnsServer is currently ignored as there is no direct support for it in the AT command set, afaict
// at any rate, we are currently *emulating* DNS in this library, not actually sending explicit DNS requests to a name server
boolean ESP8266_AT_Client::setStaticIPAddress(uint32_t ipAddress, uint32_t netMask, uint32_t defaultGateway, uint32_t dnsServer){
char ip_str[16] = {0};
char netMask_str[16] = {0};
char defaultGateway_str[16] = {0};
char dnsServer_str[16] = {0};
IpUint32ToString(ipAddress, (char *) ip_str);
IpUint32ToString(netMask, (char *) netMask_str);
IpUint32ToString(defaultGateway, (char *) defaultGateway_str);
IpUint32ToString(dnsServer, (char *) dnsServer_str);
waitForIncomingDataToComplete();
ok_flag = false;
error_flag = false;
streamWrite("AT+CIPSTA_CUR=\"");
streamWrite((char *) ip_str);
streamWrite("\",\"");
streamWrite((char *) defaultGateway_str);
streamWrite("\",\"");
streamWrite((char *) netMask_str);
streamWrite("\"\r\n");
const int32_t interval = 1000;
uint32_t current_millis = millis();
uint32_t previous_millis = current_millis;
boolean timeout_flag = false;
while(!error_flag && !ok_flag && !timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
}
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC18");
printDebugWindow();
#endif
}
}
return ok_flag;
}
boolean ESP8266_AT_Client::setDHCP(void){
boolean ret = false;
waitForIncomingDataToComplete();
ok_flag = false;
error_flag = false;
streamWrite("AT+CWDHCP_CUR=1,1\r\n");
const int32_t interval = 2000;
uint32_t current_millis = millis();
uint32_t previous_millis = current_millis;
boolean timeout_flag = false;
while(!error_flag && !ok_flag && !timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
}
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC19");
printDebugWindow();
#endif
}
}
return ok_flag;
}
/** Write a character in request
@param c Character to write
@return 1 if a character was sent 0 otherwise
*/
size_t ESP8266_AT_Client::write(uint8_t c){
uint8_t buf[1] = {c};
return write(buf, 1);
}
/** Write a characters buffer in request
@param buf a buffer of bytes to send
@param sz the number of bytes in the buffer
@return sz if buf was sent 0 otherwise
*/
size_t ESP8266_AT_Client::write(const uint8_t *buf, size_t sz){
size_t ret = 0;
boolean timeout_flag = false;
boolean ok_to_exit = false;
boolean got_ok = false;
boolean got_sendok = false;
boolean wroteData = false;
boolean gotArrow = false;
int32_t interval = 2000;
uint32_t current_millis = millis();
uint32_t previous_millis = current_millis;
// expect to get ">"
// then send the bytes
// then expect to get SEND OK
waitForIncomingDataToComplete();
flushInput();
ok_flag = false;
send_ok_flag = false;
error_flag = false;
streamWrite("AT+CIPSEND=");
if(listener_started){
// TODO: this assumes the link id is zero, and so only supports one connection
streamWrite("0,");
}
streamWrite((uint32_t) sz);
streamWrite("\r\n");
while(!error_flag && !ok_to_exit && !timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
if(b == '>'){
gotArrow = true;
}
}
if(got_ok && !wroteData && gotArrow){ // this is the trigger to write bytes to the output stream
ret = streamWrite(buf, sz); // pass it along
wroteData = true;
// Serial.println("WROTE DATA");
}
}
if(ok_flag){
got_ok = true;
// Serial.println("GOT OK");
}
else if(send_ok_flag){
got_sendok = true;
// Serial.println("GOT SEND OK");
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC16");
printDebugWindow();
#endif
}
else{
ok_to_exit = got_ok && got_sendok && wroteData;
}
}
// NOTE: this next bit is 'magic bullet'
// sometimes sending doesn't pick up the expected event sequence
// so... if you go through all that and timeout without writing
// just go ahead and write anyway and hope for the best
// and this actually seems to work reliably
// don't leave the ESP hanging waiting for data
if((ret < sz) && timeout_flag){
streamWrite(buf + ret, sz - ret);
ret = 0; // mark a failure
// so if ret = 0 it does streamWrite(buf, sz);
// if ret = sz - 1 it does streamWrite(&buf[sz-1], 1);
// and everything in between
}
// Serial.print("RET=");
// Serial.println(ret);
// Serial.println();
// datasheet actually says:
// Enter transparent transmission, with a 20-ms
// interval between each packet, and a maximum of
// 2048 bytes per packet.
// delay(25); // minimum 20ms, lets be generous
// but we can't _actually_ do delay, because
// our send _may_ have inspired a flood of incoming
// packet data, i.e. if we ar downloading a file
// so we need to handle incoming traffic while we wait
interval = 25;
current_millis = millis();
previous_millis = current_millis;
while(current_millis - previous_millis < interval){
current_millis = millis();
flushInput(); // this should handle incoming traffic
}
// should return the number of bytes written
return ret;
}
/** Check if there is data pending receipt
@return 1 if exists, 0 if not exists
*/
int ESP8266_AT_Client::available(){
// an intent to read() is always preceded by a call to available(),
// if the caller knows what is good for them,
// so this is where we need to perform asynchronous receipt of +IPD data
flushInput(); // obviously nobody is waiting for AT command responses
// so consume them bytes
return num_consumed_bytes_in_input_buffer;
}
/** Read a character from stream
@return character
*/
int ESP8266_AT_Client::read(){
return readFromInputBuffer();
}
/** Read from stream and copy size specified to buffer
@param buf Buffer
@param size Buffer size
@return bytes read
*/
int ESP8266_AT_Client::read(uint8_t *buf, size_t size){
int num_bytes_to_read = size;
if(num_consumed_bytes_in_input_buffer < num_bytes_to_read){
num_bytes_to_read = num_consumed_bytes_in_input_buffer;
}
for(int ii = 0; ii < num_bytes_to_read; ii++){
*buf++ = readFromInputBuffer();
}
return num_bytes_to_read;
}
/** Read a character from response buffer but does not move the pointer.
@returns input_buffer[input_buffer_read_idx];
*/
int ESP8266_AT_Client::peek(){
return input_buffer[input_buffer_read_idx];
}
/** Flush response buffer
*/
void ESP8266_AT_Client::flush(){
// Serial.println("FLUSHED");
stream->flush();
}
/** Stop client
*/
void ESP8266_AT_Client::stop(){
if(socket_connected || (socket_type == ESP8266_UDP) || listener_started){
// set up an AT command and send it
// then return whether or not it succeeded
waitForIncomingDataToComplete();
flushInput();
ok_flag = false;
error_flag = false;
streamWrite("AT+CIPCLOSE");
if(listener_started){
streamWrite("=0"); //TODO: assumes target is link id 0
}
streamWrite("\r\n");
const int32_t interval = 5000;
uint32_t current_millis = millis();
uint32_t previous_millis = current_millis;
boolean timeout_flag = false;
while(!error_flag && !ok_flag && !timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
}
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC20");
printDebugWindow();
#endif
}
}
}
// and drop all the remaining unread user buffer data
while(num_consumed_bytes_in_input_buffer > 0){
readFromInputBuffer();
}
socket_connected = false;
}
/** Check if connected to server
@return 1 if connected
*/
uint8_t ESP8266_AT_Client::connected(){
return connected(false);
}
// returns true *only* if the requested SSID is found
// if multiples of the SSID are found, the one with the highest RSSI is returned
boolean ESP8266_AT_Client::scanForAccessPoint(char * ssid, ap_scan_result_t * result, uint8_t * num_results_found, uint32_t timeout_ms){
boolean ret = false;
int8_t max_rssi = -128;
waitForIncomingDataToComplete();
ok_flag = false;
error_flag = false;
streamWrite("AT+CWLAP");
streamWrite("\r\n");
uint8_t match_index = 0xFF;
char line[128] = {0};
uint16_t line_idx = 0;
boolean inside_result = false;
boolean inside_quotes = false;
uint8_t result_number = 0;
const int32_t interval = timeout_ms;
uint32_t current_millis = millis();
uint32_t previous_millis = current_millis;
boolean timeout_flag = false;
// anytime you encounter a '(' start buffering a line
// anytime you encounter a ')' process the buffered line
// exit when you get OK and you aren't inside_result
while(!timeout_flag){
current_millis = millis();
if(stream->available() > 0){
int16_t b = streamReadChar();
if(b > 0){
previous_millis = current_millis;
if(!inside_result){
if(b == '('){
inside_quotes = false;
inside_result = true;
line_idx = 0;
line[0] = 0;
}
else if(ok_flag || error_flag){ // you got OK or ERROR and you're not in a scan result
break; // either we got the result or we didn't, but in any case we're done
}
}
else if(inside_result){
if(!inside_quotes && (b == ')')){
inside_result = false;
// process the line
ap_scan_result_t res = {0};
parseScanResult(&res, line);
result_number++;
if((strcmp(&(res.ssid[0]), ssid) == 0) && (res.rssi > max_rssi)){
*result = res;
max_rssi = res.rssi;
ret = true;
}
}
else {
if(b == '"'){
inside_quotes = !inside_quotes;
}
// if there is still space in the buffer enqueue b
if(line_idx < 127){ // line[127] must always be zero, and we're about to write two consecutive locations
line[line_idx++] = b; // add a character, advance the write index
line[line_idx] = '\0'; // and enforce a null terminator
}
}
}
// otherwise you can ignore the character
// because you are not inside results
// and you have not just seen a '('
}
}
if (current_millis - previous_millis >= interval) {
timeout_flag = true;
#if defined(ESP8266_AT_CLIENT_ENABLE_PANIC_MESSAGES)
Serial.println("PANIC21");
printDebugWindow();
#endif
}
}
*num_results_found = result_number;
return ret;
}