GPS Tracker code troubleshooting

Hello,

I am trying to build a GPS Tracker (this one exactly: http://www.instructables.com/id/Athena-The-Global-Car-Tracking-System/step6/Programming-the-Arduino/).

Long story short, it did not work. I used the code from Seed Studio website to see if receives any messages:

//Serial Relay - Arduino will patch a
//serial link between the computer and the GPRS Shield
//at 19200 bps 8-N-1
//Computer is connected to Hardware UART
//GPRS Shield is connected to the Software UART

#include <SoftwareSerial.h>

SoftwareSerial GPRS(7, 8);
unsigned char buffer[64]; // buffer array for data recieve over serial port
int count=0; // counter for buffer array
void setup()
{
GPRS.begin(19200); // the GPRS baud rate
Serial.begin(19200); // the Serial port of Arduino baud rate.

}

void loop()
{
if (GPRS.available()) // if date is comming from softwareserial port ==> data is comming from gprs shield
{
while(GPRS.available()) // reading data into char array
{
buffer[count++]=GPRS.read(); // writing data into array
if(count == 64)break;
}
Serial.write(buffer,count); // if no data transmission ends, write buffer to hardware serial port
clearBufferArray(); // call clearBufferArray function to clear the storaged data from the array
count = 0; // set counter of while loop to zero

}
if (Serial.available()) // if data is available on hardwareserial port ==> data is comming from PC or notebook
GPRS.write(Serial.read()); // write it to the GPRS shield
}
void clearBufferArray() // function to clear buffer array
{
for (int i=0; i<count;i++)
{ buffer*=NULL;} // clear all index of array with command NULL*
}
And it worked wondefully. It received the text message and printed it in serial monitor, along with date/time and phone number it came from. Then, I used this code to see if it can send text messages:
#include <SoftwareSerial.h>

SoftwareSerial gprsSerial(7,8);

void setup()
{

  • gprsSerial.begin(19200); // GPRS shield baud rate*
  • Serial.begin(19200); *
  • delay(500);*
    }

void loop()
{

  • if (Serial.available()) // if there is incoming serial data*

  • switch(Serial.read()) // read the character*

  • {*

  • case 't': // if the character is 't'*

  • SendTextMessage(); // send the text message*

  • break;*

  • case 'd': // if the character is 'd'*

  • DialVoiceCall(); // dial a number*

  • break;*

  • }*

  • if (gprsSerial.available()){ // if the shield has something to say*

  • Serial.write(gprsSerial.read()); // display the output of the shield*

  • }*
    }

/*
* Name: SendTextMessage
* Description: Send a text message to a number
*/
void SendTextMessage()
{

  • Serial.println("Sending Text...");*
  • gprsSerial.print("AT+CMGF=1\r"); // Set the shield to SMS mode*
  • delay(100);*
  • // send sms message, the phone number needs to include the country code e.g. if a U.S. phone number such as (540) 898-5543 then the string must be:*
  • // +15408985543*
  • gprsSerial.println("AT+CMGS = "+xxxxxxxxxx"");*
  • delay(100);*
  • gprsSerial.println("How are you today?"); //the content of the message*
  • delay(100);*
  • gprsSerial.print((char)26);//the ASCII code of the ctrl+z is 26 (required according to the datasheet)*
  • delay(100);*
  • gprsSerial.println();*
  • Serial.println("Text Sent.");*
    }

/*
* Name: DialVoiceCall()
* Description: Can call/dial a phone number
*/
void DialVoiceCall()
{

  • gprsSerial.println("ATD+xxxxxxxxxx;");//dial the number, must include country code*
  • delay(100);*
  • gprsSerial.println();*
    }
    And again, no issues either. I typed "t" in serial monitor and it received a text message right away. So then I compiled a code just to see if it would send me a text message upon receiving an SMS with specific word in it and that's when I ran into a problem and I was able to identify it, but I can't fix it. When I left the boxed lines of code uncommented, the serial monitor was dead - it did not print anything. However, when I commented the line that would make go into Cmd_Read_Act() function, that's when it stopped responding. It would not print anything, not even the "password authenticated", and it would not send me back a text message at all.
    How can I properly pull the content of the SMS and store it in a variable so it can be used later in the program?
    Thanks

Please always use code tags(</> button on the toolbar) when you post code or error/warning messages on the forum. If you look at your post you'll see it was messed up because parts of your code were interpreted as smilies or forum formatting tags.

dchopek1:
So then I compiled a code just to see if it would send me a text message upon receiving an SMS with specific word in it and that's when I ran into a problem

Post the code that didn't work.

I tried the following code just to see if I will get a reply based on the text message received by the GPRS board:

//Serial Relay - Arduino will patch a 
//serial link between the computer and the GPRS Shield
//at 19200 bps 8-N-1
//Computer is connected to Hardware UART
//GPRS Shield is connected to the Software UART 
 
#include <SoftwareSerial.h>
 
SoftwareSerial GPRS(7, 8);
unsigned char buffer[64]; // buffer array for data recieve over serial port
int count=0;     // counter for buffer array 
void setup()
{
  GPRS.begin(19200);               // the GPRS baud rate   
  Serial.begin(19200);             // the Serial port of Arduino baud rate.
 
}
 
void loop()
{
  if (GPRS.available())              // if date is comming from softwareserial port ==> data is comming from gprs shield
  {
    while(GPRS.available())          // reading data into char array 
    {
      buffer[count++]=GPRS.read();     // writing data into array
      if(count == 64)break;
  }
    Serial.write(buffer,count);            // if no data transmission ends, write buffer to hardware serial port
    Cmd_Read_Act();
    clearBufferArray();              // call clearBufferArray function to clear the storaged data from the array
    count = 0;                       // set counter of while loop to zero
  }
  if (Serial.available())            // if data is available on hardwareserial port ==> data is comming from PC or notebook
    GPRS.write(Serial.read());       // write it to the GPRS shield
}
void clearBufferArray()              // function to clear buffer array
{
  for (int i=0; i<count;i++)
    { buffer[i]=NULL;}                  // clear all index of array with command NULL
}

void Cmd_Read_Act()                       //Function reads the SMS sent to SIM900 shield.
{ 
  char buffer2[64];
  for (int i=0; i<count;i )
  { buffer2[i]=char(buffer[i]);}  
    
  if (strstr(buffer2,"test"))    //Comparing password entered with password stored in program  
  {
      Serial.println("Password Authenticated.");
      Serial.println("Sending reply SMS. ");
      SendTextMessage();            
  }
  
}
void SendTextMessage()
{
  
  
GPRS.print("AT CMGF=1\r");    //Sending the SMS in text mode
delay(100);
GPRS.println("AT CMGS = \"my phone number here\"");//The predefined phone number
delay(100);
GPRS.println("SMS received");//the content of the message
delay(100);
GPRS.println((char)26);//the ASCII code of the ctrl z is 26
delay(100);
GPRS.println();
}

Now, if I put it in as is, nothing happens, not a single print in serial monitor. If I comment the Cmd_Read_Act() inside the void loop, the shield starts to print what came in in serial monitor (message content, phone number it came from, date+time).

dchopek1:

  for (int i=0; i<count;i )

That's just going to loop forever. Should be:

 for (int i=0; i<count; i++)

Please see https://www.arduino.cc/en/Reference/For

It works!! Thank you!!!

Another question: is it possible to send the SMS to the phone number the SMS came from?

So I updated the original code with the "++" signs where advised:

//Program written for Global Car Tracking System by Jayvis Gonsalves
#include <SoftwareSerial.h>
#include <string.h> 
#include <TinyGPS.h>
SoftwareSerial Sim900Serial(7, 8);
byte buffer[64];                             // buffer array for data recieve over serial port
int count=0;                                 // counter for buffer array
SoftwareSerial GPS(2, 3);
TinyGPS gps;
unsigned long fix_age;
long lat, lon;
float LAT, LON;
void gpsdump(TinyGPS &gps);
bool feedgps();
void getGPS();
void setup()
{
  Sim900Serial.begin(19200);     // the SIM900 baud rate
  GPS.begin(9600);                   // GPS module baud rate  
  Serial.begin(19200);                  // the Serial port of Arduino baud rate.
  delay(500);
  Sim900_Inti();
}
 
void loop()
{
  Sim900Serial.listen();
  if (Sim900Serial.available())                     // If date is comming from from GSM shield)
  {
    while(Sim900Serial.available())              // reading data into char array 
    {
      buffer[count++]=Sim900Serial.read();  // writing data into array
      if(count == 64)break;
     }
    Serial.write(buffer,count);                     // if no data transmission ends, write buffer to hardware serial port
    Cmd_Read_Act();                               //Read the 'COMMAND' sent to SIM900 through SMS
    clearBufferArray();                               // call clearBufferArray function to clear the storaged data from the array
    count = 0;                                          // set counter of while loop to zero
 
 
  }
  if (Serial.available())                       // if data is available on hardwareserial port ==> data is comming from PC or notebook
    Sim900Serial.println(Serial.read());       // write it to the GPRS shield
}
void clearBufferArray()                             // function to clear buffer array
{
  for (int i=0; i<count;i++)
    { buffer[i]=NULL;}                                // clear all index of array with command NULL
}
void Sim900_Inti(void)
{
  Sim900Serial.println("AT+CMGF=1");    // Set GSM shield to sms mode
  Serial.println("AT+CMGF=1");
  delay(500);
  Sim900Serial.println("AT+CNMI=2,2,0,0,0");
  Serial.println("AT+CNMI=2,2,0,0,0");
  delay(500);
}
void Cmd_Read_Act(void)                       //Function reads the SMS sent to SIM900 shield.
{ 
  char buffer2[64];
  for (int i=0; i<count;i++)
  { buffer2[i]=char(buffer[i]);}  
    
  if (strstr(buffer2,"wuzza"))    //Comparing password entered with password stored in program  
  {
      Serial.println("Password Authenticated.");
      Serial.println("Sending reply SMS. ");
      SendTextMessage();            
  }
  
}
void SendTextMessage()
{
  
  
Sim900Serial.print("AT+CMGF=1\r");    //Sending the SMS in text mode
delay(100);
Sim900Serial.println("AT+CMGS = \"+xxxx\"");//The predefined phone number
delay(100);
Sim900Serial.println("Please wait while Module calculates position");//the content of the message
delay(100);
Sim900Serial.println((char)26);//the ASCII code of the ctrl z is 26
delay(100);
Sim900Serial.println();
delay(5000);
int counter=0;
GPS.listen();


for (;;)
{
   long lat, lon;
   unsigned long fix_age, time, date, speed, course;
   unsigned long chars;
   unsigned short sentences, failed_checksum;
   long Latitude, Longitude;
    
   // retrieves /- lat/long in 100000ths of a degree
   gps.get_position(&lat, &lon, &fix_age);
   getGPS();
   Serial.print("Latitude : ");
   Serial.print(LAT/1000000,7);
   Serial.print(" :: Longitude : ");
   Serial.println(LON/1000000,7);
   if (LAT == 0 && LON == 0)
  {
    continue;    
  } 
  counter=0 ;
  if (counter<30)
  {
    continue;    
  }
  
  Sim900Serial.print("AT+CMGF=1\r");    //Sending the SMS in text mode
  delay(100);
  Sim900Serial.println("AT+CMGS = \"+xxxx\"");//The predefined phone number
  delay(100);
  Sim900Serial.print("Latitude : ");
  Sim900Serial.print(LAT/1000000,7);
  Sim900Serial.print(" :: Longitude : ");
  Sim900Serial.println(LON/1000000,7);//the content of the message
  delay(100);
  Sim900Serial.println((char)26);//the ASCII code of the ctrl z is 26
  delay(5000);
  Sim900Serial.println();
  counter=0;
  break;
 }      
}

void getGPS()
{
   bool newdata = false;
   unsigned long start = millis();
   while (millis() - start < 1000)
   {
      if (feedgps ())
      {
         newdata = true;
       }
     }
     if (newdata)
   {
      gpsdump(gps);
    }
}
bool feedgps()
{
   while (GPS.available())
 {
      if (gps.encode(GPS.read()))
        return true;
      }return 0;
}
void gpsdump(TinyGPS &gps)
{
   gps.get_position(&lat, &lon);
   LAT = lat;
   LON = lon;
   {
      feedgps(); 
    }
  }

I sent the text message to my the shield, and it did print the content. Then, it printed "password authneticated" and it sent me back an SMS with "Please wait while...."

Then, instead of getting the data off the GPS and loading it into a message, it kept printing the coordinates in serial monitor over and over again, instead of sending me back a text message with the coordinates. What's wrong?

dchopek1:

  counter=0 ;

if (counter<30)
 {
   continue;  
 }

This will always continue because you just set counter to 0 so this will cause an endless for loop(continue just skips the rest of the for loop and starts the next loop). If you explain what you are trying to accomplish here I might be able to advise you. Adding some more comments to code would be a good idea.

Please always use Tools > Auto Format on your code before posting it to the forum. This makes it easier for us to read. In fact, I recommend always Auto Formatting all your code because the automatic indentation makes it easier to spot bugs.

//Program written for Global Car Tracking System by Jayvis Gonsalves
#include <SoftwareSerial.h>
#include <string.h>
#include <TinyGPS.h>
SoftwareSerial Sim900Serial(7, 8);
byte buffer[64];                             // buffer array for data recieve over serial port
int count = 0;                               // counter for buffer array
SoftwareSerial GPS(2, 3);
TinyGPS gps;
unsigned long fix_age;
long lat, lon;
float LAT, LON;
void gpsdump(TinyGPS &gps);
bool feedgps();
void getGPS();
void setup()
{
  Sim900Serial.begin(19200);     // the SIM900 baud rate
  GPS.begin(9600);                   // GPS module baud rate
  Serial.begin(19200);                  // the Serial port of Arduino baud rate.
  delay(500);
  Sim900_Inti();
}

void loop()
{
  Sim900Serial.listen();
  if (Sim900Serial.available())                     // If date is comming from from GSM shield)
  {
    while (Sim900Serial.available())             // reading data into char array
    {
      buffer[count++] = Sim900Serial.read(); // writing data into array
      if (count == 64)break;
    }
    Serial.write(buffer, count);                    // if no data transmission ends, write buffer to hardware serial port
    Cmd_Read_Act();                               //Read the 'COMMAND' sent to SIM900 through SMS
    clearBufferArray();                               // call clearBufferArray function to clear the storaged data from the array
    count = 0;                                          // set counter of while loop to zero


  }
  if (Serial.available())                       // if data is available on hardwareserial port ==> data is comming from PC or notebook
    Sim900Serial.println(Serial.read());       // write it to the GPRS shield
}
void clearBufferArray()                             // function to clear buffer array
{
  for (int i = 0; i < count; i++)
  {
    buffer[i] = NULL; // clear all index of array with command NULL
  }
}
void Sim900_Inti(void)
{
  Sim900Serial.println("AT+CMGF=1");    // Set GSM shield to sms mode
  Serial.println("AT CMGF=1");
  delay(500);
  Sim900Serial.println("AT+CNMI=2,2,0,0,0");
  Serial.println("AT CMGF=1");
  delay(500);
}
void Cmd_Read_Act(void)                       //Function reads the SMS sent to SIM900 shield.
{
  char buffer2[64];
  for (int i = 0; i < count; i++)
  {
    buffer2[i] = char(buffer[i]);
  }

  if (strstr(buffer2, "location"))   //Comparing password entered with password stored in program
  {
    Serial.println("Password Authenticated.");
    Serial.println("Sending reply SMS. ");
    SendTextMessage();
  }

}
void SendTextMessage()
{


  Sim900Serial.print("AT+CMGF=1\r");    //Sending the SMS in text mode
  delay(100);
  Sim900Serial.println("AT+CMGS = \"+18473371610\"");//The predefined phone number
  delay(100);
  Sim900Serial.println("Yoo");//the content of the message
  delay(100);
  Sim900Serial.println((char)26);//the ASCII code of the ctrl z is 26
  delay(100);
  Sim900Serial.println();
  delay(100);
  int counter = 0;
  GPS.listen();


  for (;;)
  {
    long lat, lon;
    unsigned long fix_age, time, date, speed, course;
    unsigned long chars;
    unsigned short sentences, failed_checksum;
    long Latitude, Longitude;

    // retrieves /- lat/long in 100000ths of a degree
    gps.get_position(&lat, &lon, &fix_age);
    getGPS();
    Serial.print("Latitude : ");
    Serial.print(LAT / 1000000, 7);
    Serial.print(" :: Longitude : ");
    Serial.println(LON / 1000000, 7);
    if (LAT == 0 && LON == 0)
    {
      continue;
    }
    counter;
    if (counter < 30)
    {
      continue;
    }

    Sim900Serial.print("AT+CMGF=1\r");    //Sending the SMS in text mode
    delay(100);
    Sim900Serial.println("AT+CMGS = \"+18473371610\"");//The predefined phone number
    delay(100);
    Sim900Serial.print("Latitude : ");
    Sim900Serial.print(LAT / 1000000, 7);
    Sim900Serial.print(" :: Longitude : ");
    Sim900Serial.println(LON / 1000000, 7); //the content of the message
    delay(100);
    Sim900Serial.println((char)26);//the ASCII code of the ctrl z is 26
    delay(100);
    Sim900Serial.println();
    counter = 0;
    break;
  }
}

void getGPS()
{
  bool newdata = false;
  unsigned long start = millis();
  while (millis() - start < 1000)
  {
    if (feedgps ())
    {
      newdata = true;
    }
  }
  if (newdata)
  {
    gpsdump(gps);
  }
}
bool feedgps()
{
  while (GPS.available())
  {
    if (gps.encode(GPS.read()))
      return true;
  } return 0;
}
void gpsdump(TinyGPS &gps)
{
  gps.get_position(&lat, &lon);
  LAT = lat;
  LON = lon;
  {
    feedgps();
  }
}

I am trying to get this to work: http://www.instructables.com/id/Athena-The-Global-Car-Tracking-System/step6/Programming-the-Arduino/.

So what happens is, the arduino is listening to the shield for incoming data. When it receives a text message with a key word, it is supposed to pull coordinates from GPS and text it back to me.

Your previous tip helped solve the problem with receiving messages. Now what happens is when I send it the right word, it replies me a text message "Yoo" to let me know that the shield received my SMS. Then it should pull any data and send it back to me, but instead it just prints coordinates in the monitor and doesn't send them back. See attached picture. I know it is supposed to skip the loop and restart it when LAT =0 & LONG==0, but yesterday I had a lock with coordinates and it still wouldn't reply.

Another thing I just discovered - I just sent it various text messages, different from the password, and it replied me a text message...

dchopek1:

    counter;

if (counter < 30)
    {
      continue;
    }

The line

    counter;

does nothing. I read in a comment of that instructable from the author:

The problem is that every time I upload the code to this webpage a few + operators are automatically removed out from the code, hence you'll might be experiencing an error when trying to use it in your project.

So this makes me think that line should be changed to

    counter++;

That will cause it to get 30 non-zero LAT/LONG values before continuing with the rest of the code in the for loop and exiting the for loop. Why is that necessary? I have no clue. I also don't understand why there's a call to gps.get_position() in that for loop(line 104) that seems to have no purpose. Maybe there is a good reason for all of that, I haven't taken a lot of time to look into the code and the TinyGPS library.

You really have to be wary of anything you find on Instructables, for some reason the vast majority of the projects on there are complete garbage.

Thanks for your inputs. I will try those codes later.

I had a serious romance with MATLAB in college, but that was a while ago. I ordered myself C+ for dummies to get the basics down and teach myself a little bit of C so I don't have to rely on instructables as much.

Thanks again!