0

I'm working on a project in which when I press reset button the arduino should send longitudes and latitudes to my phone. I'm a beginner and tried to join two codes, first code can send the message and the second code can get the lon and lat printed on the serial monitor.

First code--

int timesTosend=1;
int count=0;
char phone_no[]="+917771914436";
void setup()
{
  Serial.begin(9600);
  delay(2000);
  Serial.println("AT+CMGF=1");
  delay(200);
}
void loop()
{
  while (count<timesTosend)
  {
    delay(1500);
    Serial.print("AT+CMGS=\"");
    Serial.print(phone_no);
    Serial.println("\"");
    while(Serial.read()!='>');
    {
      Serial.print("Hii If this message is sent then its a huge sucess for me");
      delay(500);
      Serial.write(0x1A);
      Serial.write(0x0D);
      Serial.write(0x0A);
      delay(5000);
    }
    count++;
  }
}

Second code--

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

long lat,lon; // create variable for latitude and longitude object

SoftwareSerial gpsSerial(4, 3); // create gps sensor connection
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
   }
  }
}

My code which is not working--

#include <SoftwareSerial.h>
#include <TinyGPS.h>
long lat,lon; // create variable for latitude and longitude object

SoftwareSerial gpsSerial(4, 3); // create gps sensor connection
TinyGPS gps; // create gps object

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

int timesTosend=1;
int count=0;
char phone_no[]="+917771914436";
void loop()
{
  while (count<timesTosend)
  {
    delay(1500);
    Serial.print("AT+CMGS=\"");
    Serial.print(phone_no);
    Serial.println("\"");
     while(Serial.read()!='>');
    {
      while(gpsSerial.available())
      {
        if(gps.encode(gpsSerial.read()))
        { // encode gps data
    gps.get_position(&lat,&lon); // get latitude and longitude
    Serial.print(lat,lon);
      delay(500);
      Serial.write(0x1A);
      Serial.write(0x0D);
      Serial.write(0x0A);
      delay(5000);
        }
      }
    }
    count++;
  }
}

Please help me out by correcting my mistake in the code.

0
Serial.print(lat,lon);

That doesn't do what you think it does. That is trying to print the latitude encoded in base "lon", which just isn't going to work.

Instead you need to send it as separate operations:

Serial.print(lat);
Serial.print(",");
Serial.print(lon);

Also you should separate out the GPS and GSM routines. Create a function that gets the latitude and longitude, and another function that sends the two values as a text message. Then you can keep things as discrete obvious steps and not get confused with what is happening when.

  • Then please tell me what can I do to send it as text message? – Tanishq Jaiswal Oct 17 '15 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.