forked from DFRobot/GPS-GPRS-GSM-Shield-V3.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim908_GPS_GSM_GPRS
388 lines (293 loc) · 8.65 KB
/
sim908_GPS_GSM_GPRS
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
/*This code has been developed by Francisco Caussa
* Under Licence GPLv3
* Th porpuse of this code is to work with any Arduino Board
* and a Sim908 GPS/GPRS/GSM Module
* email: [email protected]
*
*
*/
/*______________USAGE_______________
*This code is intended to be used between Arduino (or similar boards) and SIM908 GPS/GPRS/GSM module.
*You can modify the code to fit other MCU
*it uses Serial AT commands so it can be adapted to fit almost any MCU with serial capabilities
*/
/*___________WIRING________________
*This code uses Software serial to talk between GPS/GSM/GPRS shield and MCU
*Therefore Serial pins (0,1 on Arduino-Like boards) are unused, so you can debug with SerialMonitor
*WARNING!
*It requires hardware modifications on your shield.
*You have to "BEND" pins 0,1 from shield so they no longer connect to the MCU board
*And you have to wire those pins to the ones you will use on Software serial.
*In this example, Software serial is defined on PINS 10 and 11.
*You can change them to whatever you feel like.
*/
/*This code has been modified several times.
*It requires Software Serial and TimerOne libraries.
*TimerOne Library -----> https://github.com/PaulStoffregen/TimerOne Thank you Paul for your Library.
*Software Serial ----> https://www.arduino.cc/en/Reference/SoftwareSerial
#include <SoftwareSerial.h>
#include <TimerOne.h>
SoftwareSerial sim908(10, 11); // RX, TX for debug porpouses
char latitude[15]; //latitude variable
char longitude[15]; //longitude Variable
char last_latitude[15]; //last latitude used to compare if location changed
char last_longitude[15]; //last longitude used to compare if location changed
String inData; //Serial Buffer
char inDataCharArray[50];
boolean iguales = true; //Flag to compare if variables are the same
boolean validCode = false; //Flag to check is SMS code is valid to execute "something"
char msgSMS[20];
char msg2send[20];
char answer[20];
int inicioCodigo;
int v207;
boolean is207 = false;
int interruptCounter=0;
void setup()
{
//Init the driver pins for GSM function
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
//Output GSM Timing
digitalWrite(5,HIGH);
delay(1500);
digitalWrite(5,LOW);
sim908.begin(9600);
Serial.begin(9600);
digitalWrite(3,LOW);//enable GSM TX、RX
digitalWrite(4,HIGH);//disable GPS TX、RX
delay(30000); //wait 30s for shield to initialize
setTextModeSMS();
delay(1000);
start_GSM(); //set up GSM configuration
delay(5000);
start_GPS(); //set up and initialize GPS
delay(2000);
sim908.flush(); //clear whatever is left on serial.
Timer1.initialize(1000000); // set a timer of length 1000000 microseconds (1 sec)
Timer1.attachInterrupt( timerISR ); // attach the service routine here
}
void loop()
{
while(!(Serial.available()>0)){
//do nothing until a serial SMS reaches or interrupt makes update GPS coordinates
}
}
void start_GSM(){
//Configuracion GPRS Claro Argentina
sim908.println("AT");
delay(2000);
sim908.println("AT+CNMI=2,2,0,0,0"); // as soon as somethnig reaches the SIM908, will be sent throught serial to the Arduino
delay(2000);
sim908.println("AT+CREG?");
delay(2000);
sim908.println("AT+SAPBR=3,1,\"APN\",\"igprs.claro.com.ar\""); //APN
delay(2000);
sim908.println("AT+SAPBR=3,1,\"USER\",\"clarogprs\""); //user
delay(2000);
sim908.println("AT+SAPBR=3,1,\"PWD\",\"clarogprs999\""); //Pass
delay(2000);
sim908.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
delay(2000);
sim908.println("AT+SAPBR=1,1");
delay(10000);
sim908.println("AT+HTTPINIT");
delay(2000);
sim908.println("AT+HTTPPARA=\"CID\",1");
delay(2000);
}
void send_GPRS(){
sim908.print("AT+HTTPPARA=\"URL\",\"YOU_WEB_SERVER_URL_/.php?latitude=");
sim908.print(latitude);
sim908.print("&longitude=");
sim908.print(longitude);
sim908.println("\"");
delay(2000);
sim908.println("AT+HTTPACTION=0"); //now GET action
delay(2000);
}
void start_GPS(){
//Configuracion en Inicializacion GPS
sim908.print("AT");
delay(1000);
sim908.println("AT+CGPSIPR=9600");// (set the baud rate)
delay(1000);
sim908.println("AT+CGPSPWR=1"); // (turn on GPS power supply)
delay(1000);
sim908.println("AT+CGPSRST=1"); //(reset GPS in autonomy mode)
delay(10000); //delay para esperar señal del GPS
}
void read_GPS(){
sim908.println("AT+CGPSINF=0");
read_String();
inData.toCharArray(inDataCharArray,50);
strtok(inDataCharArray, ",");
strcpy(longitude,strtok(NULL, ",")); // Gets longitude
strcpy(latitude,strtok(NULL, ",")); // Gets latitude
DDMM2ddmm(latitude, "latitud"); //fix data
DDMM2ddmm(longitude, "longitud"); //fix data
}
void read_String() { //function to read serial data and store it as string
inData="";
inData=sim908.readStringUntil('\n');
Serial.print(inData); //Debug
}
void DDMM2ddmm(char charArray[15], String flag){
boolean negative = false;
double DD;
double MM;
char dd[5];
char mm[15];
int index=0;
int i=0;
int j=0;
int k=0;
int dotposition;
int nullcharcounter;
boolean notadot=true;
boolean notlenghtend=true;
if(charArray[0]=='-'){
negative=true;
index=1;
}
while(notadot){
if(charArray[index]=='.'){
notadot=false;
}
else{
index++;
}
}
dotposition=index;
if(negative){
i=1;
}
else{
i=0;
}
k=0;
for(j=i; j<dotposition-2;j++){
dd[k]=charArray[j];
k++;
}
dd[k]='\0';
index=(dotposition-2);
mm[0]='0';
mm[1]='.';
mm[2]=charArray[index];
mm[3]=charArray[index+1];
index=(dotposition+1);
k=0;
nullcharcounter=0;
while(notlenghtend){
if(charArray[index]=='\0'){
notlenghtend=false;
}
else{
nullcharcounter++;
index++;
}
}
index=(dotposition+1);
for(k=0;k<nullcharcounter;k++){
mm[k+4]=charArray[index+k];
}
mm[k+5]='\0';
DD=atof(dd);
if(negative){
DD=DD*(-1);
}
MM=atof(mm);
MM=100*MM/60;
if(negative){
MM=MM*(-1);
}
DD=DD+MM; //re arrange variable
if(flag=="latitud"){
dtostrf(DD,5,6,latitude); //write lattitude into variable to be sent
}
if(flag=="longitud"){
dtostrf(DD,5,6,longitude); //write longitude into variable to be sent
}
}
void setTextModeSMS(){
sim908.println("AT+CMGF=1"); //Set to Text Mode
delay(1000);
deleteALL(); //erase all messages
}
void readSMS() {
inicioCodigo=inData.indexOf("P"); //find the position of letter P. since valid code begins as P207
if(inicioCodigo>0){
if(inData.substring(inicioCodigo+1,inicioCodigo+3)=="207"){
//then, there is a valid code on the message!
validCode=true;
}
} //Now, go DECODE IT!
if(validCode){
decodeMSG;
}
else{
sendSMS("Error! Codigo no verificado!"); //Error message as the SMS did not contiain a valid code
}
}
void deleteALL() {
sim908.println("AT+CMGD=1,4"); //deletes all SMS
delay(1000);
}
void sendSMS(char message[20]) {
strcpy(msg2send,message);
sim908.println("AT+CMGF=1"); //sets to text mode
delay(1000);
sim908.println("AT+CMGS=\"+YOUR_PHONE_NUMBER\""); //sets phone number to send SMS
delay(1000);
sim908.print(msg2send); //prints message to SIM908
delay(1000);
sim908.write(26); // (ascii)Ctrl+Z sends message
deleteALL();
}
void decodeMSG(){ //decode what SMS contains and return answer form module
//inicioCodigo+5 es donde comienza el codigo, despues de "P207 " donde P es la posicion 0;
if(inData.substring(inicioCodigo+5,inicioCodigo+10)=="alarma"){
sendSMS("Alarma ON"); //instruction to execute if keyword is "alarma"
}else if(inData.substring(inicioCodigo+5,inicioCodigo+13)=="ubicacion"){
sendSMS("Ubicacion");//instruction to execute if keyword is "ubicacion"
}else if(inData.substring(inicioCodigo+5,inicioCodigo+10)=="apagar"){
sendSMS("Apagado");//instruction to execute if keyword is "alarma"
} else{ //default, this is the set of intruction to execute if there is no a valid code!
sendSMS("Error! Codigo desconocido!"); //Error, unkown instruction
}
validCode=false;
}
void updateLocation(){ //update location routine
int i=0;
for(i=0;i<=15;i++){
last_latitude[i]=latitude[i];
last_longitude[i]=longitude[i];
}
read_GPS();
delay(2000);
i=0;
for(i=0;i<15;i++){
if(last_latitude[i] == latitude[i] && last_longitude[i] == longitude[i]) //if its not the same location, update!
{
iguales = true;
}
else{
iguales = false;
break;
}
}
if(iguales==false){
send_GPRS();
delay(5000);
}
}
void timerISR(){
if(interruptCounter>60){ //If a minute has passed
updateLocation();
interruptCounter=0;
}else{
interruptCounter++;
}
}