-
Notifications
You must be signed in to change notification settings - Fork 30
/
GSM.cpp
739 lines (636 loc) · 23.5 KB
/
GSM.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
/*
This is a Beta version.
last modified 18/08/2012.
This library is based on one developed by Arduino Labs
and it is modified to preserve the compability
with the Arduino's product.
The library is modified to use the GSM Shield,
developed by www.open-electronics.org
(http://www.open-electronics.org/arduino-gsm-shield/)
and based on SIM900 chip,
with the same commands of Arduino Shield,
based on QuectelM10 chip.
*/
#include "GSM.h"
#include "WideTextFinder.h"
//De-comment this two lines below if you have the
//first version of GSM GPRS Shield
//#define _GSM_TXPIN_ 4
//#define _GSM_RXPIN_ 5
//De-comment this two lines below if you have the
//second version og GSM GPRS Shield
#define _GSM_TXPIN_ 2
#define _GSM_RXPIN_ 3
#ifdef UNO
GSM::GSM():_cell(_GSM_TXPIN_,_GSM_RXPIN_),_tf(_cell, 10),_status(IDLE)
{
};
#endif
#ifdef MEGA
GSM::GSM()
{
_cell.begin(9600);
};
#endif
int GSM::begin(long baud_rate)
{
// Set pin modes
pinMode(GSM_ON,OUTPUT);
pinMode(GSM_RESET,OUTPUT);
#ifdef UNO
if (baud_rate==115200) {
Serial.println(F("Don't use baudrate 115200 with Software Serial.\nAutomatically changed at 9600."));
baud_rate=9600;
}
#endif
int response=-1;
int cont=0;
boolean norep=true;
boolean turnedON=false;
SetCommLineStatus(CLS_ATCMD);
_cell.begin(baud_rate);
p_comm_buf = &comm_buf[0];
setStatus(IDLE);
// if no-reply we turn to turn on the module
for (cont=0; cont<3; cont++) {
if (AT_RESP_ERR_NO_RESP == SendATCmdWaitResp(str_at, 500, 100, str_ok, 5)&&!turnedON) { //check power
// there is no response => turn on the module
#ifdef DEBUG_ON
Serial.println(F("DB:NO RESP"));
#endif
// generate turn on pulse
digitalWrite(GSM_ON, HIGH);
delay(1200);
digitalWrite(GSM_ON, LOW);
delay(10000);
WaitResp(1000, 1000);
} else {
#ifdef DEBUG_ON
Serial.println(F("DB:ELSE"));
#endif
WaitResp(1000, 1000);
}
}
if (AT_RESP_OK == SendATCmdWaitResp(str_at, 500, 100, str_ok, 5)) {
#ifdef DEBUG_ON
Serial.println(F("DB:CORRECT BR"));
#endif
turnedON=true;
norep=false;
}
if (AT_RESP_ERR_DIF_RESP == SendATCmdWaitResp(str_at, 500, 100, str_ok, 5)&&!turnedON) { //check OK
#ifdef DEBUG_ON
Serial.println(F("DB:AUTO BAUD RATE"));
#endif
for (int i=0; i<8; i++) {
switch (i) {
case 0:
_cell.begin(1200);
break;
case 1:
_cell.begin(2400);
break;
case 2:
_cell.begin(4800);
break;
case 3:
_cell.begin(9600);
break;
case 4:
_cell.begin(19200);
break;
case 5:
_cell.begin(38400);
break;
case 6:
_cell.begin(57600);
break;
case 7:
_cell.begin(115200);
break;
// if nothing else matches, do the default
// default is optional
}
delay(100);
#ifdef DEBUG_PRINT
// parameter 0 - because module is off so it is not necessary
// to send finish AT<CR> here
DebugPrint(F("DEBUG: Stringa "), 0);
DebugPrint(buff, 0);
#endif
if (AT_RESP_OK == SendATCmdWaitResp(str_at, 500, 100, str_ok, 5)) {
#ifdef DEBUG_ON
Serial.println(F("DB:FOUND PREV BR"));
#endif
_cell.print("AT+IPR=");
_cell.print(baud_rate);
_cell.print("\r"); // send <CR>
delay(500);
_cell.begin(baud_rate);
delay(100);
if (AT_RESP_OK == SendATCmdWaitResp(str_at, 500, 100, str_ok, 5)) {
#ifdef DEBUG_ON
Serial.println(F("DB:OK BR"));
#endif
}
turnedON=true;
break;
}
#ifdef DEBUG_ON
Serial.println(F("DB:NO BR"));
#endif
}
// 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(norep==true&&!turnedON) {
Serial.println(F("Trying to force the baud-rate to 9600\n"));
for (int i=0; i<8; i++) {
switch (i) {
case 0:
_cell.begin(1200);
delay(1000);
Serial.println(F("1200"));
_cell.print(F("AT+IPR=9600\r"));
delay(1000);
_cell.begin(9600);
delay(1000);
SendATCmdWaitResp(str_at, 500, 100, str_ok, 5);
delay(1000);
WaitResp(1000,1000);
break;
case 1:
_cell.begin(2400);
delay(1000);
Serial.println(F("2400"));
_cell.print(F("AT+IPR=9600\r"));
delay(1000);
_cell.begin(9600);
delay(1000);
SendATCmdWaitResp(str_at, 500, 100, str_ok, 5);
delay(1000);
WaitResp(1000,1000);
break;
case 2:
_cell.begin(4800);
delay(1000);
Serial.println(F("4800"));
_cell.print(F("AT+IPR=9600\r"));
delay(1000);
_cell.begin(9600);
delay(1000);
SendATCmdWaitResp(str_at, 500, 100, str_ok, 5);
delay(1000);
WaitResp(1000,1000);
break;
case 3:
_cell.begin(9600);
delay(1000);
Serial.println(F("9600"));
_cell.print(F("AT+IPR=9600\r"));
delay(1000);
_cell.begin(9600);
delay(1000);
SendATCmdWaitResp(str_at, 500, 100, str_ok, 5);
delay(1000);
WaitResp(1000,1000);
break;
case 4:
_cell.begin(19200);
delay(1000);
Serial.println(F("19200"));
_cell.print(F("AT+IPR=9600\r"));
delay(1000);
_cell.begin(9600);
delay(1000);
SendATCmdWaitResp(str_at, 500, 100, str_ok, 5);
delay(1000);
WaitResp(1000,1000);
break;
case 5:
_cell.begin(38400);
delay(1000);
Serial.println(F("38400"));
_cell.print(F("AT+IPR=9600\r"));
delay(1000);
_cell.begin(9600);
delay(1000);
SendATCmdWaitResp(str_at, 500, 100, str_ok, 5);
delay(1000);
WaitResp(1000,1000);
break;
case 6:
_cell.begin(57600);
delay(1000);
Serial.println(F("57600"));
_cell.print(F("AT+IPR=9600\r"));
delay(1000);
_cell.begin(9600);
delay(1000);
SendATCmdWaitResp(str_at, 500, 100, str_ok, 5);
delay(1000);
WaitResp(1000,1000);
break;
case 7:
_cell.begin(115200);
delay(1000);
Serial.println(F("115200"));
_cell.print(F("AT+IPR=9600\r"));
delay(1000);
_cell.begin(9600);
delay(1000);
SendATCmdWaitResp(str_at, 500, 100, str_ok, 5);
delay(1000);
WaitResp(1000,1000);
break;
}
}
Serial.println(F("ERROR: SIM900 doesn't answer. Check power and serial pins in GSM.cpp"));
digitalWrite(GSM_ON, HIGH);
delay(1200);
digitalWrite(GSM_ON, LOW);
delay(10000);
return 0;
}
SetCommLineStatus(CLS_FREE);
if(turnedON) {
WaitResp(50, 50);
InitParam(PARAM_SET_0);
InitParam(PARAM_SET_1);//configure the module
Echo(0); //enable AT echo
setStatus(READY);
return(1);
} else {
//just to try to fix some problems with 115200 baudrate
_cell.begin(115200);
delay(1000);
_cell.print("AT+IPR=");
_cell.print(baud_rate);
_cell.print("\r"); // send <CR>
return(0);
}
}
void GSM::InitParam(byte group)
{
switch (group) {
case PARAM_SET_0:
// check comm line
//if (CLS_FREE != GetCommLineStatus()) return;
SetCommLineStatus(CLS_ATCMD);
// Reset to the factory settings
SendATCmdWaitResp(F("AT&F"), 1000, 50, str_ok, 5);
// switch off echo
SendATCmdWaitResp(F("ATE0"), 500, 50, str_ok, 5);
// setup fixed baud rate
//SendATCmdWaitResp("AT+IPR=9600", 500, 50, str_ok, 5);
// setup mode
//SendATCmdWaitResp("AT#SELINT=1", 500, 50, str_ok, 5);
// Switch ON User LED - just as signalization we are here
//SendATCmdWaitResp("AT#GPIO=8,1,1", 500, 50, str_ok, 5);
// Sets GPIO9 as an input = user button
//SendATCmdWaitResp("AT#GPIO=9,0,0", 500, 50, str_ok, 5);
// allow audio amplifier control
//SendATCmdWaitResp("AT#GPIO=5,0,2", 500, 50, str_ok, 5);
// Switch OFF User LED- just as signalization we are finished
//SendATCmdWaitResp("AT#GPIO=8,0,1", 500, 50, str_ok, 5);
SetCommLineStatus(CLS_FREE);
break;
case PARAM_SET_1:
// check comm line
//if (CLS_FREE != GetCommLineStatus()) return;
SetCommLineStatus(CLS_ATCMD);
// Request calling line identification
SendATCmdWaitResp(F("AT+CLIP=1"), 500, 50, str_ok, 5);
// Mobile Equipment Error Code
SendATCmdWaitResp(F("AT+CMEE=0"), 500, 50, str_ok, 5);
// Echo canceller enabled
//SendATCmdWaitResp("AT#SHFEC=1", 500, 50, str_ok, 5);
// Ringer tone select (0 to 32)
//SendATCmdWaitResp("AT#SRS=26,0", 500, 50, str_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, str_ok, 5);
// set the SMS mode to text
SendATCmdWaitResp(F("AT+CMGF=1"), 500, 50, str_ok, 5);
// Auto answer after first ring enabled
// auto answer is not used
//SendATCmdWaitResp("ATS0=1", 500, 50, str_ok, 5);
// select ringer path to handsfree
//SendATCmdWaitResp("AT#SRP=1", 500, 50, str_ok, 5);
// select ringer sound level
//SendATCmdWaitResp("AT+CRSL=2", 500, 50, str_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(F("AT+CPBS=\"SM\""), 1000, 50, str_ok, 5);
SendATCmdWaitResp(F("AT+CIPSHUT"), 500, 50, "SHUT OK", 5);
break;
}
}
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);
_cell.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;
}
}
WaitResp(1000, 5000);
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(const __FlashStringHelper *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);
_cell.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);
}
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);
}
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 (!_cell.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 = _cell.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 = _cell.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)
_cell.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;
}
}
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
*/
#ifdef DEBUG_ON
Serial.print("ATT: ");
Serial.println(compare_string);
Serial.print("RIC: ");
Serial.println((char *)comm_buf);
#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
*/
}
} else {
#ifdef DEBUG_ON
Serial.print(F("ATT: "));
Serial.println(compare_string);
Serial.print(F("RIC: NO STRING RCVD"));
#endif
}
return (ret_val);
}
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;
_cell.flush(); // erase rx circular buffer
}
void GSM::Echo(byte state)
{
if (state == 0 or state == 1) {
SetCommLineStatus(CLS_ATCMD);
_cell.print("ATE");
_cell.print((int)state);
_cell.print("\r");
delay(500);
SetCommLineStatus(CLS_FREE);
}
}
char GSM::InitSMSMemory(void)
{
char ret_val = -1;
if (CLS_FREE != GetCommLineStatus()) return (ret_val);
SetCommLineStatus(CLS_ATCMD);
ret_val = 0; // not initialized yet
// Disable messages about new SMS from the GSM module
SendATCmdWaitResp(F("AT+CNMI=2,0"), 1000, 50, str_ok, 2);
// send AT command to init memory for SMS in the SIM card
// response:
// +CPMS: <usedr>,<totalr>,<usedw>,<totalw>,<useds>,<totals>
if (AT_RESP_OK == SendATCmdWaitResp(F("AT+CPMS=\"SM\",\"SM\",\"SM\""), 1000, 1000, "+CPMS:", 10)) {
ret_val = 1;
} else ret_val = 0;
SetCommLineStatus(CLS_FREE);
return (ret_val);
}
int GSM::isIP(const char* cadena)
{
int i;
for (i=0; i<strlen(cadena); i++)
if (!(cadena[i]=='.' || ( cadena[i]>=48 && cadena[i] <=57)))
return 0;
return 1;
}