-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathSim800L.cpp
737 lines (595 loc) · 17.5 KB
/
Sim800L.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
/*
* 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.
*
* ESP
* Esta libreria usa SoftwareSerial, se pueden cambiar los pines de RX y TX
* en el archivo header, "Sim800L.h", por defecto los pines vienen configurado en
* RX=10 TX=11.
* Tambien se puede cambiar el RESET_PIN por otro que prefiera
*
* ITA
* Questa libreria utilizza la SoftwareSerial, si possono cambiare i pin di RX e TX
* dall' intestazione "Sim800L.h", di default essi sono impostati come RX=10 RX=11
* Assicurarsi di aver collegato il dispositivo al pin GND di Arduino.
* E' anche possibile cambiare il RESET_PIN.
*
*
* 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()
{
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);
if ( (_readSerial(5000).indexOf("ER")) == -1)
{
return false;
}
else return true;
// Error found, return 1
// Error NOT found, return 0
}
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");
return _readSerial();
}
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
}
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 ("\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("ER")) != -1) {
return true;
} else if ((_buffer.indexOf("CMGS")) != -1) {
return false;
} else {
return true;
}
// Error found, return 1
// Error NOT found, return 0
}
bool Sim800L::prepareForSmsReceive()
{
// Configure SMS in text mode
this->SoftwareSerial::print(F("AT+CMGF=1\r"));
_buffer=_readSerial();
//Serial.print(_buffer);
if((_buffer.indexOf("OK")) == -1)
{
return false;
}
this->SoftwareSerial::print(F("AT+CNMI=2,1,0,0,0\r"));
_buffer=_readSerial();
//Serial.print(_buffer);
if((_buffer.indexOf("OK")) == -1)
{
return false;
}
return true;
}
const uint8_t Sim800L::checkForSMS()
{
_buffer = _readSerial(100);
if(_buffer.length() == 0)
{
return 0;
}
_buffer += _readSerial(1000);
// Serial.println(_buffer);
// +CMTI: "SM",1
if(_buffer.indexOf("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("+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(_buffer);
if (_buffer.indexOf("CMGR") == -1)
{
return "";
}
_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+cmgda=\"del all\"\n\r"));
_buffer=_readSerial(25000);
if ( (_buffer.indexOf("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("ERR"))!=-1)
{
delay(50);
this->SoftwareSerial::print(F("at+cclk?\r\n"));
}
if ((_buffer.indexOf("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");
this->SoftwareSerial::print("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");
if ( (_readSerial().indexOf("ER"))!=-1)
{
return true;
}
else return false;
}
//
//PRIVATE METHODS
//
String Sim800L::_readSerial()
{
uint64_t timeOld = millis();
while (!this->SoftwareSerial::available() && !(millis() > timeOld + TIME_OUT_READ_SERIAL))
{
delay(13);
}
String str;
while(this->SoftwareSerial::available())
{
if (this->SoftwareSerial::available()>0)
{
str += (char) this->SoftwareSerial::read();
}
}
return str;
}
String Sim800L::_readSerial(uint32_t timeout)
{
uint64_t timeOld = millis();
while (!this->SoftwareSerial::available() && !(millis() > timeOld + timeout))
{
delay(13);
}
String str;
while(this->SoftwareSerial::available())
{
if (this->SoftwareSerial::available()>0)
{
str += (char) this->SoftwareSerial::read();
}
}
return str;
}