Combining two programs into one?

Good evening everybody,please i need your help immediately, because i will have tomorrow presentation on my project and today is the last day.I have two programs both are working successfully, but now i only want to combine them together so can you help me what is the correct way to do that.
I am using Arduino UNO , GPRS SHIELD and GPS Module.

My first program is just to send the data to MySQL .

#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(7, 8);

void setup()
{
  gprsSerial.begin(19200);
  Serial.begin(19200);

  Serial.println("Config SIM900...");
  delay(2000);
  Serial.println("Done!...");
  gprsSerial.flush();
  Serial.flush();

  // attach or detach from GPRS service 
  gprsSerial.println("AT+CGATT?");
  delay(100);
  toSerial();


  // bearer settings
  gprsSerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
  delay(2000);
  toSerial();

  // bearer settings
  gprsSerial.println("AT+SAPBR=3,1,\"APN\",\"taif\"");
  delay(2000);
  toSerial();

  // bearer settings
  gprsSerial.println("AT+SAPBR=1,1");
  delay(2000);
  toSerial();
}


void loop()
{
   // initialize http service
   gprsSerial.println("AT+HTTPINIT");
   delay(2000); 
   toSerial();

   // set http param value
   gprsSerial.println("AT+HTTPPARA=\"URL\",\"http://albusaidi.16mb.com/write_data.php?addLati=100&addLongti=200\"");
   delay(2000);
   toSerial();

   // set http action type 0 = GET, 1 = POST, 2 = HEAD
   gprsSerial.println("AT+HTTPACTION=0");
   delay(6000);
   toSerial();

   // read server response
   gprsSerial.println("AT+HTTPREAD"); 
   delay(1000);
   toSerial();

   gprsSerial.println("");
   gprsSerial.println("AT+HTTPTERM");
   toSerial();
   delay(300);

   gprsSerial.println("");
   delay(10000);
}

void toSerial()
{
  while(gprsSerial.available()!=0)
  {
    Serial.write(gprsSerial.read());
  }
}

the second program to send SMS of Altitude and Longitude to my smart phone.

#include <SoftwareSerial.h>

#include <TinyGPS.h>

/*


/* Hooked up on to GPS Receiver Module */
#define GPS_MODULE_PIN_RX (4)
#define GPS_MODULE_PIN_TX (3)
#define GPS_MODULE_BAUDRATE (9600)


TinyGPS gps;
SoftwareSerial nss(GPS_MODULE_PIN_TX, GPS_MODULE_PIN_RX);

static void gpsdump(TinyGPS &gps);
static bool feedgps();
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);
char *str1;

void setup()
{
  Serial.begin(115200);
  nss.begin(GPS_MODULE_BAUDRATE);
  
  Serial.print("SimplyTronics ST-00058 GPS Receiver Module work with TinyGPS library v."); Serial.println(TinyGPS::library_version());
  Serial.println("Modified by SimplyTronics");
  Serial.println();
  Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
  Serial.println();
  Serial.println("Sats HDOP Latitude Longitude Fix  Date       Time       Date Alt     Course Speed Card  Distance Course Card  Chars Sentences Checksum");
  Serial.println("          (deg)    (deg)     Age                        Age  (m)     --- from GPS ----  ---- to London  ----  RX    RX        Fail");
  Serial.println("--------------------------------------------------------------------------------------------------------------------------------------");
  
}

void loop()
{
  bool newdata = false;
  unsigned long start = millis();
  
  // Every second we print an update
  while (millis() - start < 1000)
  {
    if (feedgps())
      newdata = true;
  }
  
  gpsdump(gps);
}

static void gpsdump(TinyGPS &gps)
{
  float flat, flon;
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;
  static const float LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  
  print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  gps.f_get_position(&flat, &flon, &age);
  print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 9, 5);
  print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 10, 5);
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  SoftwareSerial mySerial(7, 8);
mySerial.begin(19200);  //Default serial port setting for the GPRS modem is 19200bps 8-N-1
  mySerial.print("\r");
  delay(1000);                    //Wait for a second while the modem sends an "OK"
  mySerial.print("AT+CMGF=1\r");    //Because we want to send the SMS in text mode
  delay(1000);
 
  //mySerial.print("AT+CSCA=\"+919032055002\"\r");  //Setting for the SMS Message center number,  
  //delay(1000);                                  //uncomment only if required and replace with
                                                  //the message center number obtained from
                                                  //your GSM service provider.
                                                  //Note that when specifying a tring of characters
                                                  // " is entered as \"
 
  mySerial.print("AT+CMGS=\"+96891170780\"\r");    //Start accepting the text for the message
                                                  //to be sent to the number specified.
                                                  //Replace this number with the target mobile number.
  delay(1000);
  
   mySerial.print("longitude:\r");
  mySerial.println(flon);   //The text for the message
  mySerial.println("latitude:\r");
  mySerial.println(flat);   //The text for the message
  delay(1000);
  mySerial.write(0x1A);  //Equivalent to sending Ctrl+Z 

//  
}

static void print_int(unsigned long val, unsigned long invalid, int len)
{
  char sz[32];
  if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  feedgps();
}

static void print_float(float val, float invalid, int len, int prec)
{
  char sz[32];
  if (val == invalid)
  {
    strcpy(sz, "*******");
    sz[len] = 0;
        if (len > 0) 
          sz[len-1] = ' ';
    for (int i=7; i<len; ++i)
        sz[i] = ' ';
    Serial.print(sz);
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1);
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(" ");
  }
  feedgps();
}

static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
    Serial.print("*******    *******    ");
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d   ",
        month, day, year, hour, minute, second);
    Serial.print(sz);
  }
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  feedgps();
}

static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  feedgps();
}

static bool feedgps()
{
  while (nss.available())
  {
    if (gps.encode(nss.read()))
      return true;
  }
  return false;
}

And this is my combining program, and i am not able to recive any data on MySQL.

#include <SoftwareSerial.h>

#include <TinyGPS.h>

/* Hooked up on to GPS Receiver Module */
#define GPS_MODULE_PIN_RX (4)
#define GPS_MODULE_PIN_TX (3)
#define GPS_MODULE_BAUDRATE (9600)


TinyGPS gps;
SoftwareSerial nss(GPS_MODULE_PIN_TX, GPS_MODULE_PIN_RX);

static void gpsdump(TinyGPS &gps);
static bool feedgps();
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);
char *str1;

void setup()
{
  Serial.begin(115200);
  nss.begin(GPS_MODULE_BAUDRATE);
  
  Serial.print("SimplyTronics ST-00058 GPS Receiver Module work with TinyGPS library v."); Serial.println(TinyGPS::library_version());
  Serial.println("Modified by SimplyTronics");
  Serial.println();
  Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
  Serial.println();
  Serial.println("Sats HDOP Latitude Longitude Fix  Date       Time       Date Alt     Course Speed Card  Distance Course Card  Chars Sentences Checksum");
  Serial.println("          (deg)    (deg)     Age                        Age  (m)     --- from GPS ----  ---- to London  ----  RX    RX        Fail");
  Serial.println("--------------------------------------------------------------------------------------------------------------------------------------");
  
}

void loop()
{
  bool newdata = false;
  unsigned long start = millis();
  
  // Every second we print an update
  while (millis() - start < 1000)
  {
    if (feedgps())
      newdata = true;
  }
  
  gpsdump(gps);
}

static void gpsdump(TinyGPS &gps)
{
  float flat, flon;
  unsigned long age, date, time, chars = 0;
  unsigned short sentences = 0, failed = 0;
  static const float LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  
  print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
  print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
  gps.f_get_position(&flat, &flon, &age);
  print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 9, 5);
  print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 10, 5);
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  SoftwareSerial mySerial(7, 8);
mySerial.begin(19200);  //Default serial port setting for the GPRS modem is 19200bps 8-N-1
  mySerial.print("\r");
  delay(1000);                    //Wait for a second while the modem sends an "OK"
 // mySerial.print("AT+CMGF=1\r");    //Because we want to send the SMS in text mode
  //delay(1000);
 
  //mySerial.print("AT+CSCA=\"+919032055002\"\r");  //Setting for the SMS Message center number,  
  //delay(1000);                                  //uncomment only if required and replace with
                                                  //the message center number obtained from
                                                  //your GSM service provider.
                                                  //Note that when specifying a tring of characters
                                                  // " is entered as \"
Serial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(2000);
 Serial.println("AT+SAPBR=3,1,\"APN\",\"taif\"");
delay(2000);
Serial.println("AT+SAPBR=1,1");
delay(2000);
Serial.println("AT+HTTPINIT");
delay(2000);
Serial.print("AT+HTTPPARA=\"URL\",\"albusaidi.16mb.com/write_data.php?");
Serial.print("addLati=");
Serial.print(flon); 
Serial.print("&addLongti=");
Serial.print(flat);
Serial.println("\"");
delay(3000);
Serial.println("AT+HTTPACTION=0");
delay(6000);
Serial.println("AT+HTTPREAD");
delay(1000);
Serial.println("AT+HTTPTERM");
delay(500);

}

static void print_int(unsigned long val, unsigned long invalid, int len)
{
  char sz[32];
  if (val == invalid)
    strcpy(sz, "*******");
  else
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  feedgps();
}

static void print_float(float val, float invalid, int len, int prec)
{
  char sz[32];
  if (val == invalid)
  {
    strcpy(sz, "*******");
    sz[len] = 0;
        if (len > 0) 
          sz[len-1] = ' ';
    for (int i=7; i<len; ++i)
        sz[i] = ' ';
    Serial.print(sz);
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1);
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(" ");
  }
  feedgps();
}

static void print_date(TinyGPS &gps)
{
  int year;
  byte month, day, hour, minute, second, hundredths;
  unsigned long age;
  gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  if (age == TinyGPS::GPS_INVALID_AGE)
    Serial.print("*******    *******    ");
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d   ",
        month, day, year, hour, minute, second);
    Serial.print(sz);
  }
  print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  feedgps();
}

static void print_str(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  feedgps();
}

static bool feedgps()
{
  while (nss.available())
  {
    if (gps.encode(nss.read()))
      return true;
  }
  return false;
}

Is the GPRS connected to Serial on pins 0 and 1 or mySerial on pins 7 and 8?

From your code, you appear to be sending the AT commands to Serial, but it sounds like the modem is on mySerial.

This should be at the top, next to the other software serial declaration.

SoftwareSerial mySerial(7, 8);

This should be in setup():

mySerial.begin(19200); //Default serial port setting for the GPRS modem is 19200bps 8-N-1

mySerial is a dreadfully unhelpful name - but you probably know that by now.

i have done some change and still GPRS not sending the data, can you please help me in code because i am very bad in software and coding.
my problem is GPRS not sending that data to my DB. and i am not very good in programming

on the serial monitor it shows:

 AT+SAPBR=3,1,"CONTYPE","GPRS"
AT+SAPBR=3,1,"APN","taif"
AT+SAPBR=1,1
AT+HTTPINIT
AT+HTTPPARA="URL","albusaidi.16mb.com/write_data.php?addLati=57.92&addLongti=23.69"
AT+HTTPACTION=0
AT+HTTPREAD
AT+HTTPTERM

that means i have gotten the (GPS) output correctly but sounds there is something missed on my GPRS shield not able to send it to mysql

What DrAzzy said.

How are you connecting the GPRS shield? Pins D7 & D8? I don't see where it is set up in your sketch. Also, you need to read up on SoftwareSerial, only one instance can listen at once and you need to swap that in your code.

The easier option may be to have the GPS unit on D3 & D4, and the GPRS shield on D0 & D1, but then you cannot use Serial Monitor as well.

DrAzzy:
Is the GPRS connected to Serial on pins 0 and 1 or mySerial on pins 7 and 8?

no myserial is on 7 and 8

If you've changed the code, post the changed code, we have already made recommendations re: the last code you posted.

The code you posted most recently was all messed up - most of the serial statements that look like you mean to send them to gprs are being send to Serial instead of the software serial on pins 7 and 8, so there's lots to fix there.

I just had a thought - that GPS is probably continually blasting out data on serial, isn't it? If so, that's your problem - Only one software serial at a time can be sending or receiving data - if you try to send and receive, or receive on two software serials at once, you get and/or send gibberish.

In that case, you'll need to like... maybe end() the GPS serial, and begin the one for GPRS - send the data, then stop that serial and start the other.

Alternately, you could use the hardware serial ports for one of them (pins 0 and 1), but in this case, you'd need to disconnect it while uploading the sketch, otherwise it would interfere (since the sketches are uploaded through serial too).

i added SoftwareSerial mySeria

#include <SoftwareSerial.h>
SoftwareSerial mySerial(7, 8);
#include <TinyGPS.h>

DrAzzy:
This should be in setup():

when i do this show some error:
Arduino_Sim900_GPS_to_MySQL_with_GPRS.ino: In function 'void gpsdump(TinyGPS&)':
Arduino_Sim900_GPS_to_MySQL_with_GPRS:71: error: a function-definition is not allowed here before '{' token