Arduino GPS/GPRS real time tracker

Hi, there i’m new to arduino forum, and this is my first post.
So to the case, i’m trying to make a real time GPS tracker. The princip of the project is to read GPS coordinates, store them and then send them through GPRS, to my server (database).
I’m relatively new to arduino.
So here is the problem on my arduino UNO i have a shield with SIM900 and its connected through the serial connection pin 2,3. In addition to that i have a GPS module that is connected to pins 4,5. I have found a couple of examples of codes and i have tested it and everything worked fine separately. The problem occured when i was trying to fuse the two codes together, and all of a sudden nothing was working. Right now im lost and have no idea what to do,… I’d be very thankful if someone could help me in any way posible.

This is the code of GPS module

// Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Ultimate GPS module
// using MTK33x9 chipset
//    ------> http://www.adafruit.com/products/746
// Pick one up today at the Adafruit electronics shop 
// and help support open source hardware & software! -ada

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>

// If you're using a GPS module:
// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// If using software serial (sketch example default):
//   Connect the GPS TX (transmit) pin to Digital 3
//   Connect the GPS RX (receive) pin to Digital 2
// If using hardware serial (e.g. Arduino Mega):
//   Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
//   Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3

// If you're using the Adafruit GPS shield, change 
// SoftwareSerial mySerial(3, 2); -> SoftwareSerial mySerial(8, 7);
// and make sure the switch is set to SoftSerial

// If using software serial, keep these lines enabled
// (you can change the pin numbers to match your wiring):
SoftwareSerial mySerial(5, 4);

Adafruit_GPS GPS(&mySerial);
// If using hardware serial (e.g. Arduino Mega), comment
// out the above six lines and enable this line instead:
//Adafruit_GPS GPS(&Serial1);


// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences. 
#define GPSECHO  false

// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy


void setup()  
{
     
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);
  Serial.println("Adafruit GPS library basic test!");

  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
  
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time
  
  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_NOANTENNA);

  // the nice thing about this code is you can have a timer0 interrupt go off
  // every 1 millisecond, and read data from the GPS for you. that makes the
  // loop code a heck of a lot easier!
  useInterrupt(true);

  delay(1000);
  // Ask for firmware version
  mySerial.println(PMTK_Q_RELEASE);
}


// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
#ifdef UDR0
  if (GPSECHO)
    if (c) UDR0 = c;  
    // writing direct to UDR0 is much much faster than Serial.print 
    // but only one character can be written at a time. 
#endif
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
}

uint32_t timer = millis();

void loop()                     // run over and over again
{
  
  
  
 
  // in case you are not using the interrupt above, you'll
  // need to 'hand query' the GPS, not suggested :(
  if (! usingInterrupt) {
    // read data from the GPS in the 'main loop'
    char c = GPS.read();
    // if you want to debug, this is a good time to do it!
    if (GPSECHO)
      if (c) Serial.print(c);
  }
  
  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences! 
    // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
    //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false
  
    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }

  // if millis() or timer wraps around, we'll just reset it
  if (timer > millis())  timer = millis();

  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) { 
    timer = millis(); // reset the timer
    
    Serial.print("\nTime: ");
    Serial.print(GPS.hour, DEC); Serial.print(':');
    Serial.print(GPS.minute, DEC); Serial.print(':');
    Serial.print(GPS.seconds, DEC); Serial.print('.');
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC); Serial.print('/');
    Serial.print(GPS.month, DEC); Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); Serial.print((int)GPS.fix);
    Serial.print(" quality: "); Serial.println((int)GPS.fixquality); 
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.println(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", "); 
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
      
      Serial.print("Speed (knots): "); Serial.println(GPS.speed);
      Serial.print("Angle: "); Serial.println(GPS.angle);
      Serial.print("Altitude: "); Serial.println(GPS.altitude);
      Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
      
      
    }
  }
}

Here is the code from GPRS module

#include "SIM900.h"
 
#include <SoftwareSerial.h>
 
#include "inetGSM.h"
 
InetGSM inet;


#include <Adafruit_GPS.h>


SoftwareSerial mySerial(2, 3);

//Adafruit_GPS GPS(&mySerial);


 
int k=0;
 
int j=0;
 
char msg[150];
 
boolean found=false;
 
char data;
 
int numdata;
 
char inSerial[50];
 
int i=0;
 
boolean started=false;


int  lat=1, lon=2,angle=3,altitud=4, sped=5, no_sat=8,time =8; 

char params[80]; 

 
void setup()
 
{
  pinMode(7, OUTPUT);
  
 snprintf(params, sizeof(params), "lat=%d&lon=%d&angle=%d&altitud=%d&sped=%d&no_sat=%d&time=%d", lat, lon, angle,altitud,sped,no_sat,time);
 
 
Serial.begin(9600);
 
Serial.println("GSM Shield testing.");
 
if (gsm.begin(2400)){
 
  
  Serial.println("\nstatus=READY");
 
  started=true;  
 
 
}
 
else Serial.println("\nstatus=IDLE");
 
if(started){
 
  if (inet.attachGPRS("internet.wind", "", ""))
 
    Serial.println("status=ATTACHED");
 
  else Serial.println("status=ERROR");
 
  delay(1000);
 
 numdata=inet.httpPOST("my.ip.add.res", 80, "/gps_data1.php", params ,msg, 50);
 
 
}

};

void loop()
 
{
 
 
}

Once again if anyone could help me i would wery appreciate that.

And here is my “fused code”

#include "SIM900.h"
 
#include <SoftwareSerial.h>
 
#include "inetGSM.h"

InetGSM inet;

#include <Adafruit_GPS.h>

SoftwareSerial mySerial(5, 4);

Adafruit_GPS GPS(&mySerial);




#define GPSECHO  false


boolean usingInterrupt = false;
void useInterrupt(boolean); 


 int k=0;
 
int j=0;
 
char msg[150];
 
boolean found=false;
 
char data;
 
int numdata;
 
char inSerial[50];
 
int i=0;
 
boolean started=false;
 

float latitude;
char lat;
int st_SAT;


int gps_data_send ()
 {
   
   
   
   int  lat=1, lon=2,angle=3,altitud=4, sped=5, no_sat=8,time =7; 


char params[80];
   
   
  snprintf(params, sizeof(params), "lat=%d&lon=%d&angle=%d&altitud=%d&sped=%d&no_sat=%d&time=%d", lat, lon, angle,altitud,sped,no_sat,time);
 
 
Serial.begin(9600);
 
Serial.println("GSM Shield testing.");
 
if (gsm.begin(2400)){
 
  
  Serial.println("\nstatus=READY");
 
  started=true;  
 
 
}
 
else Serial.println("\nstatus=IDLE");
 
if(started){
 
   if (inet.attachGPRS("internet.wind", "", ""))
 
      Serial.println("status=ATTACHED");
 
      else Serial.println("status=ERROR");
 
      delay(1000);
 
     numdata=inet.httpPOST("my.ip.add.res", 80, "/gps_data1.php", params ,msg, 50);
   
 
      }

   }


//////////////////////////////////////////////////////////////////////////////////////////////// 
void setup()
 
{
   Serial.begin(9600);
    
  //pinMode(6, OUTPUT);
 
 
};

uint32_t timer = millis();


///////////////////////////////////////////////////////////////////////////////////////////////

 void gps_data_get1()
 {
   
  
  
  Serial.println("Adafruit GPS library basic test!");

  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);
  
  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time
  
  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  // the nice thing about this code is you can have a timer0 interrupt go off
  // every 1 millisecond, and read data from the GPS for you. that makes the
  // loop code a heck of a lot easier!
  useInterrupt(true);

  delay(1000);
  // Ask for firmware version
  mySerial.println(PMTK_Q_RELEASE);
}


// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
 
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
#ifdef UDR0
  if (GPSECHO)
    if (c) UDR0 = c;  
    // writing direct to UDR0 is much much faster than Serial.print 
    // but only one character can be written at a time. 
#endif
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }


   
 }

//////////////////////////////////////////////////////////////////////////////////////////////
void gps_data_get ()
{
  
  // in case you are not using the interrupt above, you'll
  // need to 'hand query' the GPS, not suggested :(
  if (! usingInterrupt) {
    
    // read data from the GPS in the 'main loop'
    char c = GPS.read();
    // if you want to debug, this is a good time to do it!
    if (GPSECHO)
      if (c) Serial.print(c);
  }
  
  // if a sentence is received, we can check the checksum, parse it...
 
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences! 
    // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
    //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false
  
    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }




  // if millis() or timer wraps around, we'll just reset it
  if (timer > millis())  timer = millis();
  
  
  
  
  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) { 
    timer = millis(); // reset the timer
    
    
    Serial.print("\nTime: ");
    Serial.print(GPS.hour, DEC); Serial.print(':');
    Serial.print(GPS.minute, DEC); Serial.print(':');
    Serial.print(GPS.seconds, DEC); Serial.print('.');
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC); Serial.print('/');
    Serial.print(GPS.month, DEC); Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); Serial.print((int)GPS.fix);
    Serial.print(" quality: "); Serial.println((int)GPS.fixquality); 
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.println(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", "); 
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
      Serial.print("Speed (knots): "); Serial.println(GPS.speed);
      Serial.print("Angle: "); Serial.println(GPS.angle);
      Serial.print("Altitude: "); Serial.println(GPS.altitude);
      Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
      
       
          
    }

  }
  
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
 
{

  gps_data_get1();
  gps_data_get(); 
  gps_data_send();
  
 
 
}

So if i comment the gps_data_get1(); and gps_data_get(); the sending works fine. Bit if i comment gps_data_send(); and uncomment the gps_data_get1(); and gps_data_get(); the program is working but in a strange way .

The problem occured when i was trying to fuse the two codes together, and all of a sudden nothing was working.

Do yourself a big favor. Stop trying to do this project on a Uno.

Only ONE instance of SoftwareSerial can be active at a time. If you are listening to the GPS, you can not be talking to the GPRS. if you are listening to the GPRS, you can not be listening to the GPS.

With a Mega, with 4 hardware serial ports, you could.

Thaks for the advice, bout the thing is that i don’t need to listen to both (GPS and GPRS) at the same time. In pricip i would like the program to work like this.

  1. Start

  2. initialization of th GPS

  3. Gps power on through enable pin (HIGH)

  4. Waiting for the fix position

  5. when fix = 1, store the data in variables.

  6. power off the GPS. through enable pin (LOW)

  7. SIM 900 power up through digital pin pulldown < 1 S

  8. Initialization of GPRS module

  9. connect to mobile internet

  10. Read stored data from GPS

  11. send data to server

  12. SIM 900 power down pin pulldown > 1 S

  13. END

So at the time i am comunicating with only one. I would wery much like it if i could achive that whit an UNO.

  useInterrupt(true);

Which means that this function:

// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
 
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
#ifdef UDR0
  if (GPSECHO)
    if (c) UDR0 = c;  
    // writing direct to UDR0 is much much faster than Serial.print 
    // but only one character can be written at a time. 
#endif
}

will be called periodically. Now, lets assume that you are not debugging. That code then boils down to:

// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
   char c = GPS.read();
}

which means that the comment is wrong. The ISR does NOT store any data anywhere. The character that it read from the GPS is simply discarded.

If you are not using interrupts, you have this equally useless code:

  if (! usingInterrupt) {
    
    // read data from the GPS in the 'main loop'
    char c = GPS.read();
    // if you want to debug, this is a good time to do it!
    if (GPSECHO)
      if (c) Serial.print(c);
  }

If you are not echoing data, that boils down to:

  if (! usingInterrupt) {
    // read data from the GPS in the 'main loop'
    char c = GPS.read();
  }

where, again, the data read is simply discarded.

It hardly seems useful to have even connected the GPS.

You could, in theory, use one serial port to talk to two different devices at different times.

But you would need to ensure that the required electrical characteristics of the interconnection were met. This is not straightforward and probably beyond your capabilities.

OK obviusly i have strated the conversation on the wrong foot. With my last post i was trying to explain how i would like my program to work, and not how the program is written now. For now i would just like to see, that first the GPS part of program goes through and then the GPRS part, written in one code. The fused version is just the above 2 (1.st post) programs slammed together. I know that in the above code there are part's of code written that i don't use but they work(at least seperatly)!

So,.. the electrical part:

pin (2,3) arduino => connected to tx, rx SIM900 (GPRS module) pin (5 ,4)arduino => connected to tx, rx GPS pin (6) arduino => connected to enable GPS (high or floating active) pin (7) arduino=> connected to powering on/of SIM900

Both the GPS and GPRS are connected to power, wih addition the GPRS has additional power suply .

If anyone can hep me i would appreciate that.

Or another pin for a CD4052 and simply switch between the two serial data streams.. I'd be more worried that the relatively wimpy Uno power supply was adequate to the task of supporting the GPRS module.. I'd also ditch the Adafruit code because that interrupt will occur during any GPRS communications. the whole GPS data stream repeats every second... So I probably would choose the TinyGPS++ library by Mikal Hart instead. I've used it many times and it just works. In comparison the Adafruit example is rather clunky.. At best, Great for n00bs? but a big time hog as it decodes all the GPS sentences where two (GGA and GRMC? not sure of the last but it has all the right letters... Perhaps one more for velocity?, I've never needed to use that information so I never looked into it and this then leaves you about 700 - 800 mS for your own stuff. just a thought.. My only reservation about the CD4052 is the available drive current considering the possibility of using Long wires on either of the two switches (transmission gates, bilateral) as they are 400 to 500 ohm resistors when on and open when off. There are several data selectors that are or can easily be made to be that are digital and capable of more drive... The reason for presenting this was to point to some alternate design methods rather than say it can't be done. Even gating the GPS off is not an issue for the time in sending a GPRS message unless you are trying to do real time tracking rather than periodic measurements is easily available W/O disturbing the location data stream for very long at all, If any..

Doc

Ok so i have found the biggest problem to my communucation fault. it's called RAM. The Uno has only 2KB wich is not enough. I have made some changes to the code and everything run's on a MEGA withot a problem. As soon a s i get done wih the code i will post it. Ive bumped up into some new poblems, so my next problem is folowing.

float lat, lon, angle,altitud, sped, no_sat, time; 

char params[80]; 

void setup()
{
  Serial.begin(9600); 

Serial.print(params);

  
}


void loop()
{
  
   snprintf(params, sizeof(params), "lat=%d&lon=%d&angle=%d&altitud=%d&sped=%d&no_sat=%d&time=%d", lat, lon, angle,altitud,sped,no_sat,time);
  
   Serial.println(params);

}

how can i use the function dtostrf in the same way as the snprintf. The problem is that all of my values are type float. And i need to send them as char.

how can i use the function dtostrf in the same way as the snprintf.

You can't. You CAN use dtostrf() as a replacement for snprintf() for the purposes of converting a float to a string. I seem to recall that google finds an example or 14 bazillion.

Ok so i have found the biggest problem to my communucation fault. it's called RAM.

No. It is your misuse of SRAM that is the problem. There is NO difference to the receiving end between printing one long string of text and printing many short strings. There IS a big difference in the amount of memory needed to output the data the two different ways.