forked from riotnetwork/GSM-shield
-
Notifications
You must be signed in to change notification settings - Fork 5
/
uBlox.cpp
1451 lines (1234 loc) · 38.9 KB
/
uBlox.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
/*
This is a Beta version.
last modified 17/01/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 "uBlox.h"
#include "Streaming.h"
#define _GSM_CONNECTION_TOUT_ 5
#define _TCP_CONNECTION_TOUT_ 20
#define _GSM_DATA_TOUT_ 10
uBlox gsm;
uBlox::uBlox(){};
uBlox::~uBlox(){};
int uBlox::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.
_cell << F("AT+CGREG?") << crl;
//Se espera la unsolicited response de registered to network.
while (_tf.find("+CGREG: 0,"))
{
connCode=_tf.getValue();
if((connCode==1)||(connCode==5)) // 1: registered in home network, 5: regestered as roaming
{
setStatus(READY);
_cell << F("AT+CMGF=1") << crl; //SMS text mode.
delay(200);
// Buah, we should take this to readCall()
_cell << F("AT+CLIP=1") << crl; //SMS text mode.
delay(200);
//_cell << "AT+QIDEACT" << _DEC(cr) << endl; //To make sure not pending connection.
//delay(1000);
return 1;
}
}
}
return 0;
}
int uBlox::softReset()
{
#ifdef DEBUG_ON
Serial.println(F("DB:SRESET"));
#endif
SendATCmdWaitResp(F("AT+CFUN=16"), 5000, 100, "OK", 1);
_cell.end();
}
int uBlox::poweroff( )
{
#ifdef DEBUG_ON
Serial.println(F("DB:POWEROFF"));
#endif
SendATCmdWaitResp(F("AT+CPWROFF"), 2000, 100, "OK", 1);
}
int uBlox::sendSMS(const char* to, const char* msg)
{
//Status = READY or ATTACHED.
/*
if((getStatus() != READY)&&(getStatus() != ATTACHED))
return 0;
*/
_tf.setTimeout(_GSM_DATA_TOUT_); //Timeout for expecting modem responses.
//_cell.flush();
//AT command to send a SMS. Destination telephone number
_cell << F("AT+CMGS=\"") << to << "\"" << _DEC(cr) << endl; // Establecemos el destinatario
//Expect for ">" character.
switch(WaitResp(5000, 50, ">")){
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
//SMS text.
_cell << msg << _DEC(ctrlz) << _DEC(cr) << "\"\r";
if (!_tf.find("OK"))
return 0;
return 1;
}
int uBlox::attachGPRS(int profile, char* domain, char* dom1, char* dom2)
{
int i=0;
if(profile>6)
{
return 0;
}
delay(200);
_tf.setTimeout(_GSM_DATA_TOUT_); // Timeout for expecting modem responses.
_cell << F("AT+CGATT?\r"); // check GPRS ATTACH?
if(WaitResp(5000, 50, "1")!=RX_FINISHED_STR_RECV){ // profile is active, now we want to close it and start a new one
#ifdef DEBUG_ON
Serial.println(F("DB: !GPRS"));
#endif
_cell << F("AT+CGATT=1\r");
delay(5000);
}
_cell << F("AT+UPSND=")<<profile<<",8\r"; // check GPRS connection profile 0 is active?
if(WaitResp(5000, 50, ",8,0")!=RX_FINISHED_STR_RECV)
{ // profile is active, now we want to close it and start a new one
#ifdef DEBUG_ON
Serial<<F("DB:GPRS ")<<profile<<F(" ACTIVE\r");
#endif
return 1;
}
else {
#ifdef DEBUG_ON
Serial.println(F("DB:STARTING NEW CONNECTION"));
#endif
delay(200);
_cell <<F( "AT+UPSD=")<<profile<<",1,\"" << domain << "\"\r"; // APN name
if( WaitResp(2000, 50, "OK")==RX_FINISHED_STR_RECV)
{
_cell << F("AT+UPSD=")<<profile<<",2,\""<< dom1 << "\"\r";
if( WaitResp(2000, 50, "OK")==RX_FINISHED_STR_RECV)
{
_cell << F("AT+UPSD=")<<profile<<",3,\"" << dom2 << "\"\r";
if( WaitResp(2000, 50, "OK")==RX_FINISHED_STR_RECV)
{
_cell << F("AT+UPSD=")<<profile<<F(",7,\"0.0.0.0\"\r"); // dynamic IP assignment
if( WaitResp(2000, 50, "OK")==RX_FINISHED_STR_RECV)
{
_cell << F("AT+UPSDA=")<<profile<<",3\r"; // activate GPRS connection
if( WaitResp(4000, 50, "OK")==RX_FINISHED_STR_RECV)
{
_cell << F("AT+UPSND=")<<profile<<",8\r"; // check GPRS connection profile 0 is active?
if(WaitResp(5000, 50, ",8,1")==RX_FINISHED_STR_RECV){ // profile is active
#ifdef DEBUG_ON
Serial.println(F("DB:APN OK"));
#endif
return 1;
}
}else{} //APN activate gprs connection
}else{} //APN DHCP
}else{} //APN password
}else{} //APN username
}else{} //APN name
return 0;
}
}
int uBlox::detachGPRS(int profile)
{
if (getStatus()==IDLE) return 0;
#ifdef DEBUG_ON
Serial.println(F("DB:DETACH GPRS"));
#endif
_tf.setTimeout(_GSM_CONNECTION_TOUT_);
_cell <<F( "AT+UPSDA=")<<profile<<",4\r"; // deactivate GPRS connection,
WaitResp(500, 50, "OK");
//GPRS dettachment.
_cell << F("AT+CGATT=0\r");
if(!_tf.find("OK"))
{
setStatus(ERROR);
return 0;
}
delay(500);
setStatus(READY);
return 1;
}
int uBlox::createTCPsocket() // create a socket to be used for subsequent connections
{
_tf.setTimeout(_TCP_CONNECTION_TOUT_);
_cell <<F( "AT+USOCR=6\r"); // create a TCP socket, will return with socketID /r/n OK
delay(100);
int socketID =-1;
//int firstno=_tf.getValue(); // only because echo is turned off
socketID = _tf.getValue();
if(socketID >= 0 && socketID<=6)// valid Socket created
{
#ifdef DEBUG_ON
Serial.print(F("DB:SOCKET CREATED : "));
Serial.println(socketID);
#endif
return socketID;
}
#ifdef DEBUG_ON
Serial.println(F("DB:SOCKET ERROR"));
#endif
return -1;
}
/*
char uBlox::DNSlookup(char *server) // Performs a DNS lookup to get an IP adress for a given host identifier
{
char serverIP[15];
if(isIP(server)) // we already have an IP adress
{
return server;
}
_tf.setTimeout(_TCP_CONNECTION_TOUT_);
_cell << "AT+UDNSRN=0,\"" << server << "\"" << "\r"; //will respond with the IP address of the domain name, will reply : \rvvv.www.xxx.yyy\rOK
_tf.getString("+UDNSRN: \"", "\"", serverIP, 15);
return serverIP;
}
*/
int uBlox::ip( char* ip)
{
#ifdef DEBUG_ON
Serial.println(F("DB:IP LOOKUP"));
#endif
_cell << F("AT+UPSND=0,0\r");
if(_tf.find("+UPSND: 0"))
{
_tf.getString(",0,\"", "\"", ip, 15);
}
else
{
#ifdef DEBUG_ON
Serial.println(F("DB:IP FAIL"));
#endif
return 0;
}
if(isIP(ip))
{
return 1;
}
ip = "0.0.0.0";
return 0;
}
int uBlox::connectTCP(const char* server, int port, int id) // **** page 13/51 uBlox AT command examples
{
_tf.setTimeout(_TCP_CONNECTION_TOUT_);
/*
create TCP socket : createTCPsocket(); will return with socket identifier
get IP of host from DNS lookup ( of the socket we created) DNSlookup(server); will return the IP adress
connect to remote host ( of the socket we created)
*/
#ifdef DEBUG_ON
Serial.println(F("DB:TCP CONNECTION"));
#endif
id = createTCPsocket(); // if this is -1, we do not have a socket ID
//Visit the remote TCP server.
if(id>=0 && id <=6)
{
delay(200);
_cell <<F( "AT+USOCO=") << id <<",\"" << server << "\"," << port << "\r"; // will return OK if ok, else will return ERROR/r/n/r/n+UUSOCL: # ( socket # closed)
// Serial <<F( "AT+USOCO=") << id <<",\"" << server << "\"," << port << "\r"; // will return OK if ok, else will return ERROR/r/n/r/n+UUSOCL: # ( socket # closed)
switch(WaitResp(20000, 200, "OK")){
case RX_TMOUT_ERR:
#ifdef DEBUG_ON
Serial.println(F("DB:TCP TIMEOUT"));
#endif
_cell <<F( "AT+USOCL=")<<id<<crl;
detachGPRS(0);// detach profile 0
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
_cell <<F( "AT+USOCL=")<<id<<crl;
detachGPRS(0);// detach profile 0
#ifdef DEBUG_ON
Serial.println(F("DB:TCP NO OK"));
#endif
return 0;
break;
}
delay(20);
_cell << F("AT+USODL=")<< id <<"\r"; // go into direct datalink mode
switch(WaitResp(5000, 200, "CONNECT"))
{
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
#ifdef DEBUG_ON
Serial.println(F("DB:CONNECT"));
#endif
//delay(20);
return 1; // we are connected to a server via TCP
}
return 0;
}
int uBlox::disconnectTCP(int id)
{
//Status = TCPCONNECTEDCLIENT or TCPCONNECTEDSERVER.
/*
if ((getStatus()!=TCPCONNECTEDCLIENT)&&(getStatus()!=TCPCONNECTEDSERVER))
return 0;
*/
#ifdef DEBUG_ON
Serial.println(F("DB:TCP DISCONNECT"));
#endif
_tf.setTimeout(_GSM_CONNECTION_TOUT_);
//Switch to AT mode.
delay(2000); // wait 2 seconds after any data sent before we send the termination string to return to AT mode
_cell.flush();
_cell << "+++";
delay(1000); // wait 1 second ( no data is allowed here otherwise we cannot exit data direct link mode
//detachGPRS(0);// detach profile 0
if(WaitResp(5000, 200, "DISCONNECT")==RX_FINISHED_STR_RECV)
{
#ifdef DEBUG_ON
Serial.println(F("DB:DISCONNECT"));
#endif
// delay(500);
}
_cell<<"AT+USOCL=0\r";
switch(WaitResp(5000, 200, "OK"))
{
case RX_FINISHED_STR_RECV:
return 1;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}
return 1;
}
boolean uBlox::connectedClient(int id)
{
/*
if (getStatus()!=TCPSERVERWAIT)
return 0;
*/
_cell << F("AT+USOCTL=")<< id << ",10\r";
// Alternative: AT+QISTAT, although it may be necessary to call an AT
// command every second,which is not wise
/*
switch(WaitResp(1000, 200, "OK")){
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}*/
_tf.setTimeout(1);
if(_tf.find("4")) // response 4 means socket connection is established page 21/51
{
setStatus(TCPCONNECTEDSERVER);
return true;
}
else
return false;
}
int uBlox::read(char* result, int resultlength)
{
// Or maybe do it with AT+QIRD
int charget;
_tf.setTimeout(3);
// Not well. This way we read whatever comes in one second. If a CLOSED
// comes, we have spent a lot of time
//charget=_tf.getString("",'\0',result, resultlength);
charget=_tf.getString("","",result, resultlength);
/*if(strtok(result, "CLOSED")) // whatever chain the Q10 returns...
{
// TODO: use strtok to delete from the chain everything from CLOSED
if(getStatus()==TCPCONNECTEDCLIENT)
setStatus(ATTACHED);
else
setStatus(TCPSERVERWAIT);
} */
return charget;
}
int uBlox::signal(int param)
{
#ifdef DEBUG_ON
Serial.println(F("DB:SIGNAL"));
#endif
int rssi,ber,value;
_cell << "AT+CSQ\r";
if(_tf.find("+CSQ"))
{
rssi=_tf.getValue();
ber=_tf.getValue();
}
else
{
#ifdef DEBUG_ON
Serial.println(F("DB:SIGNAL FAIL"));
#endif
}
switch(param)
{
case 0:
value = rssi;
break;
case 1:
value = ber;
break;
}
return value;
}
int uBlox::readCellData(int &mcc, int &mnc, long &lac, long &cellid)
{
if (getStatus()==IDLE)
return 0;
_tf.setTimeout(_GSM_DATA_TOUT_);
//_cell.flush();
_cell << F("AT+QENG=1,0") << endl;
_cell << F("AT+QENG?" )<< endl;
if(!_tf.find("+QENG:"))
return 0;
mcc=_tf.getValue(); // The first one is 0
mcc=_tf.getValue();
mnc=_tf.getValue();
lac=_tf.getValue();
cellid=_tf.getValue();
_tf.find("OK");
_cell << F("AT+QENG=1,0") << endl;
_tf.find("OK");
return 1;
}
boolean uBlox::readSMS(char* msg, int msglength, char* number, int nlength)
{
long index;
/*
if (getStatus()==IDLE)
return false;
*/
_tf.setTimeout(_GSM_DATA_TOUT_);
//_cell.flush();
_cell << F("AT+CMGL=\"REC UNREAD\",1") << endl;
if(_tf.find("+CMGL: "))
{
index=_tf.getValue();
_tf.getString("\"+", "\"", number, nlength);
_tf.getString("\r\n", "\r\nOK", msg, msglength);
_cell << F("AT+CMGD=") << index << endl;
_tf.find("OK");
return true;
};
return false;
};
boolean uBlox::readCall(char* number, int nlength)
{
int index;
if (getStatus()==IDLE)
return false;
_tf.setTimeout(_GSM_DATA_TOUT_);
if(_tf.find("+CLIP: \""))
{
_tf.getString("", "\"", number, nlength);
_cell << "ATH" << endl;
delay(1000);
//_cell.flush();
return true;
};
return false;
};
boolean uBlox::call(char* number, unsigned int milliseconds)
{
if (getStatus()==IDLE)
return false;
_tf.setTimeout(_GSM_DATA_TOUT_);
_cell << "ATD" << number << ";" << endl;
delay(milliseconds);
_cell << "ATH" << endl;
return true;
}
int uBlox::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.
_cell << F("AT+CPIN=") << pin << _DEC(cr) << endl; // Establecemos el pin
//Expect "OK".
if(!_tf.find("OK"))
return 0;
else
return 1;
}
int uBlox::changeNSIPmode(char mode)
{
_tf.setTimeout(_TCP_CONNECTION_TOUT_);
//if (getStatus()!=ATTACHED)
// return 0;
//_cell.flush();
_cell << F("AT+QIDNSIP=") << mode << _DEC(cr) << endl;
if(!_tf.find("OK")) return 0;
return 1;
}
int uBlox::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.
_cell << F("AT+CCID") << _DEC(cr) << endl; // Establecemos el pin
//Read response from modem
_tf.getString("+CCID: ","\r\n",cci, 21);
//Expect "OK".
if(!_tf.find("OK"))
return 0;
else
return 1;
}
int uBlox::getIMEI(char *imei)
{
_tf.setTimeout(_GSM_DATA_TOUT_); //Timeout for expecting modem responses.
//_cell.flush();
//AT command to get IMEI.
_cell << F("AT+GSN") << _DEC(cr) << endl;
//Read response from modem
_tf.getString("","\r\n",imei, 15);
//Expect "OK".
if(!_tf.find("OK"))
return 0;
else
return 1;
}
uint8_t uBlox::read()
{
return _cell.read();
}
void uBlox::SimpleRead()
{
char datain;
if(_cell.available()>0){
datain=_cell.read();
if(datain>0){
Serial.print(datain);
}
}
}
int uBlox::available()
{
return _cell.available();
}
void uBlox::SimpleWrite(char *comm)
{
_cell.print(comm);
}
void uBlox::SimpleWrite(String comm)
{
_cell.print(comm);
}
void uBlox::SimpleWrite(const char *comm)
{
_cell.print(comm);
}
void uBlox::SimpleWrite(__FlashStringHelper* comm)
{
_cell.print(comm);
}
void uBlox::SimpleWrite(int comm)
{
_cell.print(comm);
}
void uBlox::SimpleWriteln(char *comm)
{
_cell.println(comm);
}
void uBlox::SimpleWriteln(char const *comm)
{
_cell.println(comm);
}
void uBlox::SimpleWriteln(int comm)
{
_cell.println(comm);
}
void uBlox::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("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("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("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)) {