Hi folks! I want to share my code in this community. This is a tracker with Arduino Duemilanove, GSM/GPRS Shield with SIM900 module by Open Electronics and a generic C3-370C GPS module.
Hope this helps someone!.
Greetings from Argentina.
![]()
Add “listen” method in GSM class.
add to gsm.cpp
void GSM::listen(void)
{
_cell.listen();
}
add to gsm.h
void listen();
//Matias Frith rmfrith@hotmail.com
//31 Dic 2012
//#include "SIM900.h"
#include <SoftwareSerial.h>
#include <string.h>
#include "sms.h"
#include <MemoryFree.h>
SMSGSM sms;
// declare pins and set up the serial port for gps
#define rxPin 2 //rx pin in gps connection
#define txPin 3 //tx pin in gps connection
SoftwareSerial gps = SoftwareSerial(rxPin, txPin);
// gps variables
boolean gps_started=false;
boolean invalid_sentence=true;
int comas;
byte byteGPS = 0;
int i = 0;
int h = 0;
int d = 0;
int dpos = 0;
char sentence[10][10];
char url[100];
String strurl;
boolean returnurl;
char GPS_RMC[100]="";
// gsm variables
String text;
String nro;
byte sms_reply;
byte reply_saldo;
int pos;
boolean gsm_started=false;
char position;
char smsbuffer[100];
char n[12];
void setup(){
Serial.begin(9600);
delay(1000);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
//***STARTING GPS***
Serial.print("Inicializando GPS... ");
gps.begin(9600);
delay(1000);
//SET GPS TO COLD START MODE
//gps.write("$PSRF101,0,0,0,000,0,0,12,4*10");
//delay(1000);
//SET NMEA OPTIONS
gps.write("$PSRF201,NMEA9600,NULL38400,GGA1,GLL0,GSA0,GSV0,RMC1,VTG0,USER0*0D");
returnurl=getURL();
while (!returnurl){
Serial.println("[FAIL]");
Serial.print("Inicializando GPS... ");
delay(5000);
returnurl=getURL();
}
Serial.println("[OK]");
gps_started=true;
delay(1000);
//***STARTING GSM***
Serial.print("Inicializando GSM... ");
while(!gsm.begin(2400)){
Serial.println("[FAIL]");
Serial.print("Inicializando GSM... ");
delay(5000);
}
gsm_started=true;
Serial.println("[OK]");
if((gps_started)&&(gsm_started)){
Serial.println("Sistema inicializado");
} else {
Serial.println("Sistema no inicializado.");
}
};
void loop(){
gps.listen();
returnurl=getURL();
delay(1000);
if (returnurl){
Serial.print(strurl);
gsm.listen();
Serial.print(" RAM:");
Serial.println(freeMemory());
if(gsm_started){
position=sms.IsSMSPresent(SMS_ALL);
if(position){
sms.GetSMS(position,n,smsbuffer,100);
Serial.print("Mensaje recibido de ");
Serial.print(n);
Serial.print(": ");
Serial.println(smsbuffer);
nro=n;
text=smsbuffer;
sms.DeleteSMS(position);
if (nro.indexOf("444")>=0){
sms.SendSMS("+05401164906xxx",smsbuffer);
} else if (nro=="") {
} else {
sms_reply=1;
}
if (text.indexOf("GET POS")>=0){
text=strurl;
sms_reply=1;
} else if (text.indexOf("SALDO")>=0){
saldo();
sms_reply=0;
} else {
//text="No se interpreto el comando.";
sms_reply=0;
}
if (sms_reply) {
sms_reply=0;
text.toCharArray(smsbuffer,100);
Serial.print("Enviando posicion al solicitante: ");
Serial.print(smsbuffer);
sms.SendSMS(n,smsbuffer);
}
}
delay(1000);
}
} else {
Serial.println("Error al obtener coordenadas");
}
}
void saldo(){
sms.SendSMS("444","");
}
boolean getURL(){
byteGPS = 0;
while(1==1){
byteGPS = gps.read();
delay(1);
//Serial.write(byteGPS);
if(byteGPS=='R'){
byteGPS = gps.read();
delay(1);
//Serial.write(byteGPS);
if(byteGPS=='M'){
byteGPS = gps.read();
delay(1);
//Serial.write(byteGPS);
if(byteGPS=='C'){
break;
}
}
}
}
GPS_RMC[0]='
;
GPS_RMC[1]='G';
GPS_RMC[2]='P';
GPS_RMC[3]='R';
GPS_RMC[4]='M';
GPS_RMC[5]='C';
i = 6;
comas=0;
while((byteGPS != '*')&&(i<=62)){
byteGPS = gps.read();
delay(10);
Serial.write(byteGPS);
//si recibo dos comas consecutivas faltan datos, la trama no se tiene en cuenta y se descarta
if(byteGPS == 44) {
comas++;
if (comas > 1){
returnurl=false;
break;
}
} else {
comas=0;
returnurl=true;
}
GPS_RMC[i]=byteGPS;
i++;
// delay(10);
}
if(returnurl){
h = 0;
d = 0;
//Serial.println();
while(h<strlen(GPS_RMC)){
//Serial.write(GPS_RMC[h]);
if(GPS_RMC[h] == 44) {
dpos = 0;
d++;
sentence[d][dpos]='0';
} else {
sentence[d][dpos]=GPS_RMC[h];
dpos++;
}
h++;
}
char g_lng[4]={sentence[5][0],sentence[5][1],sentence[5][2],'\0'};
char m_lng[8]={sentence[5][3],sentence[5][4],'.',sentence[5][6],sentence[5][7],sentence[5][8],sentence[5][9],'\0'};
char g_lat[4]={sentence[3][0],sentence[3][1],'\0'};
char m_lat[8]={sentence[3][2],sentence[3][3],'.',sentence[3][5],sentence[3][6],sentence[3][7],sentence[3][8],'\0'};
//convert array to float
double lngg=atof(g_lng);
double latg=atof(g_lat);
double lngm=atof(m_lng);
double latm=atof(m_lat);
//convert to decimal
double lngmd=lngg+(lngm/60);
double latmd=latg+(latm/60);
//convert float to long
long lngmdl=lngmd100000;
long latmdl=latmd100000;
//convert to array again
char lngmdc[10];
char latmdc[10];
ltoa(lngmdl,lngmdc,10);
ltoa(latmdl,latmdc,10);
//drag 4 decimal characters to the right, and insert the floating point to lat and lng
for(i=6;i>=3;i=i-1){
lngmdc[i]=lngmdc[i-1];
latmdc[i]=latmdc[i-1];
}
lngmdc[2]='.';
latmdc[2]='.';
//convert velocity to float, then to kmh [1 knots = 1.85200 kmh]
double velkmh=atof(sentence[7])*1.85200;
if (velkmh<10){
velkmh=0;
}
char velkmh_c[10];
ltoa(velkmh,velkmh_c,10);
double course=atof(sentence[8]);
char course_c[6];
ltoa(course,course_c,10);
//concatenate results!
strcpy(url, "");
strcpy(url, "Google Maps");
strcat(url, sentence[6]);
strcat(url, lngmdc);
strcat(url, ",");
strcat(url, sentence[4]);
strcat(url, latmdc);
strcat(url, " Spd: ");
strcat(url, velkmh_c);
strcat(url, " Kmh");
strcat(url, " Hdg: ");
strcat(url, course_c);
strcat(url, " Deg");
}
strurl=url;
return returnurl;
}