forked from ve3wwg/ESP8266_TCP_UDP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp8266.cpp
1406 lines (1162 loc) · 28.8 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
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
///////////////////////////////////////////////////////////////////////
// esp8266.cpp -- Implementation of ESP8266 Class
// Date: Mon Oct 26 20:20:22 2015 (C) Warren W. Gay VE3WWG
///////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include "esp8266.hpp"
#define DBG 0
#if DBG
#define RX(x) printf("RX(%c)\n",x)
#define CMD(s) puts(s)
#define CMDX(s) fputs(s,stdout);
#define CMDC(c) putchar(c)
#else
#define NDEBUG 1
#define RX(x)
#define CMD(x)
#define CMDX(s)
#define CMDC(c)
#endif
//////////////////////////////////////////////////////////////////////
// Constructor
//////////////////////////////////////////////////////////////////////
ESP8266::ESP8266(write_func_t writeb,read_func_t readb,poll_func_t rpoll,idle_func_t idle) : writeb(writeb), readb(readb), rpoll(rpoll), idle(idle) {
clear(false);
}
void
ESP8266::clear(bool notify) {
if ( notify && accept_cb )
accept_cb(-1); // Notify server of closure
for ( int sock=0; sock<N_CONNECTION; ++sock ) {
s_state& s = state[sock];
if ( notify && s.open && !s.disconnected && s.rxcallback ) {
s.rxcallback(sock,-1); // Notify app of closure
}
s.open = 0;
s.connected = s.disconnected = 0;
s.rxcallback = 0;
}
channel = -1; // Unknown
strength = -1;
first = '\n';
ready = 0;
wifi_connected = 0;
wifi_got_ip = 0;
resp_ok = resp_fail = 0;
resp_error = 0;
send_ready = send_fail = 0;
send_ok = 0;
resp_connected = 0;
resp_closed = 0;
resp_dnsfail = 0;
version = 0;
error = Ok;
accept_cb = 0;
bufsp = 0;
}
//////////////////////////////////////////////////////////////////////
// Destructor
//////////////////////////////////////////////////////////////////////
ESP8266::~ESP8266() {
}
//////////////////////////////////////////////////////////////////////
// Lookup a socket
//////////////////////////////////////////////////////////////////////
ESP8266::s_state *
ESP8266::lookup(int sock) {
if ( sock < 0 || sock >= N_CONNECTION ) {
error = Invalid;
return 0;
}
return &state[sock];
}
//////////////////////////////////////////////////////////////////////
// Read an unsigned integer into this->resp_id, returning stop char
//////////////////////////////////////////////////////////////////////
char
ESP8266::read_id() {
char b;
resp_id = 0;
while ( (b = readb()) >= '0' && b <= '9' )
resp_id = resp_id * 10 + (b & 0x0F);
return b;
}
//////////////////////////////////////////////////////////////////////
// Read into selected buffer
//////////////////////////////////////////////////////////////////////
char
ESP8266::read_buf(int bufx,char stop) {
char *buf = bufsp[bufx].buf;
int x = 0, maxlen = bufsp[bufx].bufsiz;
char b;
while ( (b = readb()) != stop && b != '\r' ) {
if ( buf && x + 1 >= maxlen )
break;
if ( buf )
buf[x++] = b;
}
if ( buf )
buf[x] = 0;
return skip_until(b,stop);
}
//////////////////////////////////////////////////////////////////////
// Skip until stop char is read, else stop if CR is reached
//////////////////////////////////////////////////////////////////////
char
ESP8266::skip_until(char b,char stop) {
do {
if ( b == stop )
return b;
b = readb();
} while ( b != '\r' );
return b;
}
//////////////////////////////////////////////////////////////////////
// Perform receive functions
//////////////////////////////////////////////////////////////////////
void
ESP8266::receive() {
struct s_rxstate;
static struct s_rxstate {
const char *pattern;
short start;
short stateno;
} rxstate[] = {
{ "+IPD,", 0, 0x0100 },
{ "+CWAUTOCONN:", 1, 0x0101 },
{ "+CWJAP:\"", 3, 0x0111 },
{ "+CWSAP:\"", 3, 0x0134 },
{ "+CIPAP:ip:\"", 2, 0x0102 },
{ "+CIPAP:gateway:\"", 7, 0x0112 },
{ "+CIPAP:netmask:\"", 7, 0x0122 },
{ "+CIPAPMAC:\"", 6, 0x0103 },
{ "+CIPSTA:ip:\"", 4, 0x0104 },
{ "+CIPMODE:", 4, 0x0107 },
{ "+CIPMUX", 5, 0x0108 },
{ "+CIPSTA:gateway:\"", 8, 0x0114 },
{ "+CIPSTA:netmask:\"", 8, 0x0124 },
{ "+CIPSTAMAC:\"", 7, 0x0105 },
{ "+CIPSTO:", 6, 0x0106 },
{ "OK", 0, 0x0200 },
{ "FAIL", 0, 0x0201 },
{ "ERROR", 0, 0x0202 },
{ "SEND OK", 0, 0x0300 },
{ ",CONNECT", 0, 0x0400 },
{ ",CLOSED", 2, 0x0500 },
{ "DNS Fail", 0, 0x0600 },
{ "WIFI DISCONNECT", 0, 0x0700 },
{ "WIFI CONNECT", 5, 0x0701 },
{ "WIFI GOT IP", 5, 0x0702 },
{ "AT version:", 0, 0x0800 },
{ "No AP", 0, 0x0900 },
{ "ready\r", 0, 0x7F00 },
{ 0, 0, 0x0000 }
};
char b;
while ( rpoll() ) {
b = readb();
#if DBG >= 3
printf("rx b='%c' %02X (first=%02X, s0=%d, ss=%d)\n",b,b,first,s0,ss);
#endif
if ( b == '\n' ) {
first = '\n';
s0 = ss = 0;
continue;
}
if ( first == '\n' ) {
first = b;
if ( first >= '0' && first <= '9' ) {
first = '9';
resp_id = 0;
} else if ( first == '>') {
send_ready = 1;
first = 0;
#if DBG >= 2
puts("))) SENDING>");
#endif
continue;
}
} else if ( !first ) {
continue;
}
// printf("first=%04X, b='%c' %02X, s0=%d, ss=%d\n",first,b,b,s0,ss);
if ( first == '9' ) {
if ( b == ',' ) {
first = b;
s0 = ss = 0;
} else {
resp_id = resp_id * 10 + ( b & 0x0F );
continue;
}
}
if ( !ss && first < 0x0100 ) {
// Locate matching first character
for ( s0 = 0; rxstate[s0].pattern && rxstate[s0].pattern[0] != first; ++s0 )
;
if ( !rxstate[s0].pattern ) {
first = 0;
continue;
}
}
if ( b == rxstate[s0].pattern[ss] ) {
if ( !rxstate[s0].pattern[++ss] ) {
#if DBG >= 2
printf("STATE = 0x%04X (%s)\n",rxstate[s0].stateno,rxstate[s0].pattern);
#endif
switch ( rxstate[s0].stateno ) {
case 0x0100: // "+IPD,",
{
b = read_id();
ipd_id = resp_id;
b = read_id();
ipd_len = resp_id;
// Stops on b=':'
// Read session data
#if DBG
printf("))) +IPD,%d,%d:\n",ipd_id,ipd_len);
#endif
s_state *statep = lookup(ipd_id);
recv_func_t rx_cb = statep ? statep->rxcallback : 0;
while ( ipd_len > 0 ) {
b = readb();
--ipd_len;
if ( rx_cb )
rx_cb(ipd_id,b);
#if DBG
else printf(" +IPD(%d,ch='%c' %02X) bytes remaining %d\n",ipd_id,b,b,ipd_len);
#endif
}
if ( statep->udp && rx_cb ) // Is this a UDP socket?
rx_cb(ipd_id,-1); // yes, send -1 to indicate end of datagram
first = '\n';
ipd_id = ipd_len = 0;
resp_id = 0;
s0 = ss = 0;
}
continue;
case 0x0101: // "+CWAUTOCONN:",
b = readb();
resp_id = b == '0' ? 0 : 1;
break;
case 0x0111: // +CWJAP:"NETGEAR67","c0:ff:d4:95:80:04",7,-66
wifi_connected = 1;
b = read_buf(0,'"');
b = skip_until(0,'"');
b = read_buf(1,'"');
b = skip_until(b,',');
b = read_buf(2,',');
b = read_buf(3,'\r');
break;
case 0x0102: // "+CIPAP:ip:\""
b = read_buf(0,'"');
if ( !wifi_got_ip ) {
// Invoked by is_wifi(bool got_ip=true)
if ( bufsp[0].buf )
wifi_got_ip = strcmp(bufsp[0].buf,"0.0.0.0") != 0;
}
break;
case 0x0112: // "+CIPAP:gateway:\""
b = read_buf(1,'"');
break;
case 0x0122: // "+CIPAP:netmask:\""
b = read_buf(2,'"');
break;
case 0x0103: // "+CIPAPMAC:\"",
b = read_buf(0,'"');
break;
case 0x0104: // "+CIPSTA:ip:\"",
b = read_buf(0,'"');
break;
case 0x0114: // "+CIPSTA:gateway:\"",
b = read_buf(1,'"');
break;
case 0x0124: // "+CIPSTA:netmask:\"",
b = read_buf(2,'"');
break;
case 0x0134: // +CWSAP:"AI-THINKER_FA205E","",11,0
assert(bufsp);
b = read_buf(0,'"');
b = skip_until(b,',');
b = skip_until(b,'"');
b = read_buf(1,'"');
b = skip_until(b,',');
b = read_buf(2,',');
b = read_buf(3,'\r');
first = 0;
break;
case 0x0105: // "+CIPSTAMAC:\"",
b = read_buf(0,'"');
break;
case 0x0106: // +CIPSTO:
b = read_id();
break;
case 0x0107: // +CIPMODE:0
case 0x0108: // +CIPMUX:1
b = read_id();
break;
case 0x0200: // "OK",
resp_ok = 1;
break;
case 0x0201: // "FAIL",
resp_fail = 1;
break;
case 0x0202: // "ERROR",
resp_error = 1;
break;
case 0x0300: // "SEND OK",
send_ok = 1;
break;
case 0x0400: // ",CONNECT",
{
s_state *statep = lookup(resp_id);
if ( statep && !statep->open ) {
statep->open = 1;
statep->connected = 1;
statep->disconnected = 0;
if ( accept_cb )
accept_cb(resp_id);
}
}
break;
case 0x0500: // ",CLOSED",
{
resp_closed = 1;
s_state *statep = lookup(resp_id);
if ( statep && statep->open ) {
statep->connected = 0;
if ( statep->rxcallback )
statep->rxcallback(resp_id,-1);
statep->disconnected = 1;
}
}
break;
case 0x0600: // "DNS Fail",
resp_dnsfail = 1;
break;
case 0x0700: // "WIFI DISCONNECT",
wifi_connected = 0;
wifi_got_ip = 0;
break;
case 0x0701: // "WIFI CONNECT",
wifi_connected = 1;
break;
case 0x0702: // "WIFI GOT IP",
wifi_got_ip = 1;
break;
case 0x0800: // "AT version:",
b = read_buf(0,'\r');
first = 0;
break;
case 0x0900: // No AP
wifi_connected = 0;
wifi_got_ip = 0;
break;
case 0x7F00: // "ready\r",
clear(true);
ready = 1;
break;
}
first = 0;
}
} else {
bool matched = false;
const char *cur = rxstate[s0].pattern;
while ( rxstate[++s0].pattern && !strncmp(cur,rxstate[s0].pattern,ss) ) {
if ( ss == rxstate[s0].start && rxstate[s0].pattern[ss] == b ) {
matched = true;
break;
}
}
if ( !matched )
first = 0;
else ++ss;
}
}
if ( idle )
idle();
}
//////////////////////////////////////////////////////////////////////
// Ignore data until LF is read
//////////////////////////////////////////////////////////////////////
void
ESP8266::waitlf() {
while ( readb() != '\n' )
;
first = '\n';
}
//////////////////////////////////////////////////////////////////////
// (Software) Reset the ESP8266
//////////////////////////////////////////////////////////////////////
bool
ESP8266::reset() {
YIELD();
// Reset
ready = 0;
first = '\n';
CMD("AT+RST");
command("AT+RST");
while ( !ready )
YIELD();
return start();
}
//////////////////////////////////////////////////////////////////////
// Here we assume that the ESP8266 pin has been activated and now
// must wait for the reception of the "ready" message. Currently
// this method waits forever if the ready message does not arrive.
//////////////////////////////////////////////////////////////////////
bool
ESP8266::wait_reset() {
ready = 0;
while ( !ready )
YIELD();
return start();
}
//////////////////////////////////////////////////////////////////////
// Set operational parameters:
//
// This method simply turns off echo (ATE0), sets AT+CIPMODE=0 and
// makes certain that we have AT+CIPMUX=1 mode established for TCP/UDP.
//////////////////////////////////////////////////////////////////////
bool
ESP8266::start() {
// Disable echo
CMD("ATE0");
command("ATE0");
if ( !waitokfail() )
return false;
if ( !set_cipmode(0) ) // Check/set AT+CIPMODE=0
return false;
if ( !set_cipmux(1) ) // Check/set AT+CIPMUX=1
return false;
close_all();
return true; // WIFI connected
}
//////////////////////////////////////////////////////////////////////
// Wait until WIFI CONNECTED occurs.
//////////////////////////////////////////////////////////////////////
void
ESP8266::wait_wifi(bool got_ip) {
do {
YIELD();
} while ( !wifi_connected );
if ( got_ip ) {
do {
YIELD();
} while ( !wifi_got_ip );
}
}
//////////////////////////////////////////////////////////////////////
// Return true if we have WIFI (AP), optionally with IP
//////////////////////////////////////////////////////////////////////
bool
ESP8266::is_wifi(bool got_ip) {
int ch, db;
if ( !get_ap_ssid(0,0,0,0,ch,db) )
return false;
if ( !got_ip )
return wifi_connected;
//////////////////////////////////////////////////////////////
// AT+CIPAP?
// +CIPSTA:ip:"192.168.0.73"
// +CIPSTA:gateway:"192.168.0.1"
// +CIPSTA:netmask:"255.255.255.0"
//
// OK
//////////////////////////////////////////////////////////////
char ip[32];
if ( !get_ap_info(ip,sizeof ip,0,0,0,0) )
return false;
return wifi_got_ip;
}
//////////////////////////////////////////////////////////////////////
// Access point connect
//////////////////////////////////////////////////////////////////////
bool
ESP8266::ap_join(const char *ap,const char *passwd) {
bool bf = true;
resp_id = 0;
resp_connected = 0;
resp_closed = 0;
resp_dnsfail = 0;
resp_error = 0;
// Connect to WIFI AP:
// AT+CWJAP="ssid","password"
write("AT+CWJAP=\"");
write(ap);
write("\",\"");
if ( passwd )
write(passwd);
write("\"");
crlf();
bf = waitokfail();
if ( !bf )
error = Fail;
return bf;
}
//////////////////////////////////////////////////////////////////////
// Write CRLF
//////////////////////////////////////////////////////////////////////
void
ESP8266::crlf() {
writeb('\r');
writeb('\n');
}
//////////////////////////////////////////////////////////////////////
// Write a nul terminated string
//////////////////////////////////////////////////////////////////////
void
ESP8266::write(const char *str) {
while ( *str )
writeb(*str++);
}
//////////////////////////////////////////////////////////////////////
// Write a command
//////////////////////////////////////////////////////////////////////
void
ESP8266::command(const char *cmd) {
resp_ok = resp_fail = resp_error = 0;
ESP8266::write(cmd);
crlf();
}
//////////////////////////////////////////////////////////////////////
// Issue ESP command and wait for OK/FAIL/ERROR (returns true for OK)
//////////////////////////////////////////////////////////////////////
bool
ESP8266::commandok(const char *cmd) {
command(cmd);
return waitokfail();
}
//////////////////////////////////////////////////////////////////////
// Read until we get OK/FAIL
//////////////////////////////////////////////////////////////////////
bool
ESP8266::waitokfail() {
resp_ok = resp_fail = resp_error = 0;
do {
YIELD();
} while ( !resp_fail && !resp_ok && !resp_error );
return resp_ok;
}
//////////////////////////////////////////////////////////////////////
// Start a TCP or UDP socket
//////////////////////////////////////////////////////////////////////
int
ESP8266::socket(const char *socktype,const char *host,int port,recv_func_t rx_cb,int local_port) {
int sock = -1;
// Allocate a socket
for ( int x=0; x<N_CONNECTION; ++x ) {
if ( !state[x].open ) {
sock = x;
break;
}
}
if ( sock == -1 ) {
error = Resource;
return -1; // No free sessions
}
YIELD();
s_state& s = state[sock];
s.open = 1; // Mark it as allocated (for now)
s.udp = socktype[0] == 'U';
s.disconnected = 0;
resp_id = 0;
resp_connected = 0;
resp_closed = 0;
resp_dnsfail = 0;
resp_error = 0;
resp_ok = 0;
// Try to connect
CMDX("AT+CIPSTART=");
write("AT+CIPSTART=");
CMDC('0' + sock);
writeb('0' + sock);
CMDX(",\"");
write(",\"");
CMDX(socktype);
write(socktype);
CMDX("\",\"");
write("\",\"");
CMDX(host);
write(host);
CMDX("\",");
write("\",");
// Convert port to string
{
char portbuf[16];
const char *portstr = int2str(port,portbuf,sizeof portbuf);
CMDX(portstr);
write(portstr);
}
if ( local_port >= 0 ) {
char lportbuf[16];
const char *lportstr = int2str(local_port,lportbuf,sizeof lportbuf);
CMDC(',');
writeb(',');
CMDX(lportstr);
write(lportstr);
CMDX(",2");
write(",2");
}
CMDC('\n');
crlf();
do {
YIELD();
if ( resp_error ) {
if ( resp_dnsfail )
error = DNS_Fail;
else error = Fail;
s.open = 0;
return -1;
}
} while ( !resp_ok );
s.connected = 1;
s.rxcallback = rx_cb;
return sock;
}
//////////////////////////////////////////////////////////////////////
// Start TCP connect to host, port. Returns socket if successful,
// else -1 if an error occurred.
//////////////////////////////////////////////////////////////////////
int
ESP8266::tcp_connect(const char *host,int port,recv_func_t rx_cb) {
return socket("TCP",host,port,rx_cb,-1);
}
//////////////////////////////////////////////////////////////////////
// Open a UDP socket for sending to host and port
//////////////////////////////////////////////////////////////////////
int
ESP8266::udp_socket(const char *host,int port,recv_func_t rx_cb,int local_port) {
return socket("UDP",host,port,rx_cb,local_port);
}
//////////////////////////////////////////////////////////////////////
// Close a socket.
//////////////////////////////////////////////////////////////////////
bool
ESP8266::close(int sock) {
s_state *statep = lookup(sock);
bool ok;
if ( !statep || !statep->open ) {
error = Invalid;
return false;
}
statep->open = 0;
if ( !statep->connected )
return true;
{
char sockbuf[16];
const char *sockstr = int2str(sock,sockbuf,sizeof sockbuf);
write("AT+CIPCLOSE=");
write(sockstr);
crlf();
}
state[sock].connected = 0;
ok = waitokfail();
if ( !ok )
error = Fail;
else statep->open = 0; // Closed
return ok;
}
//////////////////////////////////////////////////////////////////////
// Close all sockets (ignoring errors)
//////////////////////////////////////////////////////////////////////
void
ESP8266::close_all() {
for ( int s=0; s<N_CONNECTION; ++s ) {
close(s); // Attempt to close on ESP side
state[s].open = 0; // Force close on our side
}
}
//////////////////////////////////////////////////////////////////////
// Write to a socket.
//////////////////////////////////////////////////////////////////////
int
ESP8266::write(int sock,const char *data,int bytes,const char *udp_address) {
char session;
int wlen, tlen = 0;
bool bf;
s_state *statep = lookup(sock);
if ( !statep || !data || bytes < 0 ) {
error = Invalid;
return -1;
}
if ( statep->disconnected ) {
error = Disconnected;
return -1;
} else if ( udp_address && !statep->udp ) {
error = Invalid;
return -1;
} else if ( bytes == 0 )
return 0;
while ( bytes > 0 ) {
if ( (wlen = bytes) > 1500 )
wlen = 1500;
send_ready = 0;
send_ok = 0;
send_fail = 0;
session = '0' + sock;
write("AT+CIPSEND=");
writeb(session);
writeb(',');
if ( udp_address ) {
// Not supported on all ESP devices
writeb('"');
write(udp_address);
write("\",");
}
{
char buf[16];
const char *bytestr = int2str(bytes,buf,sizeof buf);
write(bytestr);
crlf();
}
bf = waitokfail();
if ( !bf ) {
error = Fail;
return -1;
}
do {
YIELD();
} while ( !send_ready );
first = 0;
int count = bytes;
while ( count-- > 0 )
writeb(*data++);
do {
YIELD();
} while ( !(send_ok || send_fail) );
if ( send_fail )
break;
tlen += wlen;
bytes -= wlen;
}
if ( !send_ok )
error = Fail;
return send_ok ? tlen : -1;
}
//////////////////////////////////////////////////////////////////////
// Return ESP8266 Version Info (only the AT version line is returned)
//
// AT+GMR
// AT version:0.25.0.0(Jun 5 2015 16:27:16)
// SDK version:1.1.1
// Ai-Thinker Technology Co. Ltd.
// Jun 23 2015 23:23:50
// OK
//////////////////////////////////////////////////////////////////////
bool
ESP8266::get_version(char *buf,int bufsiz) {
s_bufs bufs[] = {
{ buf, bufsiz }
};
this->bufsp = bufs;
CMD("AT+GMR");
command("AT+GMR");
if ( !waitokfail() ) {
*buf = 0;
return false;
}
this->bufsp = 0;
return true;
}
//////////////////////////////////////////////////////////////////////
// This method returns the name (if any) of the joined Access Point.
// Optionally, the mac address is also returned (set mac=0 to not
// allocate a buffer). Finally, the channel and signal strength is
// returned.
//////////////////////////////////////////////////////////////////////
bool
ESP8266::get_ap_ssid(char *ssid,int ssid_size,char *mac,int mac_size,int& chan,int& db) {
// +CWJAP:"NETGEAR67","c0:ff:d4:95:80:04",7,-66
char chbuf[6], dbbuf[8];
s_bufs bufs[] = {
{ ssid, ssid_size },
{ mac, mac_size },
{ chbuf, sizeof chbuf },
{ dbbuf, sizeof dbbuf }
};
this->bufsp = bufs;
CMD("AT+CWJAP?");
command("AT+CWJAP?");
if ( !waitokfail() ) {
this->bufsp = 0;
return false;
}
this->bufsp = 0;
this->channel = chan = str2int(chbuf);
this->strength = db = str2int(dbbuf);
return true;
}
//////////////////////////////////////////////////////////////////////
// Request IP/Gateway and Netmask Info
//////////////////////////////////////////////////////////////////////
bool
ESP8266::get_ap_info(char *ip,int ipsiz,char *gw,int gwsiz,char *nm,int nmsiz) {
s_bufs bufs[] = {
{ ip, gwsiz },
{ gw, gwsiz },
{ nm, nmsiz }
};
bool ok;
this->bufsp = bufs;
CMD("AT+CIPAP?");
command("AT+CIPAP?");
ok = waitokfail();
if ( !ok ) {
if ( ip )
*ip = 0;
if ( gw )
*gw = 0;
if ( nm )
*nm = 0;
}
return ok;
}
//////////////////////////////////////////////////////////////////////
// Request Station IP/Gateway and Netmask Info
//////////////////////////////////////////////////////////////////////
bool
ESP8266::get_station_info(char *ip,int ipsiz,char *gw,int gwsiz,char *nmask,int nmasksiz) {
s_bufs bufs[3] = {
{ ip, ipsiz },
{ gw, gwsiz },
{ nmask, nmasksiz }
};
bool ok;
this->bufsp = bufs;
CMD("AT+CIPSTA?");
command("AT+CIPSTA?");
ok = waitokfail();
if ( !ok ) {