-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDigiFi.cpp
1216 lines (1118 loc) · 29.8 KB
/
DigiFi.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
// DigiX WiFi module example - released by Digistump LLC/Erik Kettenburg under CC-BY-SA 3.0
#include "DigiFi.h"
#define DEBUG
bool digiFiDebugState = false;
uint8_t digiFiMode = TCP;
bool digiFiServer = false;
uint32_t digiFiActivityTimeout = 0;
DigiFi::DigiFi()
{
}
/* Stream Implementation */
int DigiFi::available( void )
{
uint8_t available = Serial1.available();
if(available>0)
digiFiActivityTimeout = millis() +1000;
return available;
}
int DigiFi::peek( void )
{
return Serial1.peek();
}
int DigiFi::read( void )
{
return Serial1.read();
}
int DigiFi::read(uint8_t *buf, size_t size)
{
return Serial1.readBytes((char*)buf,size);
}
void DigiFi::flush( void )
{
return Serial1.flush();
}
void DigiFi::stop( void )
{
startATMode();
setTCPConn("off");
endATMode();
}
void DigiFi::setFlowControl( boolean en )
{
Serial1.setCTSPin(DIGIFI_CTS);
Serial1.enableCTS(en);
}
size_t DigiFi::write( const uint8_t c )
{
digiFiActivityTimeout = millis() + (requestTimeout*1000);
return Serial1.write(c);
}
size_t DigiFi::write(const uint8_t *buf, size_t size)
{
digiFiActivityTimeout = millis() + (requestTimeout*1000);
return Serial1.write(buf,size);
}
void DigiFi::closeChunk()
{
Serial1.println('0');
Serial1.println();
}
void DigiFi::printChunk(int str)
{
printChunk(String(str));
}
void DigiFi::printChunk(long str)
{
printChunk(String(str));
}
void DigiFi::printChunk(const char *str)
{
printChunk(String(str));
}
void DigiFi::printChunk(String str)
{
Serial1.println(str.length()+2,HEX);
Serial1.println(str);
Serial1.println();
}
DigiFi::operator bool() {
return Serial1;
}
void DigiFi::begin(int aBaud, bool en)
{
setFlowControl(en);
Serial1.begin(aBaud);
/** /
//Enable USART HW Flow Control
USART0->US_MR |= US_MR_USART_MODE_HW_HANDSHAKING;
//Disable PIO Control of URTS pin
PIOB->PIO_ABSR |= (0u << 25);
PIOB->PIO_PDR |= PIO_PB25A_RTS0;
//Disable PIO Control of UCTS pin
PIOB->PIO_ABSR |= (0u << 26);
PIOB->PIO_PDR |= PIO_PB26A_CTS0;
//Disable PIO Control of WRTS pin
PIOC->PIO_ABSR |= (0u << 27);
PIOC->PIO_PDR |= (1u << 27);
//Disable PIO Control of WCTS pin
PIOC->PIO_ABSR |= (0u << 20);
PIOC->PIO_PDR |= (1u << 20);
/**/
while(Serial1.available()){Serial1.read();}
}
void DigiFi::startATMode()
{
bool ATsuccess = false;
// ensure the module properly acknowledges
// our request for AT mode. Otherwise retry.
int retries = 0; // TODO: make constant
do {
if (retries > 5) {
debug("Retried 5 times, bailing");
// need to change return-types perhaps to
// trigger some kind of reset higher up.
return;
}
ATsuccess = startATSequence();
retries += 1;
} while (!ATsuccess);
debug("Send client acknowledge AT mode");
Serial1.print("a");
debug(readResponse(0));
debug("echo off");
Serial1.print("AT+E\r");
debug(readResponse(0));
}
bool DigiFi::startATSequence(){
//silly init sequence for wifi module
delay(50); // changed from 100
// clear the incoming buffer
while(Serial1.available()){Serial1.read();}
debug("start at mode");
debug("next");
Serial1.write("+++");
debug("wait for a");
// there's a ~4s (see datasheet for newer G2 module)
// time within which the handshake must complete
// if time is longer than that, it's failed.
// TODO: turn the timeout into a constant
unsigned long timeout = millis() + (4*1000);
while(!Serial1.available()){
delay(1);
if (millis() > timeout) {
debug("FAILED: AT handshake timeout");
return false;
}
}
debug("check for a");
char resp = Serial1.read();
if (resp == 'a') {
debug("OK: module acknowledge AT mode");
return true;
}
// otherwise it's failed.
debug("FAILED: module acknowledge AT mode");
return false;
}
void DigiFi::endATMode()
{
//back to transparent mode
Serial1.print("AT+E\r");
debug(readResponse(0));
Serial1.print("AT+ENTM\r");
debug(readResponse(0));
debug("exit at mode");
}
bool DigiFi::ready(){
startATMode();
//debug("send cmd");
//+ok=<ret><CR>< LF ><CR>< LF >
//”Disconnected”, if no WiFi connection;
//”AP’ SSID(AP’s MAC” ), if WiFi connection available;
//”RF Off”, if WiFi OFF;
debug("Check Link");
String ret = STALinkStatus();
debug("OUT");
debug(ret);
endATMode();
debug(ret);
//change this to report the AP it is connected to
if(ret.substring(0,10) == "+ok=RF Off" || ret.substring(0,16) == "+ok=Disconnected")
return 0;
else
return 1;
}
uint8_t DigiFi::maintain() {return 0;}
IPAddress DigiFi::localIP(){
startATMode();
String response = getSTANetwork();
endATMode();
response = response.substring(response.indexOf(",")+1);
response = response.substring(0,response.indexOf(","));
String ip1 = response.substring(0,response.indexOf("."));
String ip2 = response.substring(response.indexOf(".")+1);
String ip3 = ip2.substring(ip2.indexOf(".")+1);
String ip4 = ip3.substring(ip3.indexOf(".")+1);
ip2 = ip2.substring(0,ip2.indexOf("."));
ip3 = ip3.substring(0,ip3.indexOf("."));
IPAddress ip(ip1.toInt(),ip2.toInt(),ip3.toInt(),ip4.toInt());
return ip;
}
IPAddress DigiFi::subnetMask(){
startATMode();
String response = getSTANetwork();
endATMode();
response = response.substring(response.indexOf(",")+1);
response = response.substring(response.indexOf(",")+1);
response = response.substring(0,response.indexOf(","));
String ip1 = response.substring(0,response.indexOf("."));
String ip2 = response.substring(response.indexOf(".")+1);
String ip3 = ip2.substring(ip2.indexOf(".")+1);
String ip4 = ip3.substring(ip3.indexOf(".")+1);
ip2 = ip2.substring(0,ip2.indexOf("."));
ip3 = ip3.substring(0,ip3.indexOf("."));
IPAddress ip(ip1.toInt(),ip2.toInt(),ip3.toInt(),ip4.toInt());
return ip;
}
IPAddress DigiFi::gatewayIP(){
startATMode();
String response = getSTANetwork();
endATMode();
response = response.substring(response.indexOf(",")+1);
response = response.substring(response.indexOf(",")+1);
response = response.substring(response.indexOf(",")+1);
response = response.substring(0,response.indexOf("\r"));
String ip1 = response.substring(0,response.indexOf("."));
String ip2 = response.substring(response.indexOf(".")+1);
String ip3 = ip2.substring(ip2.indexOf(".")+1);
String ip4 = ip3.substring(ip3.indexOf(".")+1);
ip2 = ip2.substring(0,ip2.indexOf("."));
ip3 = ip3.substring(0,ip3.indexOf("."));
IPAddress ip(ip1.toInt(),ip2.toInt(),ip3.toInt(),ip4.toInt());
return ip;
}
IPAddress DigiFi::dnsServerIP(){
startATMode();
String response = getSTADNS();
endATMode();
response = response.substring(4,response.indexOf("\r"));
String ip1 = response.substring(0,response.indexOf("."));
String ip2 = response.substring(response.indexOf(".")+1);
String ip3 = ip2.substring(ip2.indexOf(".")+1);
String ip4 = ip3.substring(ip3.indexOf(".")+1);
ip2 = ip2.substring(0,ip2.indexOf("."));
ip3 = ip3.substring(0,ip3.indexOf("."));
IPAddress ip(ip1.toInt(),ip2.toInt(),ip3.toInt(),ip4.toInt());
return ip;
}
//server functions
String DigiFi::server(uint16_t port){
startATMode();
while(Serial1.available()){Serial1.read();}
String conn=getNetParams();
String isServer = conn.substring(conn.indexOf("+ok"),conn.length());
isServer = isServer.substring(8,14);
setNetParams("TCP","SERVER",port,"127.0.0.1");
//setTCPConn("On"); //is this needed?
if(isServer != "Server"){
debug("restart for switch to server mode");
reset();
delay(3000);
uint32_t startTime = millis();
while(!ready() && millis()-startTime < 30000){
delay(1000);
}
startATMode();
}
String response = getSTANetwork();
response = response.substring(response.indexOf(",")+1);
response = response.substring(0,response.indexOf(","));
endATMode();
return response;
}
bool DigiFi::serverRequest(){
if(Serial1.available()){
String response = readResponse(0);
response = response.substring(4);
response = response.substring(0,response.indexOf("\n"));
response = response.substring(0,response.lastIndexOf("HTTP/")-1);
debug(response);
serverRequestPathString = response;
return true;
}
else
return false;
}
String DigiFi::serverRequestPath(){
return serverRequestPathString;
}
void DigiFi::serverResponse(String response, int code) //defaults to code = 200
{
Serial1.print("HTTP/1.1 ");
Serial1.print(code);
if(code==200)
Serial1.print(" OK");
else if(code==404)
Serial1.print(" Not Found");
else
Serial1.print(" OK"); //left as OK to not mess anything up
Serial1.print(" \r\n");
Serial1.print("Content-Type: text/html;\r\n");
Serial1.print("Content-Length: ");
Serial1.print(response.length());
Serial1.print("\r\n");
Serial1.print("Connection: close\r\n\r\n");
Serial1.print(response);
Serial1.print("\r\n\r\n");
return;
}
void DigiFi::setTCPTimeout(uint16_t timeout){
startATMode();
Serial1.print("AT+TCPTO=");
Serial1.print(timeout);
Serial1.print("\r");
endATMode();
}
uint8_t DigiFi::connected(){
uint8_t ret = 0;
if(Serial1.available() > 0)
return 1;
if(millis() < digiFiActivityTimeout)
return 1;
startATMode();
debug("Checking for link build up");
String status=getTCPLnk();
if (status.substring(0,6)=="+ok=on")
ret = 1;
endATMode();
return ret;
}
//client functions
int DigiFi::connect(IPAddress ip, uint16_t port = 80){
//uint8_t* server = rawIPAddress(ip);
String server = String(ip[0]) + "." + String(ip[1])+ "." + String(ip[2])+ "." + String(ip[3]);
return connect(server.c_str(),port);
}
int DigiFi::connect(const char *host, uint16_t port = 80){
debug("::connect(*char host, uint port)");
uint8_t lastMode = TCP;
debug("Connect");
startATMode();
debug("send client settings");
setTCPConn("off");
//assuming port 80 for now
String conn=getNetParams();
String isServer = conn.substring(8,14);
if(conn.substring(4,7)=="UDP")
lastMode = UDP;
debug(conn.substring(4,7));
conn=conn.substring(conn.lastIndexOf(',')+1,conn.length()-1);
debug(conn);
debug(host);
debug(isServer);
if(conn != host || isServer == "Server" || lastMode != digiFiMode){
if(digiFiMode == TCP)
setNetParams("TCP","CLIENT",port,host);
else
setNetParams("UDP","CLIENT",port,host);
debug("setting net params");
}
else{
debug("skipping net params");
}
//lastHost = conn;
if(isServer == "Server" || lastMode != digiFiMode){
debug("restart for switch to client mode");
reset();
delay(3000);
startATMode();
setTCPConn("off");
}
setTCPConn("On");
uint32_t linkStart = millis();
if(digiFiMode == TCP){
getNetParams();
debug("Checking for link build up");
String status=getTCPLnk();
while(status.substring(0,6)!="+ok=on"){
debug("Status:");
debug(status);
debug("Re-checking for link build up");
status=getTCPLnk();
debug(status);
if(millis()-linkStart > (requestTimeout*1000)){
endATMode();
return 0;
}
}
}
else{
debug("Checking for host ready");
String status = ping((char*)host);
if(status.substring(0,11)!="+ok=Success"){
while(status.substring(0,11)!="+ok=Success"){
debug("Re-checking for host ready");
status=ping((char*)host);
debug(status);
if(millis()-linkStart > (requestTimeout*1000)){
endATMode();
return 0;
}
}
debug("Wait for UDP to be ready to receive as well");
delay(2000);
}
}
endATMode();
return 1;
}
int DigiFi::disconnect() {
debug("::disconnect(*char host, uint port)");
startATMode();
setTCPConn("off");
endATMode();
return 1;
}
String DigiFi::body(){
return aBody;
}
String DigiFi::header(){
return aHeader;
}
void DigiFi::setDebug(bool debugStateVar){
digiFiDebugState = debugStateVar;
}
void DigiFi::setMode(uint8_t protocol){
digiFiMode = protocol;
}
void DigiFi::debug(String output){
if(digiFiDebugState == true)
Serial.println(output);
}
void DigiFi::debugWrite(char output){
if(digiFiDebugState == true)
Serial.write(output);
}
/*
Return value should be the HTTP return code (i.e. 100 and above).
If something else fails, the non-HTTP error codes are negative numbers.
-1 - connect failure
-2 - connect successful, but request failed
-3 - invalid HTTP return-code returned
*/
int DigiFi::get(char *aHost, char *aPath){
if(connect(aHost) == 1){
//delay(500);
Serial1.print("GET ");
Serial1.print(aPath);
Serial1.print(" HTTP/1.1\r\nHost: ");
Serial1.print(aHost);
Serial1.print("\r\nCache-Control: no-cache\r\nConnection: close\r\n\r\n");
Serial1.flush();
//don't block while awating reply
debug("wait for response...");
bool success = true;
int i=0;
int st = millis();
while(!Serial1.available()){
if(millis() - st > requestTimeout * 1000) {
success = false;
break;
}
if(((millis() - st) % 1000) == 1)
debugWrite('.');
i++;
}
debug("get header");
if(success == false)
return -2;
aHeader = readResponse(0);
debug(aHeader);
String contentLength = aHeader.substring(aHeader.lastIndexOf("Content-Length: "));
contentLength = contentLength.substring(16,contentLength.indexOf("\r"));
debug("Length:"+contentLength+";");
if(contentLength.toInt() != 0) {
debug("get body for later");
aBody = readResponse(contentLength.toInt());
}
else
{
debug("Skip body");
}
debug("return from get");
// work out the returncode
int iRetCode = aHeader.substring(9,12).toInt();
if (iRetCode == 0) {
debug("Invalid return code");
return -3;
}
return iRetCode;
}
else
return -1;
//To do:
/*
User agent!
Better handle timeouts/other errors
Efficiency!
*/
}
String DigiFi::URLEncode(String smsg)
{
const char *msg = smsg.c_str();
const char *hex = "0123456789abcdef";
String encodedMsg = "";
while (*msg!='\0'){
if( ('a' <= *msg && *msg <= 'z')
|| ('A' <= *msg && *msg <= 'Z')
|| ('0' <= *msg && *msg <= '9') ) {
encodedMsg += *msg;
} else {
encodedMsg += '%';
encodedMsg += hex[*msg >> 4];
encodedMsg += hex[*msg & 15];
}
msg++;
}
return encodedMsg;
}
/*
Return value should be the HTTP return code (i.e. 100 and above).
If something else fails, the non-HTTP error codes are negative numbers.
-1 - connect failure
-2 - connect successful, but request failed
-3 - invalid HTTP return-code returned
*/
int DigiFi::post(char *aHost, char *aPath, String postData) {
if(connect(aHost) == 1){
Serial1.print("POST ");
Serial1.print(aPath);
Serial1.print(" HTTP/1.1\r\nHost: ");
Serial1.print(aHost);
Serial1.print("\r\nCache-Control: no-cache\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: close\r\n");
Serial1.print("Content-Length: ");
Serial1.print(postData.length());
Serial1.print("\r\n\r\n");
Serial1.print(postData);
Serial1.print("\r\n\r\n");
Serial1.flush();
debug("wait for response...");
bool success = true;
int i=0;
int st = millis();
while(!Serial1.available()){
if(millis() - st > requestTimeout * 1000) {
success = false;
break;
}
if(((millis() - st) % 1000) == 1)
debugWrite('.');
i++;
}
if(success == false)
return -2;
debug("Get header");
aHeader = readResponse(0);
debug(aHeader);
String contentLength = aHeader.substring(aHeader.lastIndexOf("Content-Length: "));
contentLength = contentLength.substring(16,contentLength.indexOf("\n"));
debug(contentLength);
if(contentLength.toInt() != 0) {
debug("Get body");
aBody = readResponse(contentLength.toInt());
}
else
{
debug("Skip body");
}
// connection: close hard-coded, so disconnect here.
disconnect();
// TODO:
// + make connection: close header optional
// and run disconnect dependent on that option
// + remove the disconnect command (TCPDIS=off)
// from the start of the connect command.
// but need to have connection checking upfront
// first.
// work out the returncode
int iRetCode = aHeader.substring(9,12).toInt();
if (iRetCode == 0) {
debug("Invalid return code");
return -3;
}
return iRetCode;
}
else
return -1;
//To do:
/*
User agent!
accept post data as array or array or string, etc
Better handle timeouts/other errors
Efficiency!
*/
}
void DigiFi::close()
{
//clear buffer
while(Serial1.available()){Serial1.read();}
Serial1.end();
}
String DigiFi::readResponse(int contentLength) //0 = cmd, 1 = header, 2=body
{
String stringBuffer;
char inByte;
int rCount = 0;
int nCount = 0;
int curLength = 0;
bool end = false;
Serial1.flush();
bool timeout = false;
int st = millis();
while (!end)
{
//look for this to be four bytes in a row
if (Serial1.available())
{
inByte = Serial1.read();
curLength++;
//debugWrite(inByte);// disabled, leads to lots of duplicate debug logging
if(contentLength == 0){
if (inByte == '\n' && rCount == 2 && nCount == 1)
{
end = true;
int strLength = stringBuffer.length()-3;
stringBuffer = stringBuffer.substring(0,strLength);
}
else if (inByte == '\r')
rCount++;
else if (inByte == '\n')
nCount++;
else{
rCount = 0;
nCount = 0;
}
}
else if(curLength>=contentLength)
end = true;
stringBuffer += inByte;
}
else
{
// need a timeout otherwise we can get stuck in
// here if the server drops out for some reason
// does though imply that 15s is sufficient to
// retrieve whatever it is your after
// seems OK though as DigiX doesn't have a large
// amount of memory
if(millis() - st > requestTimeout * 1000) {
timeout = true;
break;
}
}
}
if(stringBuffer.substring(0,4) == "+ERR") {
lastErr = stringBuffer.substring(5,2).toInt();
} else if (timeout) {
lastErr = -1;
} else {
lastErr = 0;
}
return stringBuffer;
}
int DigiFi::lastError()
{
return lastErr;
}
String DigiFi::AT(char *cmd, char *params)
{
Serial1.print("AT+");
Serial1.print(cmd);
if(sizeof(*params) > 0)
{
Serial1.print("=");
Serial1.print(params);
}
Serial1.print("\r");
return readResponse(0);
}
void DigiFi::toggleEcho() //E
{
Serial1.print("AT+E\r");
readResponse(0);
}
String DigiFi::getWifiMode() //WMODE AP STA APSTA
{
Serial1.print("AT+WMODE\r");
return readResponse(0);
}
void DigiFi::setWifiMode(char *mode)
{
Serial1.print("AT+WMODE=");
Serial1.print(mode);
Serial1.print("\r");
readResponse(0);
}
void DigiFi::setTransparent() //ENTM
{
Serial1.print("AT+ENTM\r");
readResponse(0);
}
String DigiFi::getTMode() //TMODE throughput cmd
{
Serial1.print("AT+TMODE\r");
return readResponse(0);
}
void DigiFi::setTMode(char *mode)
{
Serial1.print("AT+TMODE=");
Serial1.print(mode);
Serial1.print("\r");
readResponse(0);
}
String DigiFi::getModId() //MID
{
Serial1.print("AT+MID\r");
return readResponse(0);
}
String DigiFi::version() //VER
{
Serial1.print("AT+VER\r");
return readResponse(0);
}
void DigiFi::factoryRestore() //RELD rebooting...
{
Serial1.print("AT+RELD\r");
readResponse(0);
}
void DigiFi::reset() //Z (No return)
{
Serial1.print("AT+Z\r");
//readResponse(0);
lastErr=0; //This command doesnt return anything.
}
String DigiFi::help()//H
{
Serial1.print("AT+H\r");
return readResponse(0);
}
int DigiFi::readConfig(byte* buffer)//CFGRD
{
Serial1.print("AT+CFGRD\r");
Serial1.readBytes((char*)buffer,4);
if((char*)buffer=="+ERR")
return -1; //TODO Set lastErr here (Technically it shouldn't ever error here)
Serial1.readBytes((char*)buffer,2);
int len=(int)word(buffer[1],buffer[0]);
Serial1.readBytes((char*)buffer,len);
return len;
}
void DigiFi::writeConfig(byte* config, int len)//CFGWR
{
Serial1.print("AT+CFGWR=");
Serial1.write(highByte(len));
Serial1.write(lowByte(len));
Serial1.write(config,len);
Serial1.print("\r");
readResponse(0);
}
int DigiFi::readFactoryDef(byte* buffer)//CFGFR
{
Serial1.print("AT+CFGFR\r");
Serial1.readBytes((char*)buffer,4);
if((char*)buffer=="+ERR")
return -1; //TODO Set lastErr here (Technically it shouldn't ever error here)
Serial1.readBytes((char*)buffer,2);
int len=(int)word(buffer[1],buffer[0]);
Serial1.readBytes((char*)buffer,len);
return len;
}
void DigiFi::makeFactory() //CFGTF
{
Serial1.print("AT+CFGTF\r");
readResponse(0);
}
String DigiFi::getUart()//UART baudrate,data_bits,stop_bit,parity
{
Serial1.print("AT+UART\r");
return readResponse(0);
}
void DigiFi::setUart(int baudrate,int data_bits,int stop_bit,char *parity)
{
Serial1.print("AT+UART=");
Serial1.print(baudrate);
Serial1.print(",");
Serial1.print(data_bits);
Serial1.print(",");
Serial1.print(stop_bit);
Serial1.print(",");
Serial1.print(parity);
Serial1.print("\r");
readResponse(0);
}
/*
String getAutoFrame(); //UARTF
void setAutoFrame(char *para);
int getAutoFrmTrigTime(); //UARTFT
void setAutoFrmTrigTime(int ms);
int getAutoFrmTrigLength(); //UARTFL
void setAutoFrmTrigLength(int v);
*/
void DigiFi::sendData(int len, char *data)//SEND
{
Serial1.print("AT+SEND=");
Serial1.print(len);
Serial1.print(",");
Serial1.print(data);
Serial1.print("\r");
readResponse(0);
}
String DigiFi::recvData(int len)//RECV len,data (+ok=0 if timeout (3sec))
{
Serial1.print("AT+RECV=");
Serial1.print(len);
Serial1.print("\r");
return readResponse(0);
}
String DigiFi::ping(char *ip)//PING Success Timeout Unknown host
{
Serial1.print("AT+PING=");
Serial1.print(ip);
Serial1.print("\r");
return readResponse(0);
}
String DigiFi::getNetParams()//NETP (TCP|UDP),(SERVER|CLIENT),port,IP
{
Serial1.print("AT+NETP\r");
return readResponse(0);
}
void DigiFi::setNetParams(char *proto, char *cs, int port, const char *ip)
{
if(cs == "SERVER")
digiFiServer = true;
else
digiFiServer = false;
Serial1.print("AT+NETP=");
Serial1.print(proto);
Serial1.print(",");
Serial1.print(cs);
Serial1.print(",");
Serial1.print(port);
Serial1.print(",");
Serial1.print(ip);
Serial1.print("\r");
readResponse(0);
}
String DigiFi::getTCPLnk()//TCPLK on|off
{
Serial1.print("AT+TCPLK\r");
return readResponse(0);
}
String DigiFi::getTCPTimeout()//TCPTO 0 <= int <= 600 (Def 300)
{
Serial1.print("AT+TCPTO\r");
return readResponse(0);
}
String DigiFi::getTCPConn()//TCPDIS On|off
{
Serial1.print("AT+TCPDIS\r");
return readResponse(0);
}
void DigiFi::setTCPConn(char *sta)
{
Serial1.print("AT+TCPDIS=");
Serial1.print(sta);
Serial1.print("\r");
readResponse(0);
}
String DigiFi::getWSSSID()//WSSSID
{
Serial1.print("AT+WSSSID\r");
return readResponse(0);
}
void DigiFi::setWSSSID(char *ssid)
{
Serial1.print("AT+WSSSID=");
Serial1.print(ssid);
Serial1.print("\r");
readResponse(0);
}
String DigiFi::getSTAKey()//WSKEY (OPEN|SHARED|WPAPSK|WPA2PSK),(NONE|WEP|TKIP|AES),key
{
Serial1.print("AT+WSKEY\r");
return readResponse(0);
}
void DigiFi::setSTAKey(char* auth,char *encry,char *key)
{
Serial1.print("AT+WSKEY=");
Serial1.print(auth);
Serial1.print(",");
Serial1.print(encry);
Serial1.print(",");
Serial1.print(key);
Serial1.print("\r");
readResponse(0);
}
String DigiFi::getSTANetwork()//WANN (static|DHCP),ip,subnet,gateway
{
Serial1.print("AT+WANN\r");
return readResponse(0);
}
void DigiFi::setSTANetwork(char *mode, char *ip, char *subnet, char *gateway)