-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathArduinotechGSMShield.cpp
490 lines (436 loc) · 10.9 KB
/
ArduinotechGSMShield.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
//Utilities for ArduinotechGSMShield
#include "Arduino.h"
#include "ArduinotechGSMShield.h"
#include "SoftwareSerial.h"
SoftwareSerial AGS::SIM800(RX,TX);
AGS::AGS(uint8_t debug)
{
_debug = debug;
}
void AGS::begin()
{
SIM800.begin(9600);
Serial.begin(9600);
//restartSIMHW();
SIM800Init();
}
//SIM800 initialization procedure for simple SMS and call features
void AGS::SIM800Init()
{
while (Serial.available() > 0) Serial.read();
Serial.println F("****************************************");
while (sendATcommand("AT", "OK", 2000) == 0);
Serial.println F("SIM800 available!");
delay(2000);
//otestuj registraci do site
while ((sendATcommand("AT+CREG?", "+CREG: 0,1", 1000) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 1000)) == 0);
while (Serial.available() > 0) Serial.read();
Serial.println F("SIM800 registered to GSM!");
//parameters to obtain time stamp from GSM network
while (sendATcommand("AT+CLTS=1", "OK", 500) == 0);
while (sendATcommand("AT+CENG=3", "OK", 500) == 0);
while (Serial.available() > 0) Serial.read();
Serial.println("Time stamp from GSM:" + timeStamp());
Serial.println("GSM signal quality:" + getQuality());
Serial.println("GSM provider:" + getProviderName());
sendATcommand("AT+CMGF=1", "OK", 500);
//zakaz indikace SMS
sendATcommand("AT+CNMI=0,0", "OK", 500);
//CLIP enabled
sendATcommand("AT+CLIP=1", "OK", 1000);
//smaz vsechny SMSky
sendATcommand("AT+CMGD=1,4", "OK", 2000);
sendATcommand("AT+CMGD=1", "OK", 2000);
Serial.println F("SIM800 has been configured!");
Serial.println F("****************************************");
}
bool AGS::GPRSInit(String APN)
{
APN = "AT+SAPBR=3,1,\"APN\",\"" + APN + "\"" ;
char APNchar[sizeof(APN)+1];
for (uint8_t i; i < (sizeof(APN) + 1); i++) APNchar[i] = APN[i];
while( (sendATcommand("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", "OK", 500)) == 0 );
//APN = "AT+SAPBR=3,1,\"APN\",\" + APN + "\"";
while( (sendATcommand(APNchar, "OK", 1000)) == 0 );
SIM800.println("AT+SAPBR=1,1");
delay(1000);
SIM800.println("AT+SAPBR=2,1");
delay(1000);
clrSIMbuffer();
while( (sendATcommand("AT+HTTPINIT", "OK", 3000))|| (sendATcommand("AT+HTTPINIT", "ERROR", 3000))== 0 );
//while( (sendATcommand("AT+HTTPINIT", "OK", 3000)));
if (_debug == 1)
{
Serial.println F("GPRS Attached");
Serial.println F("****************************************");
}
return 1;
}
String AGS::sendDataGPRS(String dataToSend)
{
int dataLength;
dataToSend = "\""+dataToSend+"\"";
delay(10);
clrSIMbuffer();
if (_debug == 1) Serial.println F("***********GPRS communication***********");
SIM800.println("AT+HTTPPARA=\"URL\"," + dataToSend);
while (SIM800.find("OK"));
delay(10);
clrSIMbuffer();
while( (sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:", 10000)) == 0 );
delay(10);
clrSIMbuffer();
//read response
SIM800.println("AT+HTTPREAD");
delay(500);
char gsmc;
String content="";
if(SIM800.find("+HTTPREAD:"))
{
if (_debug == 1) Serial.println("Reading response ...");
while(SIM800.available()>0)
{
gsmc = SIM800.read();
content += gsmc;
if (gsmc == '\n')
{
content = content.substring(0,content.length()-2);
dataLength = content.toInt();
if (_debug == 1) Serial.println("Data Lenght:" + String(dataLength));
content = "";
break;
}
}
//read content
if (_debug == 1)Serial.print("Content:");
delay(100);
while(SIM800.available()>0)
{
gsmc = SIM800.read();
content += gsmc;
dataLength --;
if (dataLength < -1)
{
content = content.substring(0,content.length()-2);
if (_debug == 1) Serial.println(content);
content = "";
break;
}
}
}
else
{
return "COMMUNICATION FAILURE";
}
delay(10);
clrSIMbuffer();
if (_debug == 1) Serial.println F("****************************************");
return content;
}
//Send AT command to SIM800
int8_t AGS::sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout)
{
//WDT_Restart(WDT);
uint8_t x = 0, answer = 0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
//clrSIMbuffer();
SIM800.println(ATcommand); // Send the AT command
//WDT_Restart(WDT);
x = 0;
previous = millis();
// this loop waits for the answer
do
{
if (SIM800.available() != 0) {
// if there are data in the UART input buffer, reads it and checks for the asnwer
response[x] = SIM800.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
} while ((answer == 0) && ((millis() - previous) < timeout));
//clrSIMbuffer();
return answer;
}
//Send AT command to SIM800 with response
String AGS::sendATcommandResponse(char* ATcommand, char* expected_answer, unsigned int timeout, unsigned int buf)
{
//WDT_Restart(WDT);
uint8_t x = 0, answer = 0;
char response[150];
unsigned long previous;
String rest;
memset(response, '\0', 100); // Initialize the string
delay(100);
//clrSIMbuffer();
SIM800.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
//WDT_Restart(WDT);
// this loop waits for the answer
do
{
if (SIM800.available() != 0) {
// if there are data in the UART input buffer, reads it and checks for the asnwer
response[x] = SIM800.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
} while ((answer == 0) && ((millis() - previous) < timeout));
//p?e?ti zbytek - max 20 byte
memset(response, '\0', buf); // Initialize the string
delay(100);
for (x = 0; x < buf; x++) response[x] = SIM800.read();
//clrSIMbuffer();
for (x = 0; x < buf; x++) rest += char(response[x]);
delay(100);
return rest;
}
//Check SIM800 if call or SMS is present
uint8_t AGS::checkCallAndSMS()
{
char g;
String gcmd;
uint8_t gindex;
//check call presence
while (SIM800.available()>0)
{
g = SIM800.read();
gcmd += g;
if (g == '\n')
{
gcmd = gcmd.substring(0, gcmd.length() - 2);
gindex = gcmd.indexOf('+');
if (gcmd.substring(gindex+1,gindex+6) == "CLIP:")
{
//+CLIP: "420739822476"
//cut +CLIP:
//check if + is present and parse according this character
gcmd = gcmd.substring(gindex+6);
if (gcmd.indexOf('+') != -1)
{
gindex = gcmd.indexOf('+');
gcmd = gcmd.substring((gindex + 1), (gindex + 13));
}
//parse according first " character
else
{
gindex = gcmd.indexOf('"');
gcmd = gcmd.substring((gindex + 1), (gindex + 13));
}
if(_debug == 1)
{
Serial.println("Call received!");
Serial.println("Number:"+gcmd);
}
callInProgress = false;
number = gcmd;
return 1;
}
else
{
//get timeStamp
//actTime = timeStamp();
return 0;
}
}
}
gcmd = "";
//test SMS presence
clrSIMbuffer();
SIM800.println("AT+CMGR=1");
delay(100);
//echo surpress
while (SIM800.available()>0)
{
g = SIM800.read();
gcmd += g;
if (g == '\n')
{
gcmd = "";
break;
}
}
//read first line with command response
while (SIM800.available()>0)
{
g = SIM800.read();
gcmd += g;
if (g == '\n')
{
if (gcmd.substring(0, 2) == "OK") return 0;
if (gcmd.substring(0, 5) == "+CMGR")
{
if (_debug == 1)
{
Serial.println F("****************************************");
Serial.println F("New SMS alert!");
}
//first + "+CMGR"
gindex = gcmd.indexOf('+');
if (gindex < 0) return 0;
//cut off first + character
gcmd = gcmd.substring(gindex + 1);
//sender number
gindex = gcmd.indexOf('+');
number = gcmd.substring(gindex + 1, gindex + 13);
Serial.println("From:" + number);
//read SMS content
gcmd = "";
delay(50);
while (SIM800.available()>0)
{
g = SIM800.read();
gcmd += g;
if ((g == '\n') && (gcmd.length()>2))
{
gcmd = gcmd.substring(0, gcmd.length() - 2);
Serial.println("SMS Content:" + gcmd);
sendATcommand("AT+CMGD=1", "OK", 2000);
sendATcommand("AT+CMGD=1,4", "OK", 2000);
clrSIMbuffer();
if (_debug == 1)
{
Serial.println F("SMS deleted.");
Serial.println F("****************************************");
}
SMScontent = gcmd;
return 2;
}
}
gcmd = "";
sendATcommand("AT+CMGD=1", "OK", 2000);
sendATcommand("AT+CMGD=1,4", "OK", 2000);
clrSIMbuffer();
//get timeStamp
//actTime = timeStamp();
return 0;
}
}
}
//get timeStamp
//actTime = timeStamp();
return 0;
}
//Call End
void AGS::callEnd()
{
sendATcommand("ATH","OK",2000);
return;
}
//Make call
void AGS::makeCall(String callNumber)
{
SIM800.print("ATD ");
SIM800.print(callNumber);
SIM800.println(";");
clrSIMbuffer();
}
//Get last Sender or A-party number
String AGS::getNumber()
{
return number;
}
//get SMS content
String AGS::getSMSContent()
{
return SMScontent;
}
//Get Provider Name
String AGS::getProviderName()
{
providerName = sendATcommandResponse("AT+COPS?", "+COPS:", 1000, 20);
tempIndex = providerName.indexOf('"');
providerName = providerName.substring(tempIndex + 1);
tempIndex = providerName.indexOf('"');
providerName = providerName.substring(0, tempIndex);
return providerName;
}
//Get signal quality
String AGS::getQuality()
{
GSMsignal = sendATcommandResponse("AT+CSQ", "+CSQ:", 1000, 3);
return GSMsignal;
}
//Send SMS
void AGS::sendSMS(String number, String sms)
{
if (_debug==1)
{
Serial.println("SMS pro:" + number);
Serial.println("Obsah:" + sms);
delay(500);
}
clrSIMbuffer();
//number = "+420"+number;
SIM800.println("AT+CMGS=\"" + number + "\"");
delay(200);
//toSerial();
SIM800.println(sms); // message to send
delay(100);
SIM800.write((char)26); //CTRL+Z
delay(100);
SIM800.println();
delay(100);
sendATcommand("AT+CMGD=1", "OK", 2000);
sendATcommand("AT+CMGD=1,4", "OK", 2000);
sendATcommand("AT+CMGD=1", "OK", 2000);
delay(500);
clrSIMbuffer();
if (_debug==1)
{
Serial.println("SMS odeslana!");
Serial.println F("****************************************");
}
}
//Time stamp from SIM800 - GSM network time
String AGS::timeStamp()
{
String ts = "";
ts = sendATcommandResponse("AT+CCLK?", "+CCLK: \"", 1000, 20);
//13/11/04,15:23:19+04
//return ("20"+ts.substring(0,2)+ts.substring(3,5)+ts.substring(6,8)+ts.substring(9,11)+ts.substring(12,14)+ts.substring(15,17));
return ts;
}
String AGS::actualTime()
{
return actTime;
}
//HW restart of SIM800
void AGS::restartSIMHW()
{
//HW restart
digitalWrite(GSMReset, LOW);
delay(400);
digitalWrite(GSMReset, HIGH);
delay(400);
}
//Clear SIM800 buffer
void AGS::clrSIMbuffer()
{
while (SIM800.available()>0)
{
delay(1);
SIM800.read();
}
}
//Check if SIM800 is attached to GSM
bool AGS::isConnected()
{
if ((sendATcommand("AT+CREG?", "+CREG: 0,1", 1000) ||
sendATcommand("AT+CREG?", "+CREG: 0,5", 1000)) == 1) return true;
else return false;
}