# GPS Programming

**URL:** <https://forum.arduino.cc/t/gps-programming/434169>\
**Category:** Programming\
**Created:** [January 29, 2017, 8:31am UTC](https://forum.arduino.cc/t/gps-programming/434169 "2017-01-29T08:31:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Setsuna](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@Setsuna](https://forum.arduino.cc/u/Setsuna)\
**Post date:** [January 29, 2017, 8:31am UTC](https://forum.arduino.cc/t/gps-programming/434169/1 "2017-01-29T08:31:38Z")

</div>

I made a code for GPS that provides coordinates, but the problem is when I disconnect antenna from GPS shield it still provides location instead the output "No GPS Data".

```cpp
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

SoftwareSerial gpsSerial(2, 3);
TinyGPSPlus gps; 
char lon[12]; // create variable for latitude and longitude object
char lati[12];
char text[160];

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

void loop() 
{
            while(gpsSerial.available()>0)
            {
              char c = gpsSerial.read(); 
            if (gps.encode(c))
            {
            if (gps.location.isValid())
            {     
              dtostrf(gps.location.lat(), 1, 6, lati);
              dtostrf(gps.location.lng(), 1, 6, lon);
              strcat(text,lati);
              strcat(text,",");
              strcat(text,lon);
              Serial.println(text);
              delay (1000);
              memset(text, 0, 160);      
            }
            else
            {
              strcat(text,"No GPS Data");
              Serial.println(text);
              delay (1000);
              memset(text, 0, 160);
            }
            }
            }
}

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 29, 2017, 8:56am UTC](https://forum.arduino.cc/t/gps-programming/434169/2 "2017-01-29T08:56:38Z")

</div>

> problem is when I disconnect antenna from GPS shield it still provides location instead the output "No GPS Data".

Your code is checking whether the GPS device is available, not whether a GPS signal is available. You could check how many satellites are available instead.

---

<div class="post-metadata">

**Author:** ![Setsuna](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@Setsuna](https://forum.arduino.cc/u/Setsuna)\
**Post date:** [January 29, 2017, 11:03am UTC](https://forum.arduino.cc/t/gps-programming/434169/3 "2017-01-29T11:03:06Z")

</div>

```cpp
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

SoftwareSerial gpsSerial(2, 3);
TinyGPSPlus gps; 

char lon[12]; // create variable for latitude and longitude object
char lati[12];
char text[160];
void setup() {
  Serial.begin(9600);
  gpsSerial.begin(9600);
}

void loop() 
{

            while(gpsSerial.available()>0)
            {
            if (gps.encode(gpsSerial.read()))
            {

            if (gps.satellites.value()>=5)
            { Serial.println(gps.satellites.value());
              dtostrf(gps.location.lat(), 1, 6, lati);
              dtostrf(gps.location.lng(), 1, 6, lon);
              strcat(text,lati);
              strcat(text,",");
              strcat(text,lon);
              Serial.println(text);
              delay (1000);
              memset(text, 0, 160);    
            }  
            else
            { Serial.println(gps.satellites.value());
              strcat(text,"No GPS Data");
              Serial.println(text);
              delay (1000);
              memset(text, 0, 160);
            }
            memset(text, 0, 160);    
            }
            }
}

```

Thanks man, I updated my code to depending on the number of satellites.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 29, 2017, 11:46am UTC](https://forum.arduino.cc/t/gps-programming/434169/4 "2017-01-29T11:46:24Z")

</div>

Why are you converting the values returned to strings then concatenating them before printing ?

---

<div class="post-metadata">

**Author:** ![dev\_1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dev_1/32/215847_2.png) [@dev\_1](https://forum.arduino.cc/u/dev_1)\
**Post date:** [January 30, 2017, 1:55am UTC](https://forum.arduino.cc/t/gps-programming/434169/5 "2017-01-30T01:55:43Z")

</div>

> the problem is when I disconnect antenna from GPS shield it still provides location instead the output "No GPS Data".

I wrote the [NeoGPS](https://github.com/SlashDevin/NeoGPS) library to keep track of which fields actually have data. Other libraries don't do this, or they have "special" values that mean "no value". Here is your sketch, modified to use NeoGPS:

```arduino
#include <NMEAGPS.h>
#include <NeoSWSerial.h>

NeoSWSerial gpsSerial(2, 3);

NMEAGPS gps;
gps_fix fix;

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

void loop() 
{
  while (gps.available( gpsSerial )) {
    fix = gps.read();

    if (fix.valid.location) {
      if (fix.valid.satellites)
        Serial.print( fix.satellites );
      Serial.print  ( ',' );
      Serial.print  ( fix.latitude(), 6 );
      Serial.print  ( ',' );
      Serial.println( fix.longitude(), 6 );
    } else {
      Serial.println( F("No GPS data") );
    }
  }
}

```

Notice that it does not use delay. "Blocking" or waiting with the delay statement can cause you to lose GPS characters.

Also notice that it uses [NeoSWSerial](https://github.com/SlashDevin/NeoSWSerial). SoftwareSerial is very inefficient, because it disables interrupts for long periods of time. This can interfere with other parts of your sketch, or with other libraries. AltSoftSerial is the best choice, but it only works on pins 8 & 9 (on an UNO).

NeoGPS is also [smaller](https://github.com/SlashDevin/NeoGPS/blob/master/extras/doc/RAM.md), [faster](https://github.com/SlashDevin/NeoGPS/blob/master/extras/doc/Performance.md) and more accurate than all other libraries. It can also be configured to parse only the fields that you use, which saves even more program space and RAM. It is availabe from the Arduino IDE Library Manager, under the menu **Sketch -\> Include Library -\> Manage Libraries**.

Your original program used 9,412 bytes of program and 734 bytes of RAM.  
The NeoGPS version uses 8,140 bytes of program space and 358 bytes of RAM, a significant savings.

BTW, the number of satellites is not always an indicator that you have a location fix. NeoGPS uses "valid" flags for each member of a fix, as you can see in the sketch. Other members described on the [Data Model page](https://github.com/SlashDevin/NeoGPS/blob/master/extras/doc/Data%20Model.md).

Cheers,  
/dev

---

<div class="post-metadata">

**Author:** ![dev\_1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dev_1/32/215847_2.png) [@dev\_1](https://forum.arduino.cc/u/dev_1)\
**Post date:** [February 18, 2018, 1:34pm UTC](https://forum.arduino.cc/t/gps-programming/434169/6 "2018-02-18T13:34:32Z")

</div>

Please read [How to use the forum](http://arduino.cc/forum/index.php/topic,148850.0.html).

What program did you try? Reply #4 above?

How is your system connected?

What Arduino are you using?

What GPS are you using?

---

<div class="post-metadata">

**Author:** ![Do\_Not\_Give\_Up](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/do_not_give_up/32/1126593_2.png) [@Do\_Not\_Give\_Up](https://forum.arduino.cc/u/Do_Not_Give_Up)\
**Post date:** [March 31, 2018, 11:07pm UTC](https://forum.arduino.cc/t/gps-programming/434169/7 "2018-03-31T23:07:49Z")

</div>

Hello - dev, I have attempted to use your modified to use NeoGPS sketch to solve work with GPS coordinates as varibles to convert to strings for Tx & Rx with LoRa P2P data communication for distance between calculations.

Post # 4 - "Here is your sketch, modified to use NeoGPS:" copied directly from the post to Arduino IDE 1.8.4

#include \<NMEAGPS.h\>  
#include \<NeoSWSerial.h\>

NeoSWSerial gpsSerial(2, 3);

NMEAGPS gps;  
gps\_fix fix;

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

void loop()  
{  
while (gps.available( gpsSerial )) {  
fix = gps.read();

if (fix.valid.location) {  
if (fix.valid.satellites)  
Serial.print( fix.satellites );  
Serial.print ( ',' );  
Serial.print ( fix.latitude(), 6 );  
Serial.print ( ',' );  
Serial.println( fix.longitude(), 6 );  
} else {  
Serial.println( F("No GPS data") );  
}  
}  
}

However, the IDE returns the following error when attempting to compile and upload to my Arduino Mega and NEO- 7M GPS:

NMEGPS:19: error: 'fix' was not declared in this scope

fix = gps.read();

^

NMEGPS:19: error: 'class NMEAGPS' has no member named 'read'

fix = gps.read();

Your assistance with this would be greatly appreciated, as I am sturggling with GPS coordinates as variables converting to a string for Tx & Rx via LoRa radio's,

Paul D Wilkie

Do\_Not\_Give\_Up

[NMEGPS.ino](https://forum.arduino.cc/uploads/short-url/jnT4UbDPVEYpnCIfHtb19PCqoX6.ino) (563 Bytes)

---

<div class="post-metadata">

**Author:** ![dev\_1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dev_1/32/215847_2.png) [@dev\_1](https://forum.arduino.cc/u/dev_1)\
**Post date:** [April 1, 2018, 12:37am UTC](https://forum.arduino.cc/t/gps-programming/434169/8 "2018-04-01T00:37:54Z")

</div>

> the IDE returns the following error

The sketch works fine for me. Did you follow the [NeoGPS Installation instructions](https://github.com/SlashDevin/NeoGPS/blob/master/extras/doc/Installing.md)? I suspect that the NMEAGPS.h file is not in the Libraries/NeoGPS/src directory.

Are those _all_ the errors?

Click the "Copy error messages" button in the IDE, and paste them (control C) into the post editor here. Be sure to put code tags around the error messages. Either type  
**`** **[code]**  **`**  
before the error messages and  
**`** **[/code]**  **`**  
after the error messages, _OR_ select the error messages with the mouse (highlighted blue) and press the CODE button in the upper left (looks like "\</\>").

[/b]

```arduino
  They should
   look like this.

```

Also, you should always do this with code in your posts. It would be nice if you modified your post to insert the code tags.

---

<div class="post-metadata">

**Author:** ![srnet](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/srnet/32/238868_2.png) [@srnet](https://forum.arduino.cc/u/srnet)\
**Post date:** [April 1, 2018, 6:40am UTC](https://forum.arduino.cc/t/gps-programming/434169/9 "2018-04-01T06:40:42Z")

</div>

> Do\_Not\_Give\_Up:  
> Your assistance with this would be greatly appreciated, as I am sturggling with GPS coordinates as variables converting to a string for Tx & Rx via LoRa radio's,

There are fully worked and tested examples of a GPS tracker using LoRa to send the GPS co-ordinates together with portable receivers that will display the trackers location and distance and direction, see here;

> **[LoRa Tracker](https://github.com/LoRaTracker)**
>
> LoRa Tracker has 2 repositories available. Follow their code on GitHub.

There are versions for a tracker intended for ground based use and another for high altitude balloon use.

Greatest distance I have had from it is 270km with the balloon tracker @ a mere 10mW.

I use TinyGPS++ both for the ground based tracker (RC models etc) and the high altitude balloon tracker, it works very well and is easy to understand.

---

<div class="post-metadata">

**Author:** ![Do\_Not\_Give\_Up](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/do_not_give_up/32/1126593_2.png) [@Do\_Not\_Give\_Up](https://forum.arduino.cc/u/Do_Not_Give_Up)\
**Post date:** [April 15, 2018, 1:27am UTC](https://forum.arduino.cc/t/gps-programming/434169/10 "2018-04-15T01:27:17Z")

</div>

-dev, I had installed the NeoGPS library incorrectly, problem solved, NMEGPS.ino compiled correctly.

However, I am not Serial.printing either Latitude or Longitude values?

Serial.print ( fix.latitude(), 6 );

Serial.println( fix.longitude(), 6 );

I am using an Arduino Mega board with a NEO-7M-0.000 GPS receiver that is functioning with other sketches I have been working with.

---

<div class="post-metadata">

**Author:** ![dev\_1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dev_1/32/215847_2.png) [@dev\_1](https://forum.arduino.cc/u/dev_1)\
**Post date:** [April 15, 2018, 1:57am UTC](https://forum.arduino.cc/t/gps-programming/434169/11 "2018-04-15T01:57:36Z")

</div>

> However, I am not Serial.printing either Latitude or Longitude values?
> 
> Serial.print ( fix.latitude(), 6 );
> 
> Serial.println( fix.longitude(), 6 );

Aren't you?

I can't see your sketch, so I don't know what might be wrong. Insert your code IN CODE TAGS, as I described in reply #7. Your code should look like this in the Preview:

```arduino
Serial.print  ( fix.latitude(), 6 );
  
Serial.println( fix.longitude(), 6 );

```

It looks that way because I entered this

**`** **  [code]Serial.print  ( fix.latitude(), 6 );     Serial.println( fix.longitude(), 6 );   [/code]**  **`**

...in the Quick Reply Post editor at the bottom of this page. When you press Preview, it shows you the white code block. You can put your sketch in between those two tags. Or attach the file if you can't figure that out.

> I am using an Arduino Mega board with a NEO-7M

That means you will connect the GPS to Serial1, Serial2 or Serial3 (pins 14-19). I don't know what connections you are using on the Mega.

The NeoGPS examples expect the GPS device to be on Serial1, but you can change that in GPSport.h.

---

<div class="post-metadata">

**Author:** ![Do\_Not\_Give\_Up](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/do_not_give_up/32/1126593_2.png) [@Do\_Not\_Give\_Up](https://forum.arduino.cc/u/Do_Not_Give_Up)\
**Post date:** [April 16, 2018, 12:31am UTC](https://forum.arduino.cc/t/gps-programming/434169/12 "2018-04-16T00:31:36Z")

</div>

-dev, per your suggestion, I have used Serial1, pins Rx18 & Tx19, still no GPS output?

I also verified the NEO-7M is functioning correctly on my Mega board via a blank sketch and HardwareSerial Rx0 0 & Tx0 1.

```cpp
#include <NMEAGPS.h>
#include <NeoSWSerial.h>

//NeoSWSerial gpsSerial(2, 3);

//NeoSWSerial gpsSerial(0, 1);
//NeoSWSerial gpsSerial(3, 4);
NeoSWSerial gpsSerial(18,19);

NMEAGPS gps;
gps_fix fix;

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

void loop() 
{
  //Serial.print("In the void lop");
  //Serial.println();
  
  while (gps.available( gpsSerial )) 
  {
    fix = gps.read();
    Serial.print("gps.read");
    if (fix.valid.location) {
      if (fix.valid.satellites)
        Serial.print( fix.satellites );
      Serial.print  ( ',' );
      Serial.print  ( fix.latitude(), 6 );
      Serial.print  ( ',' );
      Serial.println( fix.longitude(), 6 );
    } else {
      Serial.println( F("No GPS data") );
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Do\_Not\_Give\_Up](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/do_not_give_up/32/1126593_2.png) [@Do\_Not\_Give\_Up](https://forum.arduino.cc/u/Do_Not_Give_Up)\
**Post date:** [April 16, 2018, 12:49am UTC](https://forum.arduino.cc/t/gps-programming/434169/13 "2018-04-16T00:49:54Z")

</div>

Hello -dev, I forgot to attach the sketch, as I copied from the forum, with two Serial.print lines for debug.

[NMEGPS.ino](https://forum.arduino.cc/uploads/short-url/1D7ABplaGuZhaWfc7DnkvBRgUuT.ino) (751 Bytes)

---

<div class="post-metadata">

**Author:** ![dev\_1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dev_1/32/215847_2.png) [@dev\_1](https://forum.arduino.cc/u/dev_1)\
**Post date:** [April 16, 2018, 1:05am UTC](https://forum.arduino.cc/t/gps-programming/434169/14 "2018-04-16T01:05:05Z")

</div>

If you are using a Mega, you should never use a software serial library like NeoSWSerial. Just use Serial1:

```cpp
#include <NMEAGPS.h>

#define gpsSerial  Serial1  // just an "alias" for Serial1

NMEAGPS gps;
gps_fix fix;

void setup() {
  Serial.begin(9600);
  gpsSerial.begin(9600);  // This is really Serial1
}

void loop() 
{
  //Serial.print("In the void lop");   <--- Never print something every time through loop.
  //Serial.println();                       <-- This will cause the sketch to lose GPS data.
  
  while (gps.available( gpsSerial )) 
  {
    fix = gps.read();
    Serial.print("gps.read");
    if (fix.valid.location) {
      if (fix.valid.satellites)
        Serial.print( fix.satellites );
      Serial.print  ( ',' );
      Serial.print  ( fix.latitude(), 6 );
      Serial.print  ( ',' );
      Serial.println( fix.longitude(), 6 );
    } else {
      Serial.println( F("No GPS data") );
    }
  }
}

```

You should read this section about [Choosing a Serial Port](https://github.com/SlashDevin/NeoGPS/blob/master/extras/doc/Installing.md#2-choose-a-serial-port) and Connecting the GPS.

Be sure the GPS TX pin is connected to the Mega RX1 pin 19. You do not need to connect the GPS RX pin. Of course, the GPS VCC and GND pins should be connected to the Mega 5V and GND pins.

---

<div class="post-metadata">

**Author:** ![Rezapazoki777](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Rezapazoki777](https://forum.arduino.cc/u/Rezapazoki777)\
**Post date:** [April 21, 2018, 7:12pm UTC](https://forum.arduino.cc/t/gps-programming/434169/15 "2018-04-21T19:12:01Z")

</div>

Hello  
i have a problem with my gps gsm car tracker code,gsm module must send gps coordinates when i send the word "Track" to it but the problem is that it does not send coordinates until i open serial monitor in arduino IDE.  
I know the problem is that it recognize serial monitor as serial but i want it recognize gsm module as serial.  
please help me .  
thank you. 😕

[gps\_project.ino](https://forum.arduino.cc/uploads/short-url/51VOIBDFdyCSdvDpLbLNzUK8NTv.ino) (1.01 KB)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 21, 2018, 7:33pm UTC](https://forum.arduino.cc/t/gps-programming/434169/16 "2018-04-21T19:33:33Z")

</div>

> i want it recognize gsm module as serial.

How is the GSM module connected to the Arduino ?

---

<div class="post-metadata">

**Author:** ![Rezapazoki777](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Rezapazoki777](https://forum.arduino.cc/u/Rezapazoki777)\
**Post date:** [April 22, 2018, 7:02am UTC](https://forum.arduino.cc/t/gps-programming/434169/17 "2018-04-22T07:02:57Z")

</div>

Thank you for replying. I connect pin RX and TX of gsm to pin 0 and 1 of arduino uno.Excuse me i am new in forum and do not know how to post. I want a code in void begintracking() that recognize the gsm mudule and can find the word "Track".

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 23, 2018, 1:00pm UTC](https://forum.arduino.cc/t/gps-programming/434169/18 "2018-04-23T13:00:14Z")

</div>

> I connect pin RX and TX of gsm to pin 0 and 1 of arduino uno.

What other pins do you connect? You need at least 4 connections.

Why is the variable that controls whether to read from the GPS, or not, called temp?

Using find() to get data from a text message is NOT correct. You need to read the text message, using AT commands to tell the modem to get off it's duff and deliver whatever text messages it has received.

---

<div class="post-metadata">

**Author:** ![Rezapazoki777](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@Rezapazoki777](https://forum.arduino.cc/u/Rezapazoki777)\
**Post date:** [May 1, 2018, 12:14pm UTC](https://forum.arduino.cc/t/gps-programming/434169/19 "2018-05-01T12:14:38Z")

</div>

Hello paul.  
thank you for reply.  
i found the sketch but i have a problem.the problem is that when i send the word "@light on#" to begin tracking , i have to open serial monitor for once to track my vehicle and after that it work like a charm but the problem is that for example when i want to charge the battery the system turns off and when i replace the battery and turn on the system i have to open serial monitor once to begin working and this is not acceptable for a person that i want to sell it to him.  
please help me , 🙂

[gsm\_gps\_project\_full.ino](https://forum.arduino.cc/uploads/short-url/m3Xb8gjuN1UAhxYyw2YXm6or7Ro.ino) (1.85 KB)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 1, 2018, 12:17pm UTC](https://forum.arduino.cc/t/gps-programming/434169/20 "2018-05-01T12:17:46Z")

</div>

> i have a problem.the problem is that when i send the word "@light on#"

Words don't have spaces in them. Phrases do.

> i have to open serial monitor for once to track my vehicle

If you have to open, and close, the Serial Monitor application, in order to make your application communicate with the Arduino, your application is not opening the serial port correctly, for your Arduino.

So, which Arduino do you have, and how is your application opening the serial port?

[Next page](https://forum.arduino.cc/t/gps-programming/434169.md?page=2)
