GPS and GSM interface

i m working on GPS Locator n i want help in interface GPS and GSM that is use to send latitude and longitude value to the mobile via text msg…

My GSM is working properly and My GPS is also give correct value…

GSM code:-

int timesTosend=1;
int count=0;
char phone_no=“9870820167”;

void setup()
{
Serial.begin(9600); //Open Serial connection at baud 9600
delay(2000);
Serial.println(“AT+CMGF=1”);
delay(200);
}

void loop()
{
long lat,lon;
while(count<timesTosend){
delay(1500);
Serial.print(“AT+CMGS=”");
Serial.print(“9870820167”);
Serial.println(""");
while(Serial.read()!=’>’);
{
Serial.println(lat);
Serial.print(lon);
delay(500);
Serial.write(0x1A); // sends ctrl+z end of message
Serial.write(0x0D); // Carriage Return in Hex
Serial.write(0x0A); // Line feed in Hex

//The 0D0A pair of characters is the signal for the end of a line and beginning of another.
delay(5000);
}
count++;
}

}

GPS code:-

#include <SoftwareSerial.h>
#include “TinyGPS.h”

long lat,lon; // create variable for latitude and longitude object
SoftwareSerial gpsSerial(2, 3);
TinyGPS gps; // create gps object

void setup(){
Serial.begin(9600); // connect serial
gpsSerial.begin(9600); // connect gps sensor
}
void loop(){
while(gpsSerial.available()){ // check for gps data
if(gps.encode(gpsSerial.read())){ // encode gps data
gps.get_position(&lat,&lon); // get latitude and longitude
// display position
Serial.print("Position: ");
Serial.print("lat: “);Serial.print(lat);Serial.print(” ");// print latitude
Serial.print("lon: ");Serial.println(lon); // print longitude
}
}
}

This should be close to what you want:

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

SoftwareSerial gpsSerial(2, 3);
TinyGPS gps; // create gps object

const int timesTosend=1;
char phone_no[]="9870820167";

void setup() {
  Serial.begin(9600);
  gpsSerial.begin(9600); // connect gps sensor
  delay(2000);
  Serial.println("AT+CMGF=1");
  delay(200);
}

void loop() {
  static int count = 0;
  static long lat=0,lon=0;

  if (count < timesTosend) {
    while(gpsSerial.available()) { // check for gps data
      if(gps.encode(gpsSerial.read())) { // encode gps data
        gps.get_position(&lat,&lon); // get latitude and longitude
        Serial.print("AT+CMGS=\"");
        Serial.print(phone_no);
        Serial.println("\"");

        // Wait for '>'
        while(Serial.read() != '>');

        Serial.print(lat);
        Serial.print(", ");
        Serial.print(lon);
        Serial.write(0x0D);  // Carriage Return in Hex
        Serial.write(0x0A);  // Line feed in Hex
        Serial.write(0x1A);  // sends ctrl+z end of message

        count++;

        //The 0D0A pair of characters is the signal for the end of a line and beginning of another.
        delay(5000);    
      }
    }
  }
}

Thankyou, Mr. Johnwasser
Your code worked for me.
But I dont understand what was the problem.

#include <SoftwareSerial.h>
#include <TinyGPS.h>
SoftwareSerial SIM900(3, 4);
SoftwareSerial gpsSerial(6,7);
long lat,lon;
float LON,LAT;
TinyGPS gps;


void setup()
{
  Serial.begin(9600); 
  gpsSerial.begin(9600);
  delay(20000);  // give time to log on to network. 
  Serial.print("Go");
  Serial.println("Hm");

}


void sendSMS()
{
  Serial.println("AT+CMGF=1\r");                                                        // AT command to send SMS message
  delay(1000);
  Serial.println("AT + CMGS = \"+919962274775\"");                                     // recipient's mobile number, in international format
  delay(1000);
  Serial.println(LON,7);        // message to send
  delay(1000);
  Serial.println((char)26);                      
  delay(1000); 
  Serial.println();
  delay(5000);
  Serial.print("Done");
  
}

void GPS()
{
  while(gpsSerial.available())
  { 
   if(gps.encode(gpsSerial.read()))
   { 
    gps.get_position(&lat,&lon); 
   
    Serial.print("Position: ");
    Serial.print("lat: ");Serial.print(lat);Serial.print(" ");// print latitude
    Serial.print("lon: ");Serial.println(lon); // print longitude
   LON=lon;
    LAT=lat;
    Serial.println(LAT); 
   
   
   }
  }
}

void loop()
{

  sendSMS();
  do {} while (1);
}

I think the error is in gps part.
I would really like to know.

ayushptl: I dont understand what was the problem. I think the error is in gps part. I would really like to know.

You don't say what 'the problem' is but you are not going to get good results when you store a long integer (like 'lat' and 'lon') into floating point variables (like 'LAT' and 'LON').

long lat,lon;
float LON,LAT;

void GPS()
{
  while(gpsSerial.available())
  { 
   if(gps.encode(gpsSerial.read()))
   { 
    gps.get_position(&lat,&lon); 
    LON=lon;
    LAT=lat;
   }
  }
}

Floats can store very big numbers but when they do, the inaccuracy is worse. Do you remember "significant digits" from school? The float actually has very few significant digits.

Imagine we can store 5 significant digits. We can store a number like 0.012345 or we can store 1234500 But we can't store 1234500.012345 and that's the problem with GPS latitude and longitude - the numbers are large and we want high precision.

‘class SoftwareSerial’ has no member named ‘encode’

facing problem with this.

here is the code

#include <TinyGPS.h>
#include <SoftwareSerial.h>
SoftwareSerial gps(0, 1);
SoftwareSerial Gsm(19, 18);
long lat, lng;
float LNG,LAT;
char phone_no = “+8801536138514”;
String textMessage;
TinyGPS gps;

void setup()
{
Serial.begin(9600);
gps.begin(9600);
Serial.print(“gps start”);
Serial1.begin(9600);
Gsm.begin(9600);
Serial.print(“Gsm ready to send and receive sms”);
delay(2000);

Gsm.print(“AT+CMGF=1\r”);
delay(300);
Gsm.print(“AT+CNMI=2,2,0,0,0\r”);
delay(500);
}

void loop()
{

while (Serial.available())
{
char c = Serial.read();
Serial.print(c);
if (gps.encode(c))
newData = true;
}

if (newData)
{

unsigned long age;
gps.f_get_position(&lat, &lng );
LAT= lat;
Serial.print(LAT);
Serial.print();
Serial.print(LNG);
Serial.print();
}
if(Gsm.available()>0)
{
textMessage = Gsm.readString();
Serial.print(textMessage);
delay(10);
}
if(textMessage.indexOf(“where”)>=0)
{
SendSMS();
textMessage = " ";
}

}

void SendSMS()
{
//float flat, flon;
Gsm.print(“AT+CMGF=1\r”);
delay(400);
Gsm.print(“AT+CMGS=”");
Gsm.print(phone_no);
Gsm.println(""");

delay(3000);
Gsm.print("http://maps.google.com/maps?q=loc: ");

Gsm.print(“Latitude = “);
Gsm.println(LNG,7);
Serial.print(” “);
Serial.print(”,”);
Gsm.println(LAT,7);
Serial.print(" ");
delay(2000);
Gsm.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(2000);
Gsm.println();
delay(2000);
}

You have declared a software serial instance called gps. Later you have a TinyGPS instance called gps. Change the name of one of them.