GPS Programming

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".

#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);
            }
            }
            }
}

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.

#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.

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

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

I wrote the 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:

#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. 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, faster 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.

Cheers,
/dev

Please read How to use the forum.

What program did you try? Reply #4 above?

How is your system connected?

What Arduino are you using?

What GPS are you using?

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 (563 Bytes)

the IDE returns the following error

The sketch works fine for me. Did you follow the NeoGPS Installation instructions? 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]

  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.

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;

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.

-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.

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:

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.

-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.

#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") );
    }
  }

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

NMEGPS.ino (751 Bytes)

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

#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 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.

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. :confused:

gps_project.ino (1.01 KB)

i want it recognize gsm module as serial.

How is the GSM module connected to the Arduino ?

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".

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.

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 , :slight_smile:

gsm_gps_project_full.ino (1.85 KB)

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?