-
Notifications
You must be signed in to change notification settings - Fork 33
/
GSM_Shield.cpp
executable file
·2274 lines (1943 loc) · 70.2 KB
/
GSM_Shield.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
/*
GSM.cpp - library for the GSM Playground - GSM Shield for Arduino
Released under the Creative Commons Attribution-Share Alike 3.0 License
http://www.creativecommons.org/licenses/by-sa/3.0/
www.hwkitchen.com
*/
#include <Arduino.h>
#include <SoftwareSerial.h>
#include "GSM_Shield.h"
extern "C" {
#include <string.h>
}
#define rxPin 2
#define txPin 3
//SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
SoftwareSerial mySerial(2, 3); //rx, tx
#ifdef DEBUG_LED_ENABLED
int DEBUG_LED = 25; // LED connected to digital pin 25
void GSM::BlinkDebugLED (byte num_of_blink)
{
byte i;
pinMode(DEBUG_LED, OUTPUT); // sets the digital pin as output
for (i = 0; i < num_of_blink; i++) {
digitalWrite(DEBUG_LED, HIGH); // sets the LED on
delay(50);
digitalWrite(DEBUG_LED, LOW); // sets the LED off
delay(500);
}
}
#endif
#ifdef DEBUG_PRINT
/**********************************************************
Two methods print out debug information to the standard output
- it means to the serial line.
First method prints string.
Second method prints integer numbers.
Note:
=====
The serial line is connected to the GSM module and is
used for sending AT commands. There is used "trick" that GSM
module accepts not valid AT command strings because it doesn't
understand them and still waits for some valid AT command.
So after all debug strings are sent we send just AT<CR> as
a valid AT command and GSM module responds by OK. So previous
debug strings are overwritten and GSM module is not influenced
by these debug texts
string_to_print: pointer to the string to be print out
last_debug_print: 0 - this is not last debug info, we will
continue with sending... so don't send
AT<CR>(see explanation above)
1 - we are finished with sending debug info
for this time and finished AT<CR>
will be sent(see explanation above)
**********************************************************/
void GSM::DebugPrint(const char *string_to_print, byte last_debug_print)
{
if (last_debug_print) {
Serial.println(string_to_print);
SendATCmdWaitResp("AT", 500, 50, "OK", 1);
}
else Serial.print(string_to_print);
}
void GSM::DebugPrint(int number_to_print, byte last_debug_print)
{
Serial.println(number_to_print);
if (last_debug_print) {
SendATCmdWaitResp("AT", 500, 50, "OK", 1);
}
}
#endif
/**********************************************************
Method returns GSM library version
return val: 100 means library version 1.00
101 means library version 1.01
**********************************************************/
int GSM::LibVer(void)
{
return (GSM_LIB_VERSION);
}
/**********************************************************
Constructor definition
***********************************************************/
GSM::GSM(void)
{
// set some GSM pins as inputs, some as outputs
pinMode(GSM_ON, OUTPUT); // sets pin 5 as output
pinMode(GSM_RESET, OUTPUT); // sets pin 4 as output
//pinMode(DTMF_OUTPUT_ENABLE, OUTPUT); // sets pin 2 as output
// deactivation of IC8 so DTMF is disabled by default
//digitalWrite(DTMF_OUTPUT_ENABLE, LOW);
// not registered yet
module_status = STATUS_NONE;
// initialization of speaker volume
last_speaker_volume = 0;
}
/**********************************************************
Initialization of GSM module serial line
**********************************************************/
/*
void GSM::InitSerLine(long baud_rate)
{
// open the Serial line for the communication
mySerial.begin(baud_rate);
//SendATCmdWaitResp("AT+IPR=9600", 500, 50, "OK", 5);
for (int i=1;i<7;i++){
switch (i) {
case 1:
mySerial.begin(4800);
break;
case 2:
mySerial.begin(9600);
break;
case 3:
mySerial.begin(19200);
break;
case 4:
mySerial.begin(38400);
break;
case 5:
mySerial.begin(57600);
break;
case 6:
mySerial.begin(115200);
break;
// if nothing else matches, do the default
// default is optional
}
mySerial.write("AT+IPR=");
mySerial.write(baud_rate);
mySerial.write("\r"); // send <CR>
}
// communication line is not used yet = free
SetCommLineStatus(CLS_FREE);
// pointer is initialized to the first item of comm. buffer
p_comm_buf = &comm_buf[0];
}
*/
/**********************************************************
Initializes receiving process
start_comm_tmout - maximum waiting time for receiving the first response
character (in msec.)
max_interchar_tmout - maximum tmout between incoming characters
in msec.
if there is no other incoming character longer then specified
tmout(in msec) receiving process is considered as finished
**********************************************************/
void GSM::RxInit(uint16_t start_comm_tmout, uint16_t max_interchar_tmout)
{
rx_state = RX_NOT_STARTED;
start_reception_tmout = start_comm_tmout;
interchar_tmout = max_interchar_tmout;
prev_time = millis();
comm_buf[0] = 0x00; // end of string
p_comm_buf = &comm_buf[0];
comm_buf_len = 0;
mySerial.flush(); // erase rx circular buffer
}
/**********************************************************
Method checks if receiving process is finished or not.
Rx process is finished if defined inter-character tmout is reached
returns:
RX_NOT_FINISHED = 0,// not finished yet
RX_FINISHED, // finished - inter-character tmout occurred
RX_TMOUT_ERR, // initial communication tmout occurred
**********************************************************/
byte GSM::IsRxFinished(void)
{
byte num_of_bytes;
byte ret_val = RX_NOT_FINISHED; // default not finished
// Rx state machine
// ----------------
if (rx_state == RX_NOT_STARTED) {
// Reception is not started yet - check tmout
if (!mySerial.available()) {
// still no character received => check timeout
/*
#ifdef DEBUG_GSMRX
DebugPrint("\r\nDEBUG: reception timeout", 0);
Serial.print((unsigned long)(millis() - prev_time));
DebugPrint("\r\nDEBUG: start_reception_tmout\r\n", 0);
Serial.print(start_reception_tmout);
#endif
*/
if ((unsigned long)(millis() - prev_time) >= start_reception_tmout) {
// timeout elapsed => GSM module didn't start with response
// so communication is takes as finished
/*
#ifdef DEBUG_GSMRX
DebugPrint("\r\nDEBUG: RECEPTION TIMEOUT", 0);
#endif
*/
comm_buf[comm_buf_len] = 0x00;
ret_val = RX_TMOUT_ERR;
}
}
else {
// at least one character received => so init inter-character
// counting process again and go to the next state
prev_time = millis(); // init tmout for inter-character space
rx_state = RX_ALREADY_STARTED;
}
}
if (rx_state == RX_ALREADY_STARTED) {
// Reception already started
// check new received bytes
// only in case we have place in the buffer
num_of_bytes = mySerial.available();
// if there are some received bytes postpone the timeout
if (num_of_bytes) prev_time = millis();
// read all received bytes
while (num_of_bytes) {
num_of_bytes--;
if (comm_buf_len < COMM_BUF_LEN) {
// we have still place in the GSM internal comm. buffer =>
// move available bytes from circular buffer
// to the rx buffer
*p_comm_buf = mySerial.read();
p_comm_buf++;
comm_buf_len++;
comm_buf[comm_buf_len] = 0x00; // and finish currently received characters
// so after each character we have
// valid string finished by the 0x00
}
else {
// comm buffer is full, other incoming characters
// will be discarded
// but despite of we have no place for other characters
// we still must to wait until
// inter-character tmout is reached
// so just readout character from circular RS232 buffer
// to find out when communication id finished(no more characters
// are received in inter-char timeout)
mySerial.read();
}
}
// finally check the inter-character timeout
/*
#ifdef DEBUG_GSMRX
DebugPrint("\r\nDEBUG: intercharacter", 0);
Serial.print((unsigned long)(millis() - prev_time));
DebugPrint("\r\nDEBUG: interchar_tmout\r\n", 0);
Serial.print(interchar_tmout);
#endif
*/
if ((unsigned long)(millis() - prev_time) >= interchar_tmout) {
// timeout between received character was reached
// reception is finished
// ---------------------------------------------
/*
#ifdef DEBUG_GSMRX
DebugPrint("\r\nDEBUG: OVER INTER TIMEOUT", 0);
#endif
*/
comm_buf[comm_buf_len] = 0x00; // for sure finish string again
// but it is not necessary
ret_val = RX_FINISHED;
}
}
#ifdef DEBUG_GSMRX
if (ret_val == RX_FINISHED){
DebugPrint("DEBUG: Received string\r\n", 0);
for (int i=0; i<comm_buf_len; i++){
Serial.print(byte(comm_buf[i]));
}
}
#endif
return (ret_val);
}
/**********************************************************
Method checks received bytes
compare_string - pointer to the string which should be find
return: 0 - string was NOT received
1 - string was received
**********************************************************/
byte GSM::IsStringReceived(char const *compare_string)
{
char *ch;
byte ret_val = 0;
if(comm_buf_len) {
/*
#ifdef DEBUG_GSMRX
DebugPrint("DEBUG: Compare the string: \r\n", 0);
for (int i=0; i<comm_buf_len; i++){
Serial.print(byte(comm_buf[i]));
}
DebugPrint("\r\nDEBUG: with the string: \r\n", 0);
Serial.print(compare_string);
DebugPrint("\r\n", 0);
#endif
*/
ch = strstr((char *)comm_buf, compare_string);
if (ch != NULL) {
ret_val = 1;
/*#ifdef DEBUG_PRINT
DebugPrint("\r\nDEBUG: expected string was received\r\n", 0);
#endif
*/
}
else
{
/*#ifdef DEBUG_PRINT
DebugPrint("\r\nDEBUG: expected string was NOT received\r\n", 0);
#endif
*/
}
}
return (ret_val);
}
/**********************************************************
Method waits for response
start_comm_tmout - maximum waiting time for receiving the first response
character (in msec.)
max_interchar_tmout - maximum tmout between incoming characters
in msec.
return:
RX_FINISHED finished, some character was received
RX_TMOUT_ERR finished, no character received
initial communication tmout occurred
**********************************************************/
byte GSM::WaitResp(uint16_t start_comm_tmout, uint16_t max_interchar_tmout)
{
byte status;
RxInit(start_comm_tmout, max_interchar_tmout);
// wait until response is not finished
do {
status = IsRxFinished();
} while (status == RX_NOT_FINISHED);
return (status);
}
/**********************************************************
Method waits for response with specific response string
start_comm_tmout - maximum waiting time for receiving the first response
character (in msec.)
max_interchar_tmout - maximum tmout between incoming characters
in msec.
expected_resp_string - expected string
return:
RX_FINISHED_STR_RECV, finished and expected string received
RX_FINISHED_STR_NOT_RECV finished, but expected string not received
RX_TMOUT_ERR finished, no character received
initial communication tmout occurred
**********************************************************/
byte GSM::WaitResp(uint16_t start_comm_tmout, uint16_t max_interchar_tmout,
char const *expected_resp_string)
{
byte status;
byte ret_val;
RxInit(start_comm_tmout, max_interchar_tmout);
// wait until response is not finished
do {
status = IsRxFinished();
} while (status == RX_NOT_FINISHED);
if (status == RX_FINISHED) {
// something was received but what was received?
// ---------------------------------------------
if(IsStringReceived(expected_resp_string)) {
// expected string was received
// ----------------------------
ret_val = RX_FINISHED_STR_RECV;
}
else ret_val = RX_FINISHED_STR_NOT_RECV;
}
else {
// nothing was received
// --------------------
ret_val = RX_TMOUT_ERR;
}
return (ret_val);
}
/**********************************************************
Method sends AT command and waits for response
return:
AT_RESP_ERR_NO_RESP = -1, // no response received
AT_RESP_ERR_DIF_RESP = 0, // response_string is different from the response
AT_RESP_OK = 1, // response_string was included in the response
**********************************************************/
char GSM::SendATCmdWaitResp(char const *AT_cmd_string,
uint16_t start_comm_tmout, uint16_t max_interchar_tmout,
char const *response_string,
byte no_of_attempts)
{
byte status;
char ret_val = AT_RESP_ERR_NO_RESP;
byte i;
for (i = 0; i < no_of_attempts; i++) {
// delay 500 msec. before sending next repeated AT command
// so if we have no_of_attempts=1 tmout will not occurred
if (i > 0) delay(500);
mySerial.println(AT_cmd_string);
status = WaitResp(start_comm_tmout, max_interchar_tmout);
if (status == RX_FINISHED) {
// something was received but what was received?
// ---------------------------------------------
if(IsStringReceived(response_string)) {
ret_val = AT_RESP_OK;
break; // response is OK => finish
}
else ret_val = AT_RESP_ERR_DIF_RESP;
}
else {
// nothing was received
// --------------------
ret_val = AT_RESP_ERR_NO_RESP;
}
}
return (ret_val);
}
/**********************************************************
Methods return the state of corresponding
bits in the status variable
- these methods do not communicate with the GSM module
return values:
0 - not true (not active)
>0 - true (active)
**********************************************************/
byte GSM::IsRegistered(void)
{
return (module_status & STATUS_REGISTERED);
}
byte GSM::IsInitialized(void)
{
return (module_status & STATUS_INITIALIZED);
}
/**********************************************************
Checks if the GSM module is responding
to the AT command
- if YES nothing is made
- if NO GSM module is turned on
**********************************************************/
void GSM::TurnOn(long baud_rate)
{
SetCommLineStatus(CLS_ATCMD);
mySerial.begin(baud_rate);
#ifdef DEBUG_PRINT
// parameter 0 - because module is off so it is not necessary
// to send finish AT<CR> here
DebugPrint("DEBUG: baud ", 0);
DebugPrint(baud_rate, 0);
#endif
if (AT_RESP_ERR_NO_RESP == SendATCmdWaitResp("AT", 500, 100, "OK", 5)) { //check power
// there is no response => turn on the module
#ifdef DEBUG_PRINT
// parameter 0 - because module is off so it is not necessary
// to send finish AT<CR> here
DebugPrint("DEBUG: GSM module is off\r\n", 0);
DebugPrint("DEBUG: start the module\r\n", 0);
#endif
// generate turn on pulse
digitalWrite(GSM_ON, HIGH);
delay(1200);
digitalWrite(GSM_ON, LOW);
delay(5000);
}
else
{
#ifdef DEBUG_PRINT
// parameter 0 - because module is off so it is not necessary
// to send finish AT<CR> here
DebugPrint("DEBUG: GSM module is on\r\n", 0);
#endif
}
if (AT_RESP_ERR_DIF_RESP == SendATCmdWaitResp("AT", 500, 100, "OK", 5)) { //check OK
#ifdef DEBUG_PRINT
// parameter 0 - because module is off so it is not necessary
// to send finish AT<CR> here
DebugPrint("DEBUG: the baud is not ok\r\n", 0);
#endif
//SendATCmdWaitResp("AT+IPR=9600", 500, 50, "OK", 5);
for (int i=1;i<7;i++){
switch (i) {
case 1:
mySerial.begin(4800);
#ifdef DEBUG_PRINT
DebugPrint("DEBUG: provo Baud 4800\r\n", 0);
#endif
break;
case 2:
mySerial.begin(9600);
#ifdef DEBUG_PRINT
DebugPrint("DEBUG: provo Baud 9600\r\n", 0);
#endif
break;
case 3:
mySerial.begin(19200);
#ifdef DEBUG_PRINT
DebugPrint("DEBUG: provo Baud 19200\r\n", 0);
#endif
break;
case 4:
mySerial.begin(38400);
#ifdef DEBUG_PRINT
DebugPrint("DEBUG: provo Baud 38400\r\n", 0);
#endif
break;
case 5:
mySerial.begin(57600);
#ifdef DEBUG_PRINT
DebugPrint("DEBUG: provo Baud 57600\r\n", 0);
#endif
break;
case 6:
mySerial.begin(115200);
#ifdef DEBUG_PRINT
DebugPrint("DEBUG: provo Baud 115200\r\n", 0);
#endif
break;
// if nothing else matches, do the default
// default is optional
}
/*
p_char = strchr((char *)(comm_buf),',');
p_char1 = p_char+2; // we are on the first phone number character
p_char = strchr((char *)(p_char1),'"');
if (p_char != NULL) {
*p_char = 0; // end of string
strcpy(phone_number, (char *)(p_char1));
}
*/
delay(100);
/*sprintf (buff,"AT+IPR=%f",baud_rate);
#ifdef DEBUG_PRINT
// parameter 0 - because module is off so it is not necessary
// to send finish AT<CR> here
DebugPrint("DEBUG: Stringa ", 0);
DebugPrint(buff, 0);
#endif
*/
mySerial.write("AT+IPR=");
mySerial.write(baud_rate);
mySerial.write("\r"); // send <CR>
delay(500);
mySerial.begin(baud_rate);
delay(100);
if (AT_RESP_OK == SendATCmdWaitResp("AT", 500, 100, "OK", 5)){
#ifdef DEBUG_PRINT
// parameter 0 - because module is off so it is not necessary
// to send finish AT<CR> here
DebugPrint("DEBUG: ricevuto ok da modulo, baud impostato: ", 0);
DebugPrint(baud_rate, 0);
#endif
break;
}
}
// communication line is not used yet = free
SetCommLineStatus(CLS_FREE);
// pointer is initialized to the first item of comm. buffer
p_comm_buf = &comm_buf[0];
if (AT_RESP_ERR_NO_RESP == SendATCmdWaitResp("AT", 500, 50, "OK", 5)) {
#ifdef DEBUG_PRINT
// parameter 0 - because module is off so it is not necessary
// to send finish AT<CR> here
DebugPrint("DEBUG: No answer from the module\r\n", 0);
#endif
}
else{
#ifdef DEBUG_PRINT
// parameter 0 - because module is off so it is not necessary
// to send finish AT<CR> here
DebugPrint("DEBUG: 1 baud ok\r\n", 0);
#endif
}
}
else
{
#ifdef DEBUG_PRINT
DebugPrint("DEBUG: 2 GSM module is on and baud is ok\r\n", 0);
#endif
}
SetCommLineStatus(CLS_FREE);
// send collection of first initialization parameters for the GSM module
InitParam(PARAM_SET_0);
}
/**********************************************************
Sends parameters for initialization of GSM module
group: 0 - parameters of group 0 - not necessary to be registered in the GSM
1 - parameters of group 1 - it is necessary to be registered
**********************************************************/
void GSM::InitParam(byte group)
{
switch (group) {
case PARAM_SET_0:
// check comm line
if (CLS_FREE != GetCommLineStatus()) return;
#ifdef DEBUG_PRINT
DebugPrint("DEBUG: configure the module PARAM_SET_0\r\n", 0);
#endif
SetCommLineStatus(CLS_ATCMD);
// Reset to the factory settings
SendATCmdWaitResp("AT&F", 1000, 50, "OK", 5);
// switch off echo
//SendATCmdWaitResp("ATE0", 500, 50, "OK", 5);
// setup fixed baud rate
//SendATCmdWaitResp("AT+IPR=9600", 500, 50, "OK", 5);
// setup mode
//SendATCmdWaitResp("AT#SELINT=1", 500, 50, "OK", 5);
// Switch ON User LED - just as signalization we are here
//SendATCmdWaitResp("AT#GPIO=8,1,1", 500, 50, "OK", 5);
// Sets GPIO9 as an input = user button
//SendATCmdWaitResp("AT#GPIO=9,0,0", 500, 50, "OK", 5);
// allow audio amplifier control
//SendATCmdWaitResp("AT#GPIO=5,0,2", 500, 50, "OK", 5);
// Switch OFF User LED- just as signalization we are finished
//SendATCmdWaitResp("AT#GPIO=8,0,1", 500, 50, "OK", 5);
SetCommLineStatus(CLS_FREE);
break;
case PARAM_SET_1:
// check comm line
if (CLS_FREE != GetCommLineStatus()) return;
#ifdef DEBUG_PRINT
DebugPrint("DEBUG: configure the module PARAM_SET_1\r\n", 0);
#endif
SetCommLineStatus(CLS_ATCMD);
// Request calling line identification
SendATCmdWaitResp("AT+CLIP=1", 500, 50, "OK", 5);
// Mobile Equipment Error Code
SendATCmdWaitResp("AT+CMEE=0", 500, 50, "OK", 5);
// Echo canceller enabled
//SendATCmdWaitResp("AT#SHFEC=1", 500, 50, "OK", 5);
// Ringer tone select (0 to 32)
//SendATCmdWaitResp("AT#SRS=26,0", 500, 50, "OK", 5);
// Microphone gain (0 to 7) - response here sometimes takes
// more than 500msec. so 1000msec. is more safety
//SendATCmdWaitResp("AT#HFMICG=7", 1000, 50, "OK", 5);
// set the SMS mode to text
SendATCmdWaitResp("AT+CMGF=1", 500, 50, "OK", 5);
// Auto answer after first ring enabled
// auto answer is not used
//SendATCmdWaitResp("ATS0=1", 500, 50, "OK", 5);
// select ringer path to handsfree
//SendATCmdWaitResp("AT#SRP=1", 500, 50, "OK", 5);
// select ringer sound level
//SendATCmdWaitResp("AT+CRSL=2", 500, 50, "OK", 5);
// we must release comm line because SetSpeakerVolume()
// checks comm line if it is free
SetCommLineStatus(CLS_FREE);
// select speaker volume (0 to 14)
//SetSpeakerVolume(9);
// init SMS storage
InitSMSMemory();
// select phonebook memory storage
SendATCmdWaitResp("AT+CPBS=\"SM\"", 1000, 50, "OK", 5);
break;
}
}
/**********************************************************
Enables DTMF receiver = IC8 device
**********************************************************/
/*void GSM::EnableDTMF(void)
{
// configuration of corresponding DTMF pins
pinMode(DTMF_DATA_VALID, INPUT); // sets pin 3 as input
pinMode(DTMF_DATA0, INPUT); // sets pin 6 as input
pinMode(DTMF_DATA1, INPUT); // sets pin 7 as input
pinMode(DTMF_DATA2, INPUT); // sets pin 8 as input
pinMode(DTMF_DATA3, INPUT); // sets pin 9 as input
// activation of IC8
digitalWrite(DTMF_OUTPUT_ENABLE, HIGH);
}
*/
/**********************************************************
Checks if DTMF signal is valid
return: >= 0x10hex(16dec) - DTMF signal is NOT valid
0..15 - valid DTMF signal
**********************************************************/
/*byte GSM::GetDTMFSignal(void)
{
static byte last_state = LOW; // initialization
byte ret_val = 0xff; // default - not valid
if (digitalRead(DTMF_DATA_VALID)==HIGH
&& last_state == LOW) {
// valid DTMF signal => decode it
// ------------------------------
last_state = HIGH; // remember last state
ret_val = 0;
if (digitalRead(DTMF_DATA0)) ret_val |= 0x01;
if (digitalRead(DTMF_DATA1)) ret_val |= 0x02;
if (digitalRead(DTMF_DATA2)) ret_val |= 0x04;
if (digitalRead(DTMF_DATA3)) ret_val |= 0x08;
// confirm DTMF signal by the tone 5
// ---------------------------------
SendDTMFSignal(5);
}
else if (digitalRead(DTMF_DATA_VALID) == LOW) {
// pulse is not valid => enable to check DTMF signal again
// -------------------------------------------------------
last_state = LOW;
}
return (ret_val);
}
*/
/**********************************************************
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);
}
/**********************************************************
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);
mySerial.println("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 checks status of call
return:
CALL_NONE - no call activity
CALL_INCOM_VOICE - incoming voice
CALL_ACTIVE_VOICE - active voice
CALL_NO_RESPONSE - no response to the AT command
CALL_COMM_LINE_BUSY - comm line is not free
**********************************************************/
byte GSM::CallStatus(void)
{
byte ret_val = CALL_NONE;
if (CLS_FREE != GetCommLineStatus()) return (CALL_COMM_LINE_BUSY);
SetCommLineStatus(CLS_ATCMD);
mySerial.println("AT+CPAS");
// 5 sec. for initial comm tmout
// 50 msec. for inter character timeout
if (RX_TMOUT_ERR == WaitResp(5000, 50)) {
// nothing was received (RX_TMOUT_ERR)
// -----------------------------------
ret_val = CALL_NO_RESPONSE;
}
else {
// something was received but what was received?
// ---------------------------------------------
// ready (device allows commands from TA/TE)
// <CR><LF>+CPAS: 0<CR><LF> <CR><LF>OK<CR><LF>
// unavailable (device does not allow commands from TA/TE)
// <CR><LF>+CPAS: 1<CR><LF> <CR><LF>OK<CR><LF>
// unknown (device is not guaranteed to respond to instructions)
// <CR><LF>+CPAS: 2<CR><LF> <CR><LF>OK<CR><LF> - NO CALL
// ringing
// <CR><LF>+CPAS: 3<CR><LF> <CR><LF>OK<CR><LF> - NO CALL
// call in progress
// <CR><LF>+CPAS: 4<CR><LF> <CR><LF>OK<CR><LF> - NO CALL
if(IsStringReceived("0")) {
// ready - there is no call
// ------------------------
ret_val = CALL_NONE;
}
else if(IsStringReceived("3")) {
// incoming call
// --------------
ret_val = CALL_INCOM_VOICE;
}
else if(IsStringReceived("4")) {
// active call
// -----------
ret_val = CALL_ACTIVE_VOICE;
}
}
SetCommLineStatus(CLS_FREE);
return (ret_val);
}
/**********************************************************
Method checks status of call(incoming or active)
and makes authorization with specified SIM positions range
phone_number: a pointer where the tel. number string of current call will be placed
so the space for the phone number string must be reserved - see example
first_authorized_pos: initial SIM phonebook position where the authorization process
starts
last_authorized_pos: last SIM phonebook position where the authorization process
finishes
Note(important):
================
In case first_authorized_pos=0 and also last_authorized_pos=0
the received incoming phone number is NOT authorized at all, so every
incoming is considered as authorized (CALL_INCOM_VOICE_NOT_AUTH is returned)
return:
CALL_NONE - no call activity
CALL_INCOM_VOICE_AUTH - incoming voice - authorized
CALL_INCOM_VOICE_NOT_AUTH - incoming voice - not authorized
CALL_ACTIVE_VOICE - active voice
CALL_INCOM_DATA_AUTH - incoming data call - authorized
CALL_INCOM_DATA_NOT_AUTH - incoming data call - not authorized
CALL_ACTIVE_DATA - active data call
CALL_NO_RESPONSE - no response to the AT command
CALL_COMM_LINE_BUSY - comm line is not free
**********************************************************/
byte GSM::CallStatusWithAuth(char *phone_number,
byte first_authorized_pos, byte last_authorized_pos)
{
byte ret_val = CALL_NONE;
byte search_phone_num = 0;
byte i;
byte status;
char *p_char;
char *p_char1;
phone_number[0] = 0x00; // no phonr number so far
if (CLS_FREE != GetCommLineStatus()) return (CALL_COMM_LINE_BUSY);
SetCommLineStatus(CLS_ATCMD);
mySerial.println("AT+CLCC");
// 5 sec. for initial comm tmout
// and max. 1500 msec. for inter character timeout
RxInit(5000, 1500);
// wait response is finished
do {
if (IsStringReceived("OK\r\n")) {
// perfect - we have some response, but what:
// there is either NO call:
// <CR><LF>OK<CR><LF>
// or there is at least 1 call