Problem run and connect SIM900 and EM-406A for Tracking device project

Hello there good people..
My little project is building the tracking device with gps module and gsm shield,
I have some problem that is like can't connecting GPS module and GSM shield together.

This is my code..
I have 2 part of code and i want to combine one each other to one part.
This code just for gsm Shield that is send and recieve the SMS
Original by Matias Fritz

#include "SIM900.h"
#include <SoftwareSerial.h>
#include <string.h>
#include "sms.h"
#include <MemoryFree.h>

SMSGSM sms;

// gsm variables
String text;
String nro;
boolean gsm_started=false;
char position;
char smsbuffer[65];
char n[15];

void setup(){
  powerUpOrDown();
  Serial.begin(9600);
  delay(1000);
  while(!gsm.begin(2400)){
    Serial.println("fail");
    delay(5000);
  }
  gsm_started=true; 
  Serial.println("[OK]");
}
void powerUpOrDown()
{
  pinMode(9, OUTPUT); 
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(9,HIGH);
  delay(2000);
  digitalWrite(9,LOW);
  delay(3000);
}
void loop(){ 
  if(gsm_started){
    position=sms.IsSMSPresent(SMS_ALL);
    if(position){
      sms.GetSMS(position,n,smsbuffer,65);
      Serial.print(F("From: "));
      Serial.println(n);
      Serial.print(F("Message: "));
      Serial.println(smsbuffer);
      nro=n;
      text=smsbuffer;
      sms.DeleteSMS(position);
      Serial.print("ram:");
      Serial.print(freeMemory());
      delay(1000);

      if (text.indexOf("GET POS")>=0){
        text.toCharArray(smsbuffer,65);
        Serial.print(F("to:"));
        Serial.println(n);
        Serial.print(F("Message: "));
        Serial.println(smsbuffer);
        sms.SendSMS(n,smsbuffer);
        Serial.print("ram:");
        Serial.print(freeMemory());
          delay(5000);
      }
    }
  }
}

and this one just for GPS module getting coordinate and speed

#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <MemoryFree.h>

TinyGPS gps;
SoftwareSerial ss(12, 13);

char url[85];
String strurl;

void setup(){
  ss.begin(4800);
  Serial.begin(9600);
}

void loop(){
  bool newData = false;

  for (unsigned long start = millis(); millis() - start < 1000;){
    while (ss.available()){
      char c = ss.read();
      if (gps.encode(c))
        newData = true;
    }
  }

  if (newData){
    float flat, flon;
    char latitude[6];
    char longitude[6];
    char speed[5];

    gps.f_get_position(&flat, &flon);
    dtostrf(flat, 3, 5, latitude);
    strcpy(url, "http://maps.google.com/maps?q=");
    strcat(url, latitude);
    strcat(url, ",");
    dtostrf(flon, 3, 5, longitude);
    strcat(url, longitude);
    float sp = gps.f_speed_kmph();
    dtostrf(sp, 3, 2, speed);
    strcat(url, "#");
    strcat(url, speed);
    strcat(url, "Km/h");
    strurl=url;
    Serial.println(strurl);
    Serial.print(freeMemory());
  }
}

If I run this one by one the code is running very good, But when i try to combine the code on this part

 ss.begin(4800); //commented
  delay(1000);//commented
  powerUpOrDown();
  Serial.begin(9600);
  delay(1000);
  while(!gsm.begin(2400)){
    Serial.println("fail");
    delay(5000);
  }
  gsm_started=true; 
  Serial.println("[OK]");

The sketch not running like first I'm trying one by one that's rolling the text:

ram:916From: 
Message:

Every one second and can't recieve anything..
But if the line commented all running good..

I dont know why it happened..?
Any advice is very helpfull.

I'm trying to write down all of the code in one sketch.
Everything it's OK in "setup" section but in "loop" section the result is "fail" in serial monitor.

#include "SIM900.h"
#include <SoftwareSerial.h>
#include <string.h>
#include "sms.h"
#include <TinyGPS.h>
#include <MemoryFree.h>

SMSGSM sms;
TinyGPS gps;
SoftwareSerial ss(12, 13);

// gsm variables
String text;
String nro;
boolean gsm_started=false;
char position;
char smsbuffer[65];
char n[15];

char url[65];
String strurl;

void setup(){
  ss.begin(4800);
  delay(1000);
  powerUpOrDown();
  Serial.begin(9600);
  delay(1000);
  while(!gsm.begin(2400)){
    Serial.println("fail");
    delay(5000);
  }
  gsm_started=true; 
  Serial.println("[OK]");
}
void powerUpOrDown()
{
  pinMode(9, OUTPUT); 
  digitalWrite(9,LOW);
  delay(1000);
  digitalWrite(9,HIGH);
  delay(2000);
  digitalWrite(9,LOW);
  delay(3000);
}

void loop(){ 
  bool newData = false;

  for (unsigned long start = millis(); millis() - start < 1000;){
    while (ss.available()){
      char c = ss.read();
      if (gps.encode(c))
        newData = true;
    }
  }
  
  if (newData){
    float flat, flon;
    char latitude[6];
    char longitude[6];
    char speed[5];

    gps.f_get_position(&flat, &flon);
    dtostrf(flat, 3, 5, latitude);
    strcpy(url, "http://maps.google.com/maps?q=");
    strcat(url, latitude);
    strcat(url, ",");
    dtostrf(flon, 3, 5, longitude);
    strcat(url, longitude);
    float sp = gps.f_speed_kmph();
    dtostrf(sp, 3, 2, speed);
    strcat(url, "#");
    strcat(url, speed);
    strcat(url, "Km/h");
    strurl=url;
    Serial.println(strurl);
    Serial.print(freeMemory());
  } else {
    Serial.println("fail");
  }
    
  if(gsm_started){
    position=sms.IsSMSPresent(SMS_ALL);
    if(position){
      sms.GetSMS(position,n,smsbuffer,65);
      Serial.print(F("From: "));
      Serial.println(n);
      Serial.print(F("Message: "));
      Serial.println(smsbuffer);
      nro=n;
      text=smsbuffer;
      sms.DeleteSMS(position);
      delay(1000);

      if (text.indexOf("GET POS")>=0){
        text.toCharArray(smsbuffer,65);
        Serial.print(F("to:"));
        Serial.println(n);
        Serial.print(F("Message: "));
        Serial.println(smsbuffer);
        sms.SendSMS(n,smsbuffer);
        Serial.print("ram:");
        Serial.print(freeMemory());
        delay(5000);
      }
    }
  }
}

#Arduino Uno R3
#http://www.famosastudio.com/20-ch-em-406a-sirf-iii-receiver-with-antennaEM-406A GPS
#http://www.famosastudio.com/icomsat-gsm-gprs-shield-v1-1IComSat v1.1 - SIM900 GSM/GPRS Shield

I'm so sorry about my messy code, and for bad code.
Thanks a lot

I have some problem that is like can't connecting GPS module and GSM shield together.

These?

#EM-406A GPS Module
#IComSat v1.1 - SIM900 GSM/GPRS Shield

Your "links" don't work.

Only one instance of SoftwareSerial can listen at a time. Do you want to listen to the GPS or to the phone?

I'm so sorry about the link I have repaired..
20 Channel EM-406A SiRF III Receiver with Antenna - Famosa Studio | Famosa Studio GPS Module
http://www.famosastudio.com/icomsat-gsm-gprs-shield-v1-1 /GSM shield

I want to combine it, how to do like this..?
"listen the GPS first and getting the coordinate and then listen to the incoming message from GSM Module and running continously."

how to do like this..?

You need an Arduino with multiple hardware serial ports, like the Mega. You can not alternately listen to the GPS and phone. You are sure to miss important data.

PaulS:

how to do like this..?

You need an Arduino with multiple hardware serial ports, like the Mega. You can not alternately listen to the GPS and phone. You are sure to miss important data.

If you don't mind this some video that's combine the GPS module and GSM Shields..

I want to do something like that, but I'm have different type of GPS module and GSM Shields.
I have emailed the owner of the video and he is very kind to give a full sketch
here:

//Matias Frith rmfrith@hotmail.com
//31 Dic 2013



#include "SIM900.h"
#include <SoftwareSerial.h>
#include <string.h>
#include "sms.h"
#include <MemoryFree.h>

SMSGSM sms;

// gps constants
#define rxPin 2 //rx pin in gps connection
#define txPin 3 //tx pin in gps connection

// set up the serial port for gps
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);


//setup for gps port
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("Reinicializando GPS... ");
   delay(5000);
   returnurl=getURL();
} 
Serial.println("[OK]");
gps_started=true;  
delay(1000);

Serial.print("Inicializando GSM... ");

  while(!gsm.begin(2400)){
    Serial.println("[FAIL]");
    Serial.print("Reinicializando 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.println(n);
          Serial.print("Mensaje: ");
          Serial.println(smsbuffer);
          nro=n;
          text=smsbuffer;
          sms.DeleteSMS(position);
        
        if (nro.indexOf("444")>=0){
          sms.SendSMS("01164906542",smsbuffer);
        } else if (nro=="") {
        
        } else {
          sms_reply=1;
        }  
     
         if (text.indexOf("GET POS")>=0){
          Serial.println("Obteniendo coordenadas del modulo GPS...");
          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 mensaje a: ");
          Serial.println(n);
          Serial.print("Mensaje: ");
          Serial.println(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]='

I'm very confuse when I try to change methode of getting the GPS coordinate because I use the "TinyGPS" Library while he does not.;
  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<=63)){
        byteGPS = gps.read();
        delay(1);
   
    //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++;
  // Serial.write(byteGPS);
  // 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++;
  }
 
  //Serial.println();

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=latmd
100000;
 
 
  char lngmdc[10];
  char latmdc[10];
 
  //convert to array again
  ltoa(lngmdl,lngmdc,10);
  ltoa(latmdl,latmdc,10);
 
  //drag 4 decimal characters to the right, and insert the floating point to lat and lng
  i=6;
  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);
 
  strcpy(url, "");
  strcpy(url, "http://maps.google.com/maps?q=");
  strcat(url, sentence[6]);
  strcat(url, lngmdc);
  strcat(url, ",");
  strcat(url, sentence[4]);
  strcat(url, latmdc);
  strcat(url, ", Velocidad: ");
  strcat(url, velkmh_c);
  strcat(url, " km/h");
  strcat(url, ", Curso: ");
  strcat(url, course_c);
  strcat(url, " grados");
}
strurl=url;
return returnurl;
}


I'm very confuse when I try to change methode of getting the GPS coordinate because I use the "TinyGPS" Library while he does not.

He is using softwareserial for the GPS. It is not quite clear what he is using for the GSM. He is also using Serial, presumably to his computer.

michinyon:
He is using softwareserial for the GPS. It is not quite clear what he is using for the GSM. He is also using Serial, presumably to his computer.

Hai Michinyon, I think he is using 2 software serial. CMIIW
We can see in the sketch on the setup() section:

void setup(){
Serial.begin(9600);
delay(1000);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);


//setup for gps port
Serial.print("Inicializando GPS... ");
gps.begin(9600); //begin gps softwareserial
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("Reinicializando GPS... ");
   delay(5000);
   returnurl=getURL();
} 
Serial.println("[OK]");
gps_started=true;  
delay(1000);

Serial.print("Inicializando GSM... ");

  while(!gsm.begin(2400)){   //begin the gsm software serial
    Serial.println("[FAIL]");
    Serial.print("Reinicializando GSM... ");
    delay(5000);
  }
  gsm_started=true; 
  Serial.println("[OK]");
  
  if((gps_started)&&(gsm_started)){
    Serial.println("Sistema inicializado");
  } else {
    Serial.println("Sistema no inicializado.");
  }
  
};

Like PaulS said we can't listen 2 software serial in the same time, but he do that like in the sketch..
how do you think...?

The getURL() function has a stupid name. It is not getting a URL. It is getting a complete sentence from the GPS. During that time, the phone is being ignored.

When it has a complete sentence, it then listens to the phone. Data that arrives from the phone (if it uses a SoftwareSerial instance) will be ignored, while the code is listening to the GPS. Data that arrives from the GPS, while the code is listening to the phone, will be ignored.

If it's OK to lose data from the GPS (and it probably is, as you are not likely to be moving all that fast) while listening to the phone, and it's OK to miss data from the phone (I have my doubts about that), then proceed. If not, get a Mega.

PaulS:
The getURL() function has a stupid name. It is not getting a URL. It is getting a complete sentence from the GPS. During that time, the phone is being ignored.

When it has a complete sentence, it then listens to the phone. Data that arrives from the phone (if it uses a SoftwareSerial instance) will be ignored, while the code is listening to the GPS. Data that arrives from the GPS, while the code is listening to the phone, will be ignored.

If it's OK to lose data from the GPS (and it probably is, as you are not likely to be moving all that fast) while listening to the phone, and it's OK to miss data from the phone (I have my doubts about that), then proceed. If not, get a Mega.

Yup I'm agree with you, on my shield and gps module running very slow and sometimes return wrong coordinates.
Because of that I want to substitute the code with mine, simplified by TinyGPS Library.
would you mind to assist me some direction to substitute the getURL() code to my code above..?
I'm so sorry before.

would you mind to assist me some direction to substitute the getURL() code to my code above..?

getURL() blocks until is has received a complete sentence.

The decode() method in the TinyGPS function returns false until it has received a complete sentence.

Replace all that stuff in getURL() with a while loop that spins until decode() returns true. Of course, in the while loop, you'll need to collect the new data as it arrives from the GPS.