-
Notifications
You must be signed in to change notification settings - Fork 15
/
SIM900.cpp
1009 lines (837 loc) · 25.9 KB
/
SIM900.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 "SIM900.h"
#include "Streaming.h"
#define _GSM_CONNECTION_TOUT_ 5
#define _TCP_CONNECTION_TOUT_ 20
#define _GSM_DATA_TOUT_ 10
//#define RESETPIN 7
SIMCOM900 gsm;
SIMCOM900::SIMCOM900() {};
SIMCOM900::~SIMCOM900() {};
/**********************************************************
Function: This function premits to wake up the module
(only for SIM908) when it goes in energy saving
mode.
Author: Marco Martines
Created: unknown
Modified: 18/02/2014
Output: none
Comments: It would be nice to call this function
automatically when gsm.begin is called (of course
only if a SIM908 is used).
**********************************************************/
char SIMCOM900::forceON()
{
char ret_val=0;
char *p_char;
char *p_char1;
SimpleWriteln(F("AT+CREG?"));
WaitResp(5000, 100, str_ok);
if(IsStringReceived(str_ok)) {
ret_val=1;
}
p_char = strchr((char *)(gsm.comm_buf),',');
p_char1 = p_char+1;
*(p_char1+2)=0;
p_char = strchr((char *)(p_char1), ',');
if (p_char != NULL) {
*p_char = 0;
}
if((*p_char1)=='4') {
digitalWrite(GSM_ON, HIGH);
delay(1200);
digitalWrite(GSM_ON, LOW);
delay(10000);
ret_val=2;
}
return ret_val;
}
int SIMCOM900::configandwait(char* pin)
{
int connCode;
//_tf.setTimeout(_GSM_CONNECTION_TOUT_);
if(pin) setPIN(pin); //syv
// Try 10 times to register in the network. Note this can take some time!
for(int i=0; i<10; i++) {
//Ask for register status to GPRS network.
SimpleWriteln(F("AT+CGREG?"));
//Se espera la unsolicited response de registered to network.
while(gsm.WaitResp(5000, 50, "+CGREG: 0,")!=RX_FINISHED_STR_RECV)
//while (_tf.find("+CGREG: 0,")) // CHANGE!!!!
{
//connCode=_tf.getValue();
connCode=_cell.read();
if((connCode==1)||(connCode==5)) {
setStatus(READY);
SimpleWriteln(F("AT+CMGF=1")); //SMS text mode.
delay(200);
// Buah, we should take this to readCall()
SimpleWriteln(F("AT+CLIP=1")); //SMS text mode.
delay(200);
//_cell << "AT+QIDEACT" << _DEC(cr) << endl; //To make sure not pending connection.
//delay(1000);
return 1;
}
}
}
return 0;
}
/**
* SIMCOM900::read(char* buffer, int buffersize)
*
* Waits for data to be readable from the gsm module, reads data until
* no more is available or the buffer has been filled
*
* returns number of bytes read
*
*/
int SIMCOM900::read(char* result, int resultlength)
{
char temp;
int i=0;
#ifdef DEBUG_ON
Serial.print(F("Starting read..\nWaiting for Data.."));
#endif
// Wait until we start receiving data
while(gsm.available()<1) {
delay(100);
#ifdef DEBUG_ON
Serial.print(F("."));
#endif
}
while(gsm.available()>0 && i<(resultlength-1)) {
temp=_cell.read();
if(temp>0) {
#ifdef DEBUG_ON
Serial.print(temp);
#endif
result[i]=temp;
i++;
}
delay(1);
}
// Terminate the string
result[resultlength-1]='\0';
#ifdef DEBUG_ON
Serial.println(F("\nDone.."));
#endif
return i;
}
int SIMCOM900::readCellData(int &mcc, int &mnc, long &lac, long &cellid)
{
if (getStatus()==IDLE)
return 0;
//_tf.setTimeout(_GSM_DATA_TOUT_);
//_cell.flush();
SimpleWriteln(F("AT+QENG=1,0"));
SimpleWriteln(F("AT+QENG?"));
if(gsm.WaitResp(5000, 50, "+QENG")!=RX_FINISHED_STR_NOT_RECV)
return 0;
//mcc=_tf.getValue(); // The first one is 0
mcc=_cell.read();
//mcc=_tf.getValue();
mcc=_cell.read();
//mnc=_tf.getValue();
mnc=_cell.read();
//lac=_tf.getValue();
lac=_cell.read();
//cellid=_tf.getValue();
cellid=_cell.read();
gsm.WaitResp(5000, 50, "+OK");
SimpleWriteln(F("AT+QENG=1,0"));
gsm.WaitResp(5000, 50, "+OK");
return 1;
}
boolean SIMCOM900::readSMS(char* msg, int msglength, char* number, int nlength)
{
Serial.println(F("This method is deprecated! Please use GetSMS in the SMS class."));
long index;
char *p_char;
char *p_char1;
/*
if (getStatus()==IDLE)
return false;
*/
#ifdef UNO
_tf.setTimeout(_GSM_DATA_TOUT_);
#endif
//_cell.flush();
WaitResp(500, 500);
SimpleWriteln(F("AT+CMGL=\"REC UNREAD\",1"));
WaitResp(5000, 500);
if(gsm.IsStringReceived("+CMGL")) {
//index
p_char = strchr((char *)(gsm.comm_buf),'+CMGL');
p_char1 = p_char+3; //we are on the first char of string
p_char = p_char1+1;
*p_char = 0;
index=atoi(p_char1);
p_char1 = p_char+1;
p_char = strstr((char *)(p_char1), "\",\"");
p_char1 = p_char+3;
p_char = strstr((char *)(p_char1), "\",\"");
if (p_char != NULL) {
*p_char = 0;
}
strcpy(number, (char *)(p_char1));
//////
p_char1 = p_char+3;
p_char = strstr((char *)(p_char1), "\",\"");
p_char1 = p_char+3;
p_char = strstr((char *)(p_char1), "\n");
p_char1 = p_char+1;
p_char = strstr((char *)(p_char1), "\n");
if (p_char != NULL) {
*p_char = 0;
}
strcpy(msg, (char *)(p_char1));
// #ifdef UNO
// index=_tf.getValue();
// #endif
// #ifdef MEGA
//index=_cell.read();
// #endif
// Serial.println("DEBUG");
// #ifdef UNO
// _tf.getString("\",\"", "\"", number, nlength);
// #endif
// Serial.println("PRIMA");
// #ifdef MEGA
// _cell.getString("\",\"", "\"", number, nlength);
// #endif
// Serial.println("DEBUG");
// #ifdef UNO
// _tf.getString("\n", "\nOK", msg, msglength);
// #endif
// #ifdef MEGA
// _cell.getString("\n", "\nOK", msg, msglength);
// #endif
SimpleWrite(F("AT+CMGD="));
SimpleWriteln(index);
// Serial.print("VAL= ");
// Serial.println(index);
gsm.WaitResp(5000, 50, str_ok);
return true;
};
return false;
};
boolean SIMCOM900::readCall(char* number, int nlength)
{
int index;
if (getStatus()==IDLE)
return false;
//_tf.setTimeout(_GSM_DATA_TOUT_);
if(gsm.WaitResp(5000, 50, "+CLIP: \"")!=RX_FINISHED_STR_RECV)
//if(_tf.find("+CLIP: \""))
{
#ifdef UNO
_tf.getString("", "\"", number, nlength);
#endif
#ifdef MEGA
_cell.getString("", "\"", number, nlength);
#endif
SimpleWriteln(F("ATH"));
delay(1000);
//_cell.flush();
return true;
};
return false;
};
boolean SIMCOM900::call(char* number, unsigned int milliseconds)
{
if (getStatus()==IDLE)
return false;
//_tf.setTimeout(_GSM_DATA_TOUT_);
SimpleWrite(F("ATD"));
SimpleWrite(number);
SimpleWriteln(F(";"));
delay(milliseconds);
SimpleWriteln(F("ATH"));
return true;
}
int SIMCOM900::setPIN(char *pin)
{
//Status = READY or ATTACHED.
if((getStatus() != IDLE))
return 2;
//_tf.setTimeout(_GSM_DATA_TOUT_); //Timeout for expecting modem responses.
//_cell.flush();
//AT command to set PIN.
SimpleWrite(F("AT+CPIN="));
SimpleWriteln(pin);
//Expect str_ok.
if(gsm.WaitResp(5000, 50, str_ok)!=RX_FINISHED_STR_NOT_RECV)
return 0;
else
return 1;
}
int SIMCOM900::changeNSIPmode(char mode)
{
//_tf.setTimeout(_TCP_CONNECTION_TOUT_);
//if (getStatus()!=ATTACHED)
// return 0;
//_cell.flush();
SimpleWrite(F("AT+QIDNSIP="));
SimpleWriteln(mode);
if(gsm.WaitResp(5000, 50, str_ok)!=RX_FINISHED_STR_NOT_RECV) return 0;
//if(!_tf.find(str_ok)) return 0;
return 1;
}
int SIMCOM900::getCCI(char *cci)
{
//Status must be READY
if((getStatus() != READY))
return 2;
//_tf.setTimeout(_GSM_DATA_TOUT_); //Timeout for expecting modem responses.
//_cell.flush();
//AT command to get CCID.
SimpleWriteln(F("AT+QCCID"));
//Read response from modem
#ifdef UNO
_tf.getString("AT+QCCID\r\r\r\n","\r\n",cci, 21);
#endif
#ifdef MEGA
_cell.getString("AT+QCCID\r\r\r\n","\r\n",cci, 21);
#endif
//Expect str_ok.
if(gsm.WaitResp(5000, 50, str_ok)!=RX_FINISHED_STR_NOT_RECV)
return 0;
else
return 1;
}
int SIMCOM900::getIMEI(char *imei)
{
//_tf.setTimeout(_GSM_DATA_TOUT_); //Timeout for expecting modem responses.
//_cell.flush();
//AT command to get IMEI.
SimpleWriteln(F("AT+GSN"));
//Read response from modem
#ifdef UNO
_tf.getString("\r\n","\r\n",imei, 16);
#endif
#ifdef MEGA
_cell.getString("\r\n","\r\n",imei, 16);
#endif
//Expect str_ok.
if(gsm.WaitResp(5000, 50, str_ok)!=RX_FINISHED_STR_NOT_RECV)
return 0;
else
return 1;
}
int SIMCOM900::available()
{
return _cell.available();
}
uint8_t SIMCOM900::read()
{
return _cell.read();
}
void SIMCOM900::SimpleRead()
{
char datain;
if(_cell.available()>0) {
datain=_cell.read();
if(datain>0) {
Serial.print(datain);
}
}
}
void SIMCOM900::SimpleWrite(char *comm)
{
_cell.print(comm);
}
void SIMCOM900::SimpleWrite(const char *comm)
{
_cell.print(comm);
}
void SIMCOM900::SimpleWrite(int comm)
{
_cell.print(comm);
}
void SIMCOM900::SimpleWrite(const __FlashStringHelper *pgmstr)
{
_cell.print(pgmstr);
}
void SIMCOM900::SimpleWriteln(char *comm)
{
_cell.println(comm);
}
void SIMCOM900::SimpleWriteln(const __FlashStringHelper *pgmstr)
{
_cell.println(pgmstr);
}
void SIMCOM900::SimpleWriteln(char const *comm)
{
_cell.println(comm);
}
void SIMCOM900::SimpleWriteln(int comm)
{
_cell.println(comm);
}
void SIMCOM900::WhileSimpleRead()
{
char datain;
while(_cell.available()>0) {
datain=_cell.read();
if(datain>0) {
Serial.print(datain);
}
}
}
//---------------------------------------------
/**********************************************************
Turns on/off the speaker
off_on: 0 - off
1 - on
**********************************************************/
void GSM::SetSpeaker(byte off_on)
{
if (CLS_FREE != GetCommLineStatus()) return;
SetCommLineStatus(CLS_ATCMD);
if (off_on) {
//SendATCmdWaitResp("AT#GPIO=5,1,2", 500, 50, "#GPIO:", 1);
} else {
//SendATCmdWaitResp("AT#GPIO=5,0,2", 500, 50, "#GPIO:", 1);
}
SetCommLineStatus(CLS_FREE);
}
byte GSM::IsRegistered(void)
{
return (module_status & STATUS_REGISTERED);
}
byte GSM::IsInitialized(void)
{
return (module_status & STATUS_INITIALIZED);
}
/**********************************************************
Method checks if the GSM module is registered in the GSM net
- this method communicates directly with the GSM module
in contrast to the method IsRegistered() which reads the
flag from the module_status (this flag is set inside this method)
- must be called regularly - from 1sec. to cca. 10 sec.
return values:
REG_NOT_REGISTERED - not registered
REG_REGISTERED - GSM module is registered
REG_NO_RESPONSE - GSM doesn't response
REG_COMM_LINE_BUSY - comm line between GSM module and Arduino is not free
for communication
**********************************************************/
byte GSM::CheckRegistration(void)
{
byte status;
byte ret_val = REG_NOT_REGISTERED;
if (CLS_FREE != GetCommLineStatus()) return (REG_COMM_LINE_BUSY);
SetCommLineStatus(CLS_ATCMD);
_cell.println(F("AT+CREG?"));
// 5 sec. for initial comm tmout
// 50 msec. for inter character timeout
status = WaitResp(5000, 50);
if (status == RX_FINISHED) {
// something was received but what was received?
// ---------------------------------------------
if(IsStringReceived("+CREG: 0,1")
|| IsStringReceived("+CREG: 0,5")) {
// it means module is registered
// ----------------------------
module_status |= STATUS_REGISTERED;
// in case GSM module is registered first time after reset
// sets flag STATUS_INITIALIZED
// it is used for sending some init commands which
// must be sent only after registration
// --------------------------------------------
if (!IsInitialized()) {
module_status |= STATUS_INITIALIZED;
SetCommLineStatus(CLS_FREE);
InitParam(PARAM_SET_1);
}
ret_val = REG_REGISTERED;
} else {
// NOT registered
// --------------
module_status &= ~STATUS_REGISTERED;
ret_val = REG_NOT_REGISTERED;
}
} else {
// nothing was received
// --------------------
ret_val = REG_NO_RESPONSE;
}
SetCommLineStatus(CLS_FREE);
return (ret_val);
}
/**********************************************************
Method sets speaker volume
speaker_volume: volume in range 0..14
return:
ERROR ret. val:
---------------
-1 - comm. line to the GSM module is not free
-2 - GSM module did not answer in timeout
-3 - GSM module has answered "ERROR" string
OK ret val:
-----------
0..14 current speaker volume
**********************************************************/
/*
char GSM::SetSpeakerVolume(byte speaker_volume)
{
char ret_val = -1;
if (CLS_FREE != GetCommLineStatus()) return (ret_val);
SetCommLineStatus(CLS_ATCMD);
// remember set value as last value
if (speaker_volume > 14) speaker_volume = 14;
// select speaker volume (0 to 14)
// AT+CLVL=X<CR> X<0..14>
_cell.print("AT+CLVL=");
_cell.print((int)speaker_volume);
_cell.print("\r"); // send <CR>
// 10 sec. for initial comm tmout
// 50 msec. for inter character timeout
if (RX_TMOUT_ERR == WaitResp(10000, 50)) {
ret_val = -2; // ERROR
}
else {
if(IsStringReceived(str_ok)) {
last_speaker_volume = speaker_volume;
ret_val = last_speaker_volume; // OK
}
else ret_val = -3; // ERROR
}
SetCommLineStatus(CLS_FREE);
return (ret_val);
}
*/
/**********************************************************
Method increases speaker volume
return:
ERROR ret. val:
---------------
-1 - comm. line to the GSM module is not free
-2 - GSM module did not answer in timeout
-3 - GSM module has answered "ERROR" string
OK ret val:
-----------
0..14 current speaker volume
**********************************************************/
/*
char GSM::IncSpeakerVolume(void)
{
char ret_val;
byte current_speaker_value;
current_speaker_value = last_speaker_volume;
if (current_speaker_value < 14) {
current_speaker_value++;
ret_val = SetSpeakerVolume(current_speaker_value);
}
else ret_val = 14;
return (ret_val);
}
*/
/**********************************************************
Method decreases speaker volume
return:
ERROR ret. val:
---------------
-1 - comm. line to the GSM module is not free
-2 - GSM module did not answer in timeout
-3 - GSM module has answered "ERROR" string
OK ret val:
-----------
0..14 current speaker volume
**********************************************************/
/*
char GSM::DecSpeakerVolume(void)
{
char ret_val;
byte current_speaker_value;
current_speaker_value = last_speaker_volume;
if (current_speaker_value > 0) {
current_speaker_value--;
ret_val = SetSpeakerVolume(current_speaker_value);
}
else ret_val = 0;
return (ret_val);
}
*/
/**********************************************************
Method sends DTMF signal
This function only works when call is in progress
dtmf_tone: tone to send 0..15
return:
ERROR ret. val:
---------------
-1 - comm. line to the GSM module is not free
-2 - GSM module didn't answer in timeout
-3 - GSM module has answered "ERROR" string
OK ret val:
-----------
0.. tone
**********************************************************/
/*
char GSM::SendDTMFSignal(byte dtmf_tone)
{
char ret_val = -1;
if (CLS_FREE != GetCommLineStatus()) return (ret_val);
SetCommLineStatus(CLS_ATCMD);
// e.g. AT+VTS=5<CR>
_cell.print("AT+VTS=");
_cell.print((int)dtmf_tone);
_cell.print("\r");
// 1 sec. for initial comm tmout
// 50 msec. for inter character timeout
if (RX_TMOUT_ERR == WaitResp(1000, 50)) {
ret_val = -2; // ERROR
}
else {
if(IsStringReceived(str_ok)) {
ret_val = dtmf_tone; // OK
}
else ret_val = -3; // ERROR
}
SetCommLineStatus(CLS_FREE);
return (ret_val);
}
*/
/**********************************************************
Method returns state of user button
return: 0 - not pushed = released
1 - pushed
**********************************************************/
byte GSM::IsUserButtonPushed(void)
{
byte ret_val = 0;
if (CLS_FREE != GetCommLineStatus()) return(0);
SetCommLineStatus(CLS_ATCMD);
//if (AT_RESP_OK == SendATCmdWaitResp("AT#GPIO=9,2", 500, 50, "#GPIO: 0,0", 1)) {
// user button is pushed
// ret_val = 1;
//}
//else ret_val = 0;
//SetCommLineStatus(CLS_FREE);
//return (ret_val);
}
/**********************************************************
Method reads phone number string from specified SIM position
position: SMS position <1..20>
return:
ERROR ret. val:
---------------
-1 - comm. line to the GSM module is not free
-2 - GSM module didn't answer in timeout
-3 - position must be > 0
phone_number is empty string
OK ret val:
-----------
0 - there is no phone number on the position
1 - phone number was found
phone_number is filled by the phone number string finished by 0x00
so it is necessary to define string with at least
15 bytes(including also 0x00 termination character)
an example of usage:
GSM gsm;
char phone_num[20]; // array for the phone number string
if (1 == gsm.GetPhoneNumber(1, phone_num)) {
// valid phone number on SIM pos. #1
// phone number string is copied to the phone_num array
#ifdef DEBUG_PRINT
gsm.DebugPrint("DEBUG phone number: ", 0);
gsm.DebugPrint(phone_num, 1);
#endif
}
else {
// there is not valid phone number on the SIM pos.#1
#ifdef DEBUG_PRINT
gsm.DebugPrint("DEBUG there is no phone number", 1);
#endif
}
**********************************************************/
char GSM::GetPhoneNumber(byte position, char *phone_number)
{
char ret_val = -1;
char *p_char;
char *p_char1;
if (position == 0) return (-3);
if (CLS_FREE != GetCommLineStatus()) return (ret_val);
SetCommLineStatus(CLS_ATCMD);
ret_val = 0; // not found yet
phone_number[0] = 0; // phone number not found yet => empty string
//send "AT+CPBR=XY" - where XY = position
_cell.print(F("AT+CPBR="));
_cell.print((int)position);
_cell.print("\r");
// 5000 msec. for initial comm tmout
// 50 msec. for inter character timeout
switch (WaitResp(5000, 50, "+CPBR")) {
case RX_TMOUT_ERR:
// response was not received in specific time
ret_val = -2;
break;
case RX_FINISHED_STR_RECV:
// response in case valid phone number stored:
// <CR><LF>+CPBR: <index>,<number>,<type>,<text><CR><LF>
// <CR><LF>OK<CR><LF>
// response in case there is not phone number:
// <CR><LF>OK<CR><LF>
p_char = strstr((char *)(comm_buf),",\"");
if (p_char != NULL) {
p_char++;
p_char++; // we are on the first phone number character
// find out '"' as finish character of phone number string
p_char1 = strchr((char *)(p_char),'"');
if (p_char1 != NULL) {
*p_char1 = 0; // end of string
}
// extract phone number string
strcpy(phone_number, (char *)(p_char));
// output value = we have found out phone number string
ret_val = 1;
}
break;
case RX_FINISHED_STR_NOT_RECV:
// only OK or ERROR => no phone number
ret_val = 0;
break;
}
SetCommLineStatus(CLS_FREE);
return (ret_val);
}
/**********************************************************
Method writes phone number string to the specified SIM position
position: SMS position <1..20>
phone_number: phone number string for the writing
return:
ERROR ret. val:
---------------
-1 - comm. line to the GSM module is not free
-2 - GSM module didn't answer in timeout
-3 - position must be > 0
OK ret val:
-----------
0 - phone number was not written
1 - phone number was written
**********************************************************/
char GSM::WritePhoneNumber(byte position, char *phone_number)
{
char ret_val = -1;
if (position == 0) return (-3);
if (CLS_FREE != GetCommLineStatus()) return (ret_val);
SetCommLineStatus(CLS_ATCMD);
ret_val = 0; // phone number was not written yet
//send: AT+CPBW=XY,"00420123456789"
// where XY = position,
// "00420123456789" = phone number string
_cell.print(F("AT+CPBW="));
_cell.print((int)position);
_cell.print(F(",\""));
_cell.print(phone_number);
_cell.print(F("\"\r"));
// 5000 msec. for initial comm tmout
// 50 msec. for inter character timeout
switch (WaitResp(5000, 50, str_ok)) {
case RX_TMOUT_ERR:
// response was not received in specific time
break;
case RX_FINISHED_STR_RECV:
// response is OK = has been written
ret_val = 1;
break;
case RX_FINISHED_STR_NOT_RECV:
// other response: e.g. ERROR
break;
}
SetCommLineStatus(CLS_FREE);
return (ret_val);
}
/**********************************************************
Method del phone number from the specified SIM position
position: SMS position <1..20>
return:
ERROR ret. val:
---------------
-1 - comm. line to the GSM module is not free
-2 - GSM module didn't answer in timeout
-3 - position must be > 0
OK ret val:
-----------
0 - phone number was not deleted
1 - phone number was deleted
**********************************************************/
char GSM::DelPhoneNumber(byte position)
{
char ret_val = -1;
if (position == 0) return (-3);
if (CLS_FREE != GetCommLineStatus()) return (ret_val);
SetCommLineStatus(CLS_ATCMD);
ret_val = 0; // phone number was not written yet
//send: AT+CPBW=XY
// where XY = position
_cell.print(F("AT+CPBW="));
_cell.print((int)position);
_cell.print(F("\r"));
// 5000 msec. for initial comm tmout
// 50 msec. for inter character timeout
switch (WaitResp(5000, 50, str_ok)) {
case RX_TMOUT_ERR:
// response was not received in specific time
break;
case RX_FINISHED_STR_RECV:
// response is OK = has been written
ret_val = 1;
break;
case RX_FINISHED_STR_NOT_RECV:
// other response: e.g. ERROR
break;
}
SetCommLineStatus(CLS_FREE);
return (ret_val);
}
/**********************************************************
Function compares specified phone number string
with phone number stored at the specified SIM position
position: SMS position <1..20>
phone_number: phone number string which should be compare
return:
ERROR ret. val:
---------------
-1 - comm. line to the GSM module is not free
-2 - GSM module didn't answer in timeout
-3 - position must be > 0
OK ret val:
-----------
0 - phone numbers are different
1 - phone numbers are the same
an example of usage:
if (1 == gsm.ComparePhoneNumber(1, "123456789")) {
// the phone num. "123456789" is stored on the SIM pos. #1
// phone number string is copied to the phone_num array
#ifdef DEBUG_PRINT
gsm.DebugPrint("DEBUG phone numbers are the same", 1);
#endif
}
else {
#ifdef DEBUG_PRINT
gsm.DebugPrint("DEBUG phone numbers are different", 1);
#endif
}
**********************************************************/
char GSM::ComparePhoneNumber(byte position, char *phone_number)
{
char ret_val = -1;
char sim_phone_number[20];
ret_val = 0; // numbers are not the same so far
if (position == 0) return (-3);
if (1 == GetPhoneNumber(position, sim_phone_number)) {
//Serial.print("CHIAMANTE ");
//Serial.println(phone_number);
//Serial.print("SALVATO ");
//Serial.println(sim_phone_number);
// there is a valid number at the spec. SIM position
// -------------------------------------------------
if (0 == strcmp(phone_number, sim_phone_number)) {