forked from vittorioexp/Sim800L-Arduino-Library-revised
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSim800L.cpp
More file actions
1063 lines (856 loc) · 25.8 KB
/
Copy pathSim800L.cpp
File metadata and controls
1063 lines (856 loc) · 25.8 KB
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 library was written by Vittorio Esposito
* https://github.com/VittorioEsposito
*
* Designed to work with the GSM Sim800L.
*
* ENG
* This library uses SoftwareSerial, you can define RX and TX pins
* in the header "Sim800L.h", by default pins are RX=10 and TX=11.
* Be sure that GND is connected to arduino too.
* You can also change the RESET_PIN as you prefer.
*
* DEFAULT PINOUT:
* _____________________________
* | ARDUINO UNO >>> Sim800L |
* -----------------------------
* GND >>> GND
* RX 10 >>> TX
* TX 11 >>> RX
* RESET 2 >>> RST
*
* POWER SOURCE 4.2V >>> VCC
*
*
* SOFTWARE SERIAL NOTES:
*
* PINOUT
* The library has the following known limitations:
* 1. If using multiple software serial ports, only one can receive data at a time.
* 2. Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
* 3. Not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
* 4. On Arduino or Genuino 101 the current maximum RX speed is 57600bps
* 5. On Arduino or Genuino 101 RX doesn't work on Pin 13
*
* BAUD RATE
* Supported baud rates are 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200.
*
*
* Edited on: December 24, 2016
* Editor: Vittorio Esposito
*
* Original version by: Cristian Steib
*
*
*/
#include "Arduino.h"
#include "Sim800L.h"
#include <SoftwareSerial.h>
//SoftwareSerial SIM(RX_PIN,TX_PIN);
//String _buffer;
Sim800L::Sim800L(void) : SoftwareSerial(DEFAULT_RX_PIN, DEFAULT_TX_PIN)
{
RX_PIN = DEFAULT_RX_PIN;
TX_PIN = DEFAULT_TX_PIN;
RESET_PIN = DEFAULT_RESET_PIN;
LED_PIN = DEFAULT_LED_PIN;
LED_FLAG = DEFAULT_LED_FLAG;
}
Sim800L::Sim800L(uint8_t rx, uint8_t tx) : SoftwareSerial(rx, tx)
{
RX_PIN = rx;
TX_PIN = tx;
RESET_PIN = DEFAULT_RESET_PIN;
LED_PIN = DEFAULT_LED_PIN;
LED_FLAG = DEFAULT_LED_FLAG;
}
Sim800L::Sim800L(uint8_t rx, uint8_t tx, uint8_t rst) : SoftwareSerial(rx, tx)
{
RX_PIN = rx;
TX_PIN = tx;
RESET_PIN = rst;
LED_PIN = DEFAULT_LED_PIN;
LED_FLAG = DEFAULT_LED_FLAG;
}
Sim800L::Sim800L(uint8_t rx, uint8_t tx, uint8_t rst, uint8_t led) : SoftwareSerial(rx, tx)
{
RX_PIN = rx;
TX_PIN = tx;
RESET_PIN = rst;
LED_PIN = led;
LED_FLAG = true;
}
void Sim800L::begin()
{
isBusy = false;
pinMode(RESET_PIN, OUTPUT);
_baud = DEFAULT_BAUD_RATE; // Default baud rate 9600
this->SoftwareSerial::begin(_baud);
_sleepMode = 0;
_functionalityMode = 1;
if (LED_FLAG) pinMode(LED_PIN, OUTPUT);
_buffer.reserve(BUFFER_RESERVE_MEMORY); // Reserve memory to prevent intern fragmention
}
void Sim800L::begin(uint32_t baud)
{
pinMode(RESET_PIN, OUTPUT);
_baud = baud;
this->SoftwareSerial::begin(_baud);
_sleepMode = 0;
_functionalityMode = 1;
if (LED_FLAG) pinMode(LED_PIN, OUTPUT);
_buffer.reserve(BUFFER_RESERVE_MEMORY); // Reserve memory to prevent intern fragmention
}
/*
* AT+CSCLK=0 Disable slow clock, module will not enter sleep mode.
* AT+CSCLK=1 Enable slow clock, it is controlled by DTR. When DTR is high, module can enter sleep mode. When DTR changes to low level, module can quit sleep mode
*/
bool Sim800L::setSleepMode(bool state)
{
_sleepMode = state;
if (_sleepMode) this->SoftwareSerial::print(F("AT+CSCLK=1\r\n "));
else this->SoftwareSerial::print(F("AT+CSCLK=0\r\n "));
if ( (_readSerial().indexOf("ER")) == -1)
{
return false;
}
else return true;
// Error found, return 1
// Error NOT found, return 0
}
bool Sim800L::getSleepMode()
{
return _sleepMode;
}
/*
* AT+CFUN=0 Minimum functionality
* AT+CFUN=1 Full functionality (defualt)
* AT+CFUN=4 Flight mode (disable RF function)
*/
bool Sim800L::setFunctionalityMode(uint8_t fun)
{
if (fun==0 || fun==1 || fun==4)
{
_functionalityMode = fun;
switch(_functionalityMode)
{
case 0:
this->SoftwareSerial::print(F("AT+CFUN=0\r\n "));
break;
case 1:
this->SoftwareSerial::print(F("AT+CFUN=1\r\n "));
break;
case 4:
this->SoftwareSerial::print(F("AT+CFUN=4\r\n "));
break;
}
if ( (_readSerial().indexOf("ER")) == -1)
{
return false;
}
else return true;
// Error found, return 1
// Error NOT found, return 0
}
return false;
}
uint8_t Sim800L::getFunctionalityMode()
{
return _functionalityMode;
}
bool Sim800L::setPIN(String pin)
{
String command;
command = "AT+CPIN=";
command += pin;
command += "\r";
// Can take up to 5 seconds
this->SoftwareSerial::print(command);
String pinStatus = "";
int indexOfAnswer = -1;
while (indexOfAnswer == -1)
{
pinStatus = _readSerial(10000);
if(pinStatus.indexOf("ERR") != -1)
{
Serial.println(pinStatus);
return false;
}
if(pinStatus.indexOf("OK") != -1)
{
Serial.println(pinStatus);
return true;
}
}
// Error found, return 1
// Error NOT found, return 0
}
bool Sim800L::PINIsReady()
{
String command;
command = "AT+CPIN?";
command += "\r";
// Can take up to 5 seconds
this->SoftwareSerial::print(command);
int indexOfAnswer = -1;
String pinStatus = "";
while (indexOfAnswer == -1)
{
pinStatus = _readSerial(10000);
indexOfAnswer = pinStatus.indexOf("ERR");
if(indexOfAnswer == -1)
{
indexOfAnswer = pinStatus.indexOf("OK");
}
}
if ( pinStatus.indexOf("CPIN: READY") != -1)
{
return true;
}
return false;
}
bool Sim800L::disablePin(String pin)
{
if(setPIN(pin))
{
String command;
command = "AT+CLCK=\"SC\",0,\""+pin+"\"";
command += "\r";
// Can take up to 5 seconds
this->SoftwareSerial::print(command);
String pinStatus = _readSerial(10000);
Serial.println(pinStatus);
if(pinStatus.indexOf("OK") != -1)
{
return true;
}
}
return false;
}
String Sim800L::getProductInfo()
{
this->SoftwareSerial::print("ATI\r");
return (_readSerial());
}
String Sim800L::getOperatorsList()
{
// Can take up to 45 seconds
this->SoftwareSerial::print("AT+COPS=?\r");
return _readSerial(45000);
}
String Sim800L::getOperator()
{
this->SoftwareSerial::print("AT+COPS ?\r");
String operatorName = _readSerial(1500);
if ((operatorName.indexOf("+COPS:")) == -1)
{
return "Unknown";
}
if ((operatorName.indexOf("\"")) != -1)
{
int indexOfQuote = operatorName.indexOf("\"");
int lastIndexOfQuote = operatorName.lastIndexOf("\"");
if(lastIndexOfQuote != indexOfQuote && indexOfQuote < lastIndexOfQuote)
{
return operatorName.substring(indexOfQuote+1, lastIndexOfQuote);
}
}
return _readSerial();
}
bool Sim800L::registerToNetwork()
{
this->SoftwareSerial::print("AT+CREG=1\r");
if ( (_readSerial(5000).indexOf("OK")) == -1)
{
return true;
}
return false;
}
NetworkRegistrationStatus Sim800L::registrationStatus()
{
this->SoftwareSerial::print("AT+CREG ?\r");
String status = _readSerial();
if(status.indexOf("CREG: 0,1") > -1)
{
return NetworkRegistrationStatus::notRegistrerAndNotSearching;
}
if(status.indexOf("CREG: 1,1") > -1)
{
return NetworkRegistrationStatus::registrerHomeNetwork;
}
if(status.indexOf("CREG: 2,1") > -1)
{
return NetworkRegistrationStatus::notRegistrerAndSearching;
}
if(status.indexOf("CREG: 3,1") > -1)
{
return NetworkRegistrationStatus::registerDenied;
}
if(status.indexOf("CREG: 4,1") > -1)
{
return NetworkRegistrationStatus::unknown;
}
if(status.indexOf("CREG: 5,1") > -1)
{
return NetworkRegistrationStatus::registerRoaming;
}
if(status.indexOf("CREG: 6,1") > -1)
{
return NetworkRegistrationStatus::registerSmsOnlyHomeNetwork;
}
if(status.indexOf("CREG: 7,1") > -1)
{
return NetworkRegistrationStatus::registerSmsOnlyRoaming;
}
if(status.indexOf("CREG: 8,1") > -1)
{
return NetworkRegistrationStatus::registeredForEmergencyService;
}
if(status.indexOf("CREG: 9,1") > -1)
{
return NetworkRegistrationStatus::registeredForCSFBNotPreferedHomeNetwork;
}
if(status.indexOf("CREG: 9,1") > -1)
{
return NetworkRegistrationStatus::registeredForCSFBNotPreferedRoaming;
}
return NetworkRegistrationStatus::unknown;
}
bool Sim800L::calculateLocation()
{
/*
Type: 1 To get longitude and latitude
Cid = 1 Bearer profile identifier refer to AT+SAPBR
*/
uint8_t type = 1;
uint8_t cid = 1;
String tmp = "AT+CIPGSMLOC=" + String(type) + "," + String(cid) + "\r\n";
this->SoftwareSerial::print(tmp);
/*
this->SoftwareSerial::print("AT+CIPGSMLOC=");
this->SoftwareSerial::print(type);
this->SoftwareSerial::print(",");
this->SoftwareSerial::print(cid);
this->SoftwareSerial::print("\r");
*/
String data = _readSerial(20000);
if (data.indexOf("ER")!=(-1)) return false;
uint8_t indexOne;
uint8_t indexTwo;
indexOne = data.indexOf(":") + 1;
indexTwo = data.indexOf(",");
_locationCode = data.substring(indexOne, indexTwo);
indexOne = data.indexOf(",") + 1;
indexTwo = data.indexOf(",", indexOne);
_longitude = data.substring(indexOne, indexTwo);
indexOne = data.indexOf(",", indexTwo) + 1;
indexTwo = data.indexOf(",", indexOne);
_latitude = data.substring(indexOne, indexTwo);
return true;
}
String Sim800L::getLocationCode()
{
return _locationCode;
/*
Location Code:
0 Success
404 Not Found
408 Request Time-out
601 Network Error
602 No Memory
603 DNS Error
604 Stack Busy
65535 Other Error
*/
}
String Sim800L::getLongitude()
{
return _longitude;
}
String Sim800L::getLatitude()
{
return _latitude;
}
//
//PUBLIC METHODS
//
void Sim800L::reset()
{
if (LED_FLAG) digitalWrite(LED_PIN,1);
digitalWrite(RESET_PIN,1);
delay(1000);
digitalWrite(RESET_PIN,0);
delay(1000);
// wait for the module response
this->SoftwareSerial::print(F("AT\r\n"));
while (_readSerial().indexOf("OK")==-1 )
{
this->SoftwareSerial::print(F("AT\r\n"));
}
//wait for sms ready
while (_readSerial().indexOf("SMS")==-1 );
if (LED_FLAG) digitalWrite(LED_PIN,0);
}
void Sim800L::setPhoneFunctionality()
{
/*AT+CFUN=<fun>[,<rst>]
Parameters
<fun> 0 Minimum functionality
1 Full functionality (Default)
4 Disable phone both transmit and receive RF circuits.
<rst> 1 Reset the MT before setting it to <fun> power level.
*/
this->SoftwareSerial::print (F("AT+CFUN=1\r\n"));
}
String Sim800L::signalQuality()
{
/*Response
+CSQ: <rssi>,<ber>Parameters
<rssi>
0 -115 dBm or less
1 -111 dBm
2...30 -110... -54 dBm
31 -52 dBm or greater
99 not known or not detectable
<ber> (in percent):
0...7 As RXQUAL values in the table in GSM 05.08 [20]
subclause 7.2.4
99 Not known or not detectable
*/
this->SoftwareSerial::print (F("AT+CSQ\r\n"));
return(_readSerial());
}
void Sim800L::activateBearerProfile()
{
this->SoftwareSerial::print (F(" AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\" \r\n" ));
_buffer=_readSerial(); // set bearer parameter
this->SoftwareSerial::print (F(" AT+SAPBR=3,1,\"APN\",\"internet\" \r\n" ));
_buffer=_readSerial(); // set apn
this->SoftwareSerial::print (F(" AT+SAPBR=1,1 \r\n"));
delay(1200);
_buffer=_readSerial(); // activate bearer context
this->SoftwareSerial::print (F(" AT+SAPBR=2,1\r\n "));
delay(3000);
_buffer=_readSerial(); // get context ip address
}
void Sim800L::deactivateBearerProfile()
{
this->SoftwareSerial::print (F("AT+SAPBR=0,1\r\n "));
delay(1500);
}
bool Sim800L::answerCall()
{
this->SoftwareSerial::print (F("ATA\r\n"));
//Response in case of data call, if successfully connected
if ( (_readSerial().indexOf("ER")) == -1)
{
return false;
}
else return true;
// Error found, return 1
// Error NOT found, return 0
}
void Sim800L::callNumber(char* number)
{
this->SoftwareSerial::print (F("ATD"));
this->SoftwareSerial::print (number);
this->SoftwareSerial::print (F(";\r\n"));
}
uint8_t Sim800L::getCallStatus()
{
/*
values of return:
0 Ready (MT allows commands from TA/TE)
2 Unknown (MT is not guaranteed to respond to tructions)
3 Ringing (MT is ready for commands from TA/TE, but the ringer is active)
4 Call in progress
*/
this->SoftwareSerial::print (F("AT+CPAS\r\n"));
_buffer=_readSerial();
return _buffer.substring(_buffer.indexOf("+CPAS: ")+7,_buffer.indexOf("+CPAS: ")+9).toInt();
}
bool Sim800L::hangoffCall()
{
this->SoftwareSerial::print (F("ATH\r\n"));
_buffer=_readSerial();
if ( (_buffer.indexOf("ER")) == -1)
{
return false;
}
else return true;
// Error found, return 1
// Error NOT found, return 0
}
int Sim800L::sendSms(String pdu)
{
int pduLength = (pdu.length() / 2)-1;
if(pduLength < 10 || isBusy)
{
// Bad Pdu
return -4;
}
isBusy = true;
this->SoftwareSerial::print (F("AT+CMGS=")); // command to send sms
this->SoftwareSerial::print (pduLength);
this->SoftwareSerial::println();
_buffer=_readSerial(100);
this->SoftwareSerial::print (pdu);
_buffer=_readSerial(100);
this->SoftwareSerial::write(0x1a); // Ctrl+Z end of the message
_buffer=_readSerial(60000);
//expect CMGS:xxx , where xxx is a number,for the sending sms.
if ((_buffer.indexOf(F("ERROR"))) != -1) {
isBusy = false;
return -2;
}
if ((_buffer.indexOf(F("CMGS"))) < 0) {
isBusy = false;
return -3;
}
int indexOfTwoDots = _buffer.indexOf(':');
isBusy = false;
if(indexOfTwoDots == -1)
{
return -1;
}
return (_buffer.substring(indexOfTwoDots+1)).toInt();
}
bool Sim800L::sendSms(char* number,char* text)
{
// Can take up to 60 seconds
this->SoftwareSerial::print (F("AT+CMGF=1\r")); //set sms to text mode
_buffer=_readSerial();
this->SoftwareSerial::print (F("AT+CMGS=\"")); // command to send sms
this->SoftwareSerial::print (number);
this->SoftwareSerial::print(F("\"\r"));
_buffer=_readSerial();
this->SoftwareSerial::print (text);
this->SoftwareSerial::print (F("\r"));
_buffer=_readSerial();
this->SoftwareSerial::print((char)26);
_buffer=_readSerial(60000);
// Serial.println(_buffer);
//expect CMGS:xxx , where xxx is a number,for the sending sms.
if ((_buffer.indexOf(F("ER"))) != -1) {
return true;
} else if ((_buffer.indexOf(F("CMGS"))) != -1) {
return false;
} else {
return true;
}
// Error found, return 1
// Error NOT found, return 0
}
bool Sim800L::setPduMode()
{
this->SoftwareSerial::print(F("AT+CMGF=0\r"));
_buffer=_readSerial();
if((_buffer.indexOf(F("OK"))) == -1)
{
return false;
}
return true;
}
bool Sim800L::setTextMode()
{
this->SoftwareSerial::print(F("AT+CMGF=1\r"));
_buffer=_readSerial();
Serial.print(_buffer);
if((_buffer.indexOf(F("OK"))) == -1)
{
return false;
}
return true;
}
bool Sim800L::prepareForSmsReceive()
{
this->SoftwareSerial::print(F("AT+CNMI=2,2,0,1,0\r")); // 2,1,0,1,0 Active Ds mode (Data report)
_buffer=_readSerial();
//Serial.print(_buffer);
if((_buffer.indexOf(F("OK"))) == -1)
{
return false;
}
return true;
}
void Sim800L::checkForGsmMessage()
{
_buffer = _readSerial(100);
if(_buffer.length() == 0)
{
return ;
//return 0;
}
_buffer += _readSerial(5000);
/* Serial.println("checkForSMS");
Serial.println(_buffer);
Serial.println("-----------------");*/
int indexOfCds = _buffer.indexOf(F("+CDS:"));
int firstIndexHandle = 0;
int lastIndexHandle = 0;
//bool hasMeetFirstCrlf = false;
if(indexOfCds != -1)
{
// Here we receive a Status report
// Response format
//+CDS: 33\r\n
//07913366003000F006090B913356108867F8122091901000401220919010004000\r\n
for(int i = indexOfCds; i < _buffer.length(); i++)
{
/* if(hasMeetFirstCrlf)
{
// need to wait for the first CR
Serial.print(_buffer[i]);
Serial.print(" --- ");
Serial.println(_buffer[i], HEX);
}*/
if(i>0 && _buffer[i-1] == '\r' && _buffer[i] == '\n')
{
if(firstIndexHandle == 0)
{
firstIndexHandle = i+1;
//hasMeetFirstCrlf = true;
}
else{
lastIndexHandle = i-1;
// we are at the end of the line. Need to send info
if(firstIndexHandle > 0 && lastIndexHandle > firstIndexHandle)
{
if(onStatusReport != NULL){
onStatusReport(_buffer.substring(firstIndexHandle, lastIndexHandle));
}
}
}
}
}
}
firstIndexHandle = 0;
lastIndexHandle = 0;
int indexOfCMT = _buffer.indexOf(F("+CMT:"));
while(indexOfCMT != -1){
// Here we receive a new message
// Response format
//+CMT: "",135\r\n
//07913366003000F0240B913356108867F800001220919013734084C170381C0E87C3E170381C0E87C3E170381C0E87C3E1B05EAFD7EBF57ABD7E3E9FCFCF67341AAD56ABD56A75BD9E4AD3631ADA9FBEBC97AE3DE84974EFDE8436DFAD3872C68C62152C68C92399C29396BD964A31634AA56BD6A59CD6\r\n
for(int i = indexOfCMT; i < _buffer.length(); i++)
{
// need to wait for the first CR
/* Serial.print(_buffer[i]);
Serial.print(" --- ");
Serial.println(_buffer[i], HEX);*/
if(i>0 && (_buffer[i-1] == '\r' && _buffer[i] == '\n') || (_buffer[i] == '\n'))
{
if(firstIndexHandle == 0)
{
firstIndexHandle = i+1;
//hasMeetFirstCrlf = true;
}
else{
if(_buffer[i-1] == '\r')
{lastIndexHandle = i-1;}
else
{lastIndexHandle = i;}
// we are at the end of the line. Need to send info
if(firstIndexHandle > 0 && lastIndexHandle > firstIndexHandle)
{
if(onNewMessage != NULL){
onNewMessage(_buffer.substring(firstIndexHandle, lastIndexHandle));
}
// Check all messages in the _buffer. Indeed, we can have more than one message
indexOfCMT = _buffer.indexOf(F("+CMT:"), lastIndexHandle); // add the header '+CMT:' length
firstIndexHandle = 0;
}
}
}
}
}
//return _buffer;
// +CMTI: "SM",1
// if(_buffer.indexOf("+CMTI:") == -1)
// {
// return 0;
// }
// return _buffer.substring(_buffer.indexOf(',')+1).toInt();
}
const uint8_t Sim800L::checkForSMS()
{
_buffer = _readSerial(100);
if(_buffer.length() == 0)
{
return 0;
}
_buffer += _readSerial(1000);
// +CMTI: "SM",1
if(_buffer.indexOf(F("+CMTI:")) == -1)
{
return 0;
}
return _buffer.substring(_buffer.indexOf(',')+1).toInt();
}
String Sim800L::getNumberSms(uint8_t index)
{
_buffer=readSms(index);
//Serial.println(_buffer.length());
if (_buffer.length() > 10) //avoid empty sms
{
uint8_t _idx1=_buffer.indexOf(F("+CMGR:"));
_idx1=_buffer.indexOf("\",\"",_idx1+1);
return _buffer.substring(_idx1+3,_buffer.indexOf("\",\"",_idx1+4));
}
else
{
return "";
}
}
String Sim800L::readSms(uint8_t index)
{
// Can take up to 5 seconds
if(( _readSerial(5000).indexOf("ER")) != -1)
{
return "";
}
this->SoftwareSerial::print (F("AT+CMGR="));
this->SoftwareSerial::print (index);
this->SoftwareSerial::print ("\r");
_buffer=_readSerial();
//Serial.println("Received !!");
//Serial.println(_buffer);
if (_buffer.indexOf(F("CMGR=")) == -1)
{
return "";
}
//Serial.println(_buffer);
_buffer = _readSerial(10000);
byte first = _buffer.indexOf('\n', 2) + 1;
byte second = _buffer.indexOf('\n', first);
return _buffer.substring(first, second);
}
bool Sim800L::delAllSms()
{
// Can take up to 25 seconds
this->SoftwareSerial::print(F("AT+CMGD=4\r"));
_buffer=_readSerial(25000);
if ( (_buffer.indexOf(F("ER"))) == -1)
{
return false;
}
else return true;
// Error found, return 1
// Error NOT found, return 0
}
void Sim800L::RTCtime(int *day,int *month, int *year,int *hour,int *minute, int *second)
{
this->SoftwareSerial::print(F("at+cclk?\r\n"));
// if respond with ERROR try one more time.
_buffer=_readSerial();
if ((_buffer.indexOf(F("ERR")))!=-1)
{
delay(50);
this->SoftwareSerial::print(F("at+cclk?\r\n"));
}
if ((_buffer.indexOf(F("ERR")))==-1)
{
_buffer=_buffer.substring(_buffer.indexOf("\"")+1,_buffer.lastIndexOf("\"")-1);
*year=_buffer.substring(0,2).toInt();
*month= _buffer.substring(3,5).toInt();
*day=_buffer.substring(6,8).toInt();
*hour=_buffer.substring(9,11).toInt();
*minute=_buffer.substring(12,14).toInt();
*second=_buffer.substring(15,17).toInt();
}
}
//Get the time of the base of GSM
String Sim800L::dateNet()
{
this->SoftwareSerial::print(F("AT+CIPGSMLOC=2,1\r\n "));
_buffer=_readSerial();
if (_buffer.indexOf("OK")!=-1 )
{
return _buffer.substring(_buffer.indexOf(":")+2,(_buffer.indexOf("OK")-4));
}
else
return "0";
}
// Update the RTC of the module with the date of GSM.
bool Sim800L::updateRtc(int utc)
{
activateBearerProfile();
_buffer=dateNet();
deactivateBearerProfile();
_buffer=_buffer.substring(_buffer.indexOf(",")+1,_buffer.length());
String dt=_buffer.substring(0,_buffer.indexOf(","));
String tm=_buffer.substring(_buffer.indexOf(",")+1,_buffer.length()) ;
int hour = tm.substring(0,2).toInt();
int day = dt.substring(8,10).toInt();
hour=hour+utc;
String tmp_hour;
String tmp_day;
//TODO : fix if the day is 0, this occur when day is 1 then decrement to 1,
// will need to check the last month what is the last day .
if (hour<0)
{
hour+=24;
day-=1;
}
if (hour<10)
{
tmp_hour="0"+String(hour);
}
else
{
tmp_hour=String(hour);
}
if (day<10)
{
tmp_day="0"+String(day);
}
else
{
tmp_day=String(day);
}
//for debugging
//Serial.println("at+cclk=\""+dt.substring(2,4)+"/"+dt.substring(5,7)+"/"+tmp_day+","+tmp_hour+":"+tm.substring(3,5)+":"+tm.substring(6,8)+"-03\"\r\n");