-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathSim800l.cpp
322 lines (260 loc) · 7.75 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
/* this library is writing by Cristian Steib.
* Designed to work with the GSM Sim800l,maybe work with SIM900L
*
* This library use SoftwareSerial, you can define de RX and TX pin
* in the header "Sim800l.h" ,by default the pin is RX=10 TX=11..
* be sure that gnd is attached to arduino too.
* You can also change the other preferred RESET_PIN
*
* 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
*
* PINOUT:
* _____________________________
* | ARDUINO UNO >>> SIM800L |
* -----------------------------
* GND >>> GND
* RX 10 >>> TX
* TX 11 >>> RX
* RESET 2 >>> RST
*
* POWER SOURCE 4.2V >>> VCC
*
* Created on: April 20, 2016
* Author: Cristian Steib
*
*
*/
#include "Arduino.h"
#include "Sim800l.h"
#include <SoftwareSerial.h>
SoftwareSerial SIM(RX_PIN,TX_PIN);
//String _buffer;
void Sim800l::begin(){
SIM.begin(9600);
#if (LED)
pinMode(OUTPUT,LED_PIN);
#endif
_buffer.reserve(255); //reserve memory to prevent intern fragmention
}
//
//PRIVATE METHODS
//
String Sim800l::_readSerial(){
_timeout=0;
while (!SIM.available() && _timeout < 12000 )
{
delay(13);
_timeout++;
}
if (SIM.available()) {
return SIM.readString();
}
}
//
//PUBLIC METHODS
//
void Sim800l::reset(){
#if (LED)
digitalWrite(LED_PIN,1);
#endif
digitalWrite(RESET_PIN,1);
delay(1000);
digitalWrite(RESET_PIN,0);
delay(1000);
// wait for the module response
SIM.print(F("AT\r\n"));
while (_readSerial().indexOf("OK")==-1 ){
SIM.print(F("AT\r\n"));
}
//wait for sms ready
while (_readSerial().indexOf("SMS")==-1 ){
}
#if (LED)
digitalWrite(LED_PIN,0);
#endif
}
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.
*/
SIM.print (F("AT+CFUN=1\r\n"));
}
void 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
*/
SIM.print (F("AT+CSQ\r\n"));
Serial.println(_readSerial());
}
void Sim800l::activateBearerProfile(){
SIM.print (F(" AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\" \r\n" ));_buffer=_readSerial(); // set bearer parameter
SIM.print (F(" AT+SAPBR=3,1,\"APN\",\"internet\" \r\n" ));_buffer=_readSerial(); // set apn
SIM.print (F(" AT+SAPBR=1,1 \r\n"));delay(1200);_buffer=_readSerial();// activate bearer context
SIM.print (F(" AT+SAPBR=2,1\r\n "));delay(3000);_buffer=_readSerial(); // get context ip address
}
void Sim800l::deactivateBearerProfile(){
SIM.print (F("AT+SAPBR=0,1\r\n "));
delay(1500);
}
bool Sim800l::answerCall(){
SIM.print (F("ATA\r\n"));
_buffer=_readSerial();
//Response in case of data call, if successfully connected
if ( (_buffer.indexOf("OK") )!=-1 ) return true;
else return false;
}
void Sim800l::callNumber(char* number){
SIM.print (F("ATD"));
SIM.print (number);
SIM.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
*/
SIM.print (F("AT+CPAS\r\n"));
_buffer=_readSerial();
return _buffer.substring(_buffer.indexOf("+CPAS: ")+7,_buffer.indexOf("+CPAS: ")+9).toInt();
}
bool Sim800l::hangoffCall(){
SIM.print (F("ATH\r\n"));
_buffer=_readSerial();
if ( (_buffer.indexOf("OK") ) != -1) return true;
else return false;
}
bool Sim800l::sendSms(char* number,char* text){
SIM.print (F("AT+CMGF=1\r")); //set sms to text mode
_buffer=_readSerial();
SIM.print (F("AT+CMGS=\"")); // command to send sms
SIM.print (number);
SIM.print(F("\"\r"));
_buffer=_readSerial();
SIM.print (text);
SIM.print ("\r");
//change delay 100 to readserial
_buffer=_readSerial();
SIM.print((char)26);
_buffer=_readSerial();
//expect CMGS:xxx , where xxx is a number,for the sending sms.
if (((_buffer.indexOf("CMGS") ) != -1 ) ){
return true;
}
else {
return false;
}
}
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){
SIM.print (F("AT+CMGF=1\r"));
if (( _readSerial().indexOf("ER")) ==-1) {
SIM.print (F("AT+CMGR="));
SIM.print (index);
SIM.print("\r");
_buffer=_readSerial();
if (_buffer.indexOf("CMGR:")!=-1){
return _buffer;
}
else return "";
}
else
return "";
}
bool Sim800l::delAllSms(){
SIM.print(F("at+cmgda=\"del all\"\n\r"));
_buffer=_readSerial();
if (_buffer.indexOf("OK")!=-1) {return true;}else {return false;}
}
void Sim800l::RTCtime(int *day,int *month, int *year,int *hour,int *minute, int *second){
SIM.print(F("at+cclk?\r\n"));
// if respond with ERROR try one more time.
_buffer=_readSerial();
if ((_buffer.indexOf("ERR"))!=-1){
delay(50);
SIM.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() {
SIM.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");
SIM.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 false;}else return true;
}