-
Notifications
You must be signed in to change notification settings - Fork 20
/
GPRS_Shield_Arduino.cpp
802 lines (698 loc) · 20.3 KB
/
GPRS_Shield_Arduino.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
/*
* GPRS_Shield_Arduino.cpp
* A library for SeeedStudio seeeduino GPRS shield
*
* Copyright (c) 2015 seeed technology inc.
* Website : www.seeed.cc
* Author : lawliet zou
* Create Time: April 2015
* Change Log :
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <HardwareSerial.h>
#include "GPRS_Shield_Arduino.h"
GPRS* GPRS::inst;
GPRS::GPRS(Stream& serial, uint8_t pkPin, uint8_t stPin)
{
_stPin = stPin;
_pkPin = pkPin;
inst = this;
stream = &serial;
sim900_init(stream);
}
bool GPRS::init(void)
{
if (!sim900_check_with_cmd("AT\r\n","OK\r\n",CMD))
return false;
if (!sim900_check_with_cmd("AT+CFUN=1\r\n","OK\r\n",CMD))
return false;
if (!checkSIMStatus())
return false;
if (!sim900_check_with_cmd("AT+CNMI?\r\n", "+CNMI: 2,2,0,0,0\r\nOK\r\n", CMD)) {
if (!sim900_check_with_cmd("AT+CNMI=2,2,0,0,0\r\n","OK\r\n", CMD)) {
return false;
}
}
if (!sim900_check_with_cmd("AT+CMGF?\r\n", "+CMGF: 1\r\nOK\r\n", CMD)) {
if (!sim900_check_with_cmd("AT+CMGF=1\r\n","OK\r\n", CMD)) {
return false;
}
}
if (!sim900_check_with_cmd("AT+CLIP=1\r\n","OK\r\n", CMD)) {
return false;
}
return true;
}
bool GPRS::checkPowerUp(void) {
return sim900_check_with_cmd("AT\r\n","OK\r\n", CMD);
}
// power Up GPRS Shield
void GPRS::powerOn() {
delay(1000);
pinMode(_pkPin, OUTPUT);
if (!digitalRead(_stPin)) {
digitalWrite(_pkPin, HIGH);
delay(3000);
}
digitalWrite(_pkPin, LOW);
delay(3000);
}
// power Off GPRS Shield
void GPRS::powerOff() {
pinMode(_pkPin, OUTPUT);
if (digitalRead(_stPin)) {
digitalWrite(_pkPin, HIGH);
delay(3000);
}
digitalWrite(_pkPin, LOW);
delay(3000);
}
bool GPRS::checkSIMStatus(void)
{
char gprsBuffer[32];
int count = 0;
sim900_clean_buffer(gprsBuffer,32);
while(count < 3) {
sim900_send_cmd("AT+CPIN?\r\n");
sim900_read_buffer(gprsBuffer,32,DEFAULT_TIMEOUT);
if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
break;
}
count++;
delay(300);
}
if(count == 3) {
return false;
}
return true;
}
bool GPRS::sendSMS(char *number, char *data)
{
// Set message mode to ASCII
if(!sim900_check_with_cmd("AT+CMGF=1\r\n", "OK\r\n", CMD)) {
return false;
}
delay(500);
sim900_send_cmd("AT+CMGS=\"");
sim900_send_cmd(number);
if(!sim900_check_with_cmd("\"\r\n",">",CMD)) {
return false;
}
delay(1000);
sim900_send_cmd(data);
delay(500);
sim900_send_End_Mark();
return true;
}
char GPRS::isSMSunread()
{
char gprsBuffer[48]; //48 is enough to see +CMGL:
char *s;
//List of all UNREAD SMS and DON'T change the SMS UNREAD STATUS
sim900_send_cmd(F("AT+CMGL=\"REC UNREAD\",1\r\n"));
/*If you want to change SMS status to READ you will need to send:
AT+CMGL=\"REC UNREAD\"\r\n
This command will list all UNREAD SMS and change all of them to READ
If there is not SMS, response is (30 chars)
AT+CMGL="REC UNREAD",1 --> 22 + 2
--> 2
OK --> 2 + 2
If there is SMS, response is like (>64 chars)
AT+CMGL="REC UNREAD",1
+CMGL: 9,"REC UNREAD","XXXXXXXXX","","14/10/16,21:40:08+08"
Here SMS text.
OK
or
AT+CMGL="REC UNREAD",1
+CMGL: 9,"REC UNREAD","XXXXXXXXX","","14/10/16,21:40:08+08"
Here SMS text.
+CMGL: 10,"REC UNREAD","YYYYYYYYY","","14/10/16,21:40:08+08"
Here second SMS
OK
*/
sim900_clean_buffer(gprsBuffer,31);
sim900_read_buffer(gprsBuffer,30,DEFAULT_TIMEOUT);
//Serial.print("Buffer isSMSunread: ");Serial.println(gprsBuffer);
if(NULL != ( s = strstr(gprsBuffer,"OK"))) {
//In 30 bytes "doesn't" fit whole +CMGL: response, if recieve only "OK"
// means you don't have any UNREAD SMS
delay(50);
return 0;
} else {
//More buffer to read
//We are going to flush serial data until OK is recieved
sim900_wait_for_resp("OK\r\n", CMD);
//sim900_flush_serial();
//We have to call command again
sim900_send_cmd("AT+CMGL=\"REC UNREAD\",1\r\n");
sim900_clean_buffer(gprsBuffer,48);
sim900_read_buffer(gprsBuffer,47,DEFAULT_TIMEOUT);
//Serial.print("Buffer isSMSunread 2: ");Serial.println(gprsBuffer);
if(NULL != ( s = strstr(gprsBuffer,"+CMGL:"))) {
//There is at least one UNREAD SMS, get index/position
s = strstr(gprsBuffer,":");
if (s != NULL) {
//We are going to flush serial data until OK is recieved
sim900_wait_for_resp("OK\r\n", CMD);
return atoi(s+1);
}
} else {
return -1;
}
}
return -1;
}
void GPRS::readSMS(char *message, char *phone, char *datetime)
{
/* Response is like:
+CMT: "+79772941911","","15/12/15,01:51:24+12"
+CMGR: "REC READ","XXXXXXXXXXX","","14/10/09,17:30:17+08"
SMS text here
*/
int i = 0;
int j = 0;
char gprsBuffer[80 + 160];
sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer));
sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer));
int len = strlen(gprsBuffer);
if(gprsBuffer[i]=='\"') {
i++;
j = 0;
while(gprsBuffer[i]!='\"') {
phone[j++] = gprsBuffer[i++];
}
phone[j] = '\0';
i++;
}
if(gprsBuffer[i]==',')
i++;
if(gprsBuffer[i]=='\"') {
i++;
while(gprsBuffer[i]!='\"') {
i++;
}
i++;
}
if(gprsBuffer[i]==',')
i++;
if(gprsBuffer[i]=='\"') {
i++;
j = 0;
while(gprsBuffer[i]!='\"') {
datetime[j++] = gprsBuffer[i++];
}
datetime[j] = '\0';
i++;
}
if(gprsBuffer[i]=='\r')
i++;
if(gprsBuffer[i]=='\n')
i++;
j = 0;
while(i < len - 2)
message[j++] = gprsBuffer[i++];
message[j] = '\0';
}
bool GPRS::readSMS(int messageIndex, char *message,int length)
{
int i = 0;
char gprsBuffer[100];
char num[4];
char *p,*s;
sim900_check_with_cmd("AT+CMGF=1\r\n","OK\r\n",CMD);
delay(1000);
sim900_send_cmd("AT+CMGR=");
itoa(messageIndex, num, 10);
sim900_send_cmd(num);
sim900_send_cmd("\r\n");
sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer));
sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),DEFAULT_TIMEOUT);
if(NULL != ( s = strstr(gprsBuffer,"+CMGR:"))){
if(NULL != ( s = strstr(s,"\r\n"))){
p = s + 2;
while((*p != '\r')&&(i < length-1)) {
message[i++] = *(p++);
}
message[i] = '\0';
return true;
}
}
return false;
}
void GPRS::readSMS()
{
char gprsBuffer[100];
sim900_clean_buffer(gprsBuffer,sizeof(gprsBuffer));
sim900_read_buffer(gprsBuffer,sizeof(gprsBuffer),DEFAULT_TIMEOUT);
Serial.write(gprsBuffer);
}
bool GPRS::deleteSMS(int index)
{
//char cmd[16];
char num[4];
//sprintf(cmd,"AT+CMGD=%d\r\n",index);
sim900_send_cmd("AT+CMGD=");
itoa(index, num, 10);
sim900_send_cmd(num);
//snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
//sim900_send_cmd(cmd);
//return 0;
// We have to wait OK response
//return sim900_check_with_cmd(cmd,"OK\r\n",CMD);
return sim900_check_with_cmd("\r\n","OK\r\n",CMD);
}
bool GPRS::ifSMSNow(void)
{
return sim900_check_with_cmd("","+CMT: ",CMD);
}
bool GPRS::callUp(char *number)
{
if(!sim900_check_with_cmd("AT+COLP=1\r\n","OK\r\n",CMD)) {
return false;
}
delay(1000);
sim900_send_cmd("ATD");
sim900_send_cmd(number);
sim900_send_cmd(";\r\n");
return true;
}
void GPRS::answer(void)
{
//TO CHECK: ATA doesnt return "OK" ????
sim900_send_cmd("ATA\r\n");
}
bool GPRS::ifcallNow(void)
{
return sim900_check_with_cmd("","RING\r\n",CMD);
}
bool GPRS::ifcallEnd(void)
{
return sim900_check_with_cmd("","NO CARRIER\r\n",CMD);
}
void GPRS::callEnd(void)
{
sim900_send_cmd("ATH0\r\n");
}
bool GPRS::hangup(void)
{
return sim900_check_with_cmd("ATH\r\n","OK\r\n",CMD);
}
bool GPRS::disableCLIPring(void)
{
return sim900_check_with_cmd("AT+CLIP=0\r\n","OK\r\n",CMD);
}
bool GPRS::isCallActive(char *number)
{
char gprsBuffer[46]; //46 is enough to see +CPAS: and CLCC:
char *p, *s;
int i = 0;
sim900_send_cmd("AT+CPAS\r\n");
/*Result code:
0: ready
2: unknown
3: ringing
4: call in progress
AT+CPAS --> 7 + 2 = 9 chars
--> 2 char
+CPAS: 3 --> 8 + 2 = 10 chars
--> 2 char
OK --> 2 + 2 = 4 chars
AT+CPAS
+CPAS: 0
OK
*/
sim900_clean_buffer(gprsBuffer,29);
sim900_read_buffer(gprsBuffer,27);
//HACERR cuando haga lo de esperar a OK no me haría falta esto
//We are going to flush serial data until OK is recieved
sim900_wait_for_resp("OK\r\n", CMD);
//Serial.print("Buffer isCallActive 1: ");Serial.println(gprsBuffer);
if(NULL != ( s = strstr(gprsBuffer,"+CPAS:"))) {
s = s + 7;
if (*s != '0') {
//There is something "running" (but number 2 that is unknow)
if (*s != '2') {
//3 or 4, let's go to check for the number
sim900_send_cmd("AT+CLCC\r\n");
/*
AT+CLCC --> 9
+CLCC: 1,1,4,0,0,"656783741",161,""
OK
Without ringing:
AT+CLCC
OK
*/
sim900_clean_buffer(gprsBuffer,46);
sim900_read_buffer(gprsBuffer,45);
//Serial.print("Buffer isCallActive 2: ");Serial.println(gprsBuffer);
if(NULL != ( s = strstr(gprsBuffer,"+CLCC:"))) {
//There is at least one CALL ACTIVE, get number
s = strstr((char *)(s),"\"");
s = s + 1; //We are in the first phone number character
p = strstr((char *)(s),"\""); //p is last character """
if (NULL != s) {
i = 0;
while (s < p) {
number[i++] = *(s++);
}
number[i] = '\0';
}
//I need to read more buffer
//We are going to flush serial data until OK is recieved
return sim900_wait_for_resp("OK\r\n", CMD);
}
}
}
}
return false;
}
bool GPRS::getDateTime(char *buffer)
{
//AT+CCLK? --> 8 + CRLF = 10
//+CCLK: "14/11/13,21:14:41+04" --> 29+ CRLF = 31
// --> CRLF = 2
//OK
byte i = 0;
char gprsBuffer[46];
char *p,*s;
sim900_send_cmd("AT+CCLK?\r\n");
sim900_clean_buffer(gprsBuffer,43);
sim900_read_buffer(gprsBuffer,43,DEFAULT_TIMEOUT);
if(NULL != ( s = strstr(gprsBuffer,"+CCLK:"))) {
s = strstr((char *)(s),"\"");
s = s + 1; //We are in the first phone number character
p = strstr((char *)(s),"\""); //p is last character """
if (NULL != s) {
i = 0;
while (s < p) {
buffer[i++] = *(s++);
}
buffer[i] = '\0';
}
//We are going to flush serial data until OK is recieved
return sim900_wait_for_resp("OK\r\n", CMD);
}
return false;
}
byte GPRS::getSignalStrength() {
//AT+CSQ: 00,00 --> 13 + CRLF = 15
// --> CRLF = 2
//OK --> 2 + CRLF = 4
byte result = 99;
char gprsBuffer[21];
sim900_clean_buffer(gprsBuffer,21);
char *s;
sim900_send_cmd("AT+CSQ\r\n");
sim900_read_buffer(gprsBuffer,21,DEFAULT_TIMEOUT);
if(NULL != ( s = strstr(gprsBuffer,"+CSQ: "))) {
result = atoi(s+6);
sim900_wait_for_resp("OK\r\n", CMD);
}
return result;
}
//Here is where we ask for APN configuration, with F() so we can save MEMORY
//bool GPRS::join(const __FlashStringHelper *apn, const __FlashStringHelper *userName, const __FlashStringHelper *passWord)
bool GPRS::join(char* apn, char* userName, char* passWord, int timeout)
{
byte i;
char *p, *s;
char ipAddr[32];
/* if(!sim900_check_with_cmd("AT+CIPSHUT\r\n","SHUT OK\r\n", CMD)) {
Serial.write("Error = 1\r\n");
return false;
}
delay(1000);
*/
sim900_send_cmd("AT+CIPSHUT\r\n");
delay(500);
//Select multiple connection
//sim900_check_with_cmd("AT+CIPMUX=1\r\n","OK",DEFAULT_TIMEOUT,CMD);
//set APN. OLD VERSION
//snprintf(cmd,sizeof(cmd),"AT+CSTT=\"%s\",\"%s\",\"%s\"\r\n",_apn,_userName,_passWord);
//sim900_check_with_cmd(cmd, "OK\r\n", DEFAULT_TIMEOUT,CMD);
sim900_send_cmd("AT+CSTT=\"");
sim900_send_cmd(apn);
sim900_send_cmd("\",\"");
sim900_send_cmd(userName);
sim900_send_cmd("\",\"");
sim900_send_cmd(passWord);
sim900_send_cmd("\"\r\n");
delay(500);
//Brings up wireless connection
sim900_send_cmd("AT+CIICR\r\n");
delay(4000);
sim900_wait_for_resp("OK\r\n", CMD);
delay(500);
// sim900_check_with_cmd("AT+CIICR\r\n","OK\r\n", CMD);
//Get local IP address
sim900_send_cmd("AT+CIFSR\r\n");
delay(500);
sim900_clean_buffer(ipAddr,32);
sim900_read_buffer(ipAddr,32,DEFAULT_TIMEOUT);
//Response:
//AT+CIFSR\r\n --> 8 + 2
//\r\n --> 0 + 2
//10.160.57.120\r\n --> 15 + 2 (max) : TOTAL: 29
//Response error:
//AT+CIFSR\r\n
//\r\n
//ERROR\r\n
if (NULL != strstr(ipAddr,"ERROR")) {
Serial.write("Error = 2\r\n");
return false;
}
s = ipAddr + 12;
p = strstr((char *)(s),"\r\n"); //p is last character \r\n
if (NULL != s) {
i = 0;
while (s < p) {
ip_string[i++] = *(s++);
}
ip_string[i] = '\0';
}
_ip = str_to_ip(ip_string);
if(_ip != 0) {
return true;
}
Serial.write("Error = 3\r\n");
return false;
}
void GPRS::disconnect()
{
sim900_send_cmd("AT+CIPSHUT\r\n");
}
bool GPRS::connect(Protocol ptl,const char * host, int port, int timeout)
{
//char cmd[64];
char num[4];
char resp[96];
//sim900_clean_buffer(cmd,64);
if(ptl == TCP) {
sim900_send_cmd("AT+CIPSTART=\"TCP\",\"");
sim900_send_cmd(host);
sim900_send_cmd("\",");
itoa(port, num, 10);
sim900_send_cmd(num);
sim900_send_cmd("\r\n");
// sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n",host, port);
} else if(ptl == UDP) {
sim900_send_cmd("AT+CIPSTART=\"UDP\",\"");
sim900_send_cmd(host);
sim900_send_cmd("\",");
itoa(port, num, 10);
sim900_send_cmd(num);
sim900_send_cmd("\r\n");
// sprintf(cmd, "AT+CIPSTART=\"UDP\",\"%s\",%d\r\n",host, port);
} else {
return false;
}
delay(2000);
//sim900_send_cmd(cmd);
sim900_read_buffer(resp,96,timeout);
//Serial.print("Connect resp: "); Serial.println(resp);
if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
return true;
}
return false;
}
//Overload with F() macro to SAVE memory
bool GPRS::connect(Protocol ptl,const __FlashStringHelper *host, const __FlashStringHelper *port, int timeout)
{
//char cmd[64];
char resp[96];
//sim900_clean_buffer(cmd,64);
if(ptl == TCP) {
sim900_send_cmd(F("AT+CIPSTART=\"TCP\",\"")); //%s\",%d\r\n",host, port);
} else if(ptl == UDP) {
sim900_send_cmd(F("AT+CIPSTART=\"UDP\",\"")); //%s\",%d\r\n",host, port);
} else {
return false;
}
sim900_send_cmd(host);
sim900_send_cmd(F("\","));
sim900_send_cmd(port);
sim900_send_cmd(F("\r\n"));
//Serial.print("Connect: "); Serial.println(cmd);
sim900_read_buffer(resp, 96, timeout);
//Serial.print("Connect resp: "); Serial.println(resp);
if(NULL != strstr(resp,"CONNECT")) { //ALREADY CONNECT or CONNECT OK
return true;
}
return false;
}
bool GPRS::is_connected(void)
{
char resp[96];
sim900_send_cmd("AT+CIPSTATUS\r\n");
sim900_read_buffer(resp,sizeof(resp),DEFAULT_TIMEOUT);
if(NULL != strstr(resp,"CONNECTED")) {
//+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CONNECTED"
return true;
} else {
//+CIPSTATUS: 1,0,"TCP","216.52.233.120","80","CLOSED"
//+CIPSTATUS: 0,,"","","","INITIAL"
return false;
}
}
bool GPRS::close()
{
// if not connected, return
if (!is_connected()) {
return true;
}
return sim900_check_with_cmd("AT+CIPCLOSE\r\n", "CLOSE OK\r\n", CMD);
}
int GPRS::readable(void)
{
return sim900_check_readable();
}
int GPRS::wait_readable(int wait_time)
{
return sim900_wait_readable(wait_time);
}
int GPRS::wait_writeable(int req_size)
{
return req_size+1;
}
int GPRS::send(const char * str, int len)
{
//char cmd[32];
char num[4];
if(len > 0){
//snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len);
//sprintf(cmd,"AT+CIPSEND=%d\r\n",len);
sim900_send_cmd("AT+CIPSEND=");
itoa(len, num, 10);
sim900_send_cmd(num);
if(!sim900_check_with_cmd("\r\n",">",CMD)) {
//if(!sim900_check_with_cmd(cmd,">",CMD)) {
return 0;
}
/*if(0 != sim900_check_with_cmd(str,"SEND OK\r\n", DEFAULT_TIMEOUT * 10 ,DATA)) {
return 0;
}*/
delay(500);
sim900_send_cmd(str);
delay(500);
sim900_send_End_Mark();
if(!sim900_wait_for_resp("SEND OK\r\n", DATA, DEFAULT_TIMEOUT * 10, DEFAULT_INTERCHAR_TIMEOUT * 10)) {
return 0;
}
}
return len;
}
int GPRS::send(const char * str) {
//char cmd[32];
int len=strlen(str);
char num[4];
if(len > 0){
//snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len);
//sprintf(cmd,"AT+CIPSEND=%d\r\n",len);
sim900_send_cmd("AT+CIPSEND=");
itoa(len, num, 10);
sim900_send_cmd(num);
if(!sim900_check_with_cmd("\r\n",">",CMD)) {
//if(!sim900_check_with_cmd(cmd,">",CMD)) {
return 0;
}
/*if(0 != sim900_check_with_cmd(str,"SEND OK\r\n", DEFAULT_TIMEOUT * 10 ,DATA)) {
return 0;
}*/
delay(500);
sim900_send_cmd(str);
delay(500);
sim900_send_End_Mark();
if(!sim900_wait_for_resp("SEND OK\r\n", DATA, DEFAULT_TIMEOUT * 10, DEFAULT_INTERCHAR_TIMEOUT * 10)) {
return 0;
}
}
return len;
}
int GPRS::recv(char* buf, int len)
{
sim900_clean_buffer(buf,len);
sim900_read_buffer(buf,len); //Ya he llamado a la funcion con la longitud del buffer - 1 y luego le estoy añadiendo el 0
return strlen(buf);
}
uint32_t GPRS::str_to_ip(const char* str)
{
uint32_t ip = 0;
char* p = (char*)str;
for(int i = 0; i < 4; i++) {
ip |= atoi(p);
p = strchr(p, '.');
if (p == NULL) {
break;
}
ip <<= 8;
p++;
}
return ip;
}
char* GPRS::getIPAddress()
{
//I have already a buffer with ip_string: snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (_ip>>24)&0xff,(_ip>>16)&0xff,(_ip>>8)&0xff,_ip&0xff);
return ip_string;
}
unsigned long GPRS::getIPnumber()
{
return _ip;
}
void setST(uint8_t stPin)
{
}
void setPK(uint8_t pkPin)
{
}
/* NOT USED bool GPRS::gethostbyname(const char* host, uint32_t* ip)
{
uint32_t addr = str_to_ip(host);
char buf[17];
//snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
if (strcmp(buf, host) == 0) {
*ip = addr;
return true;
}
return false;
}
*/