TinyGPS++ impossible to decode alt and nbr_sat

Hello everyone,

I wrote a small sketch to display on an LCD the time, date, geographic coordinates, altitude and number of satellites.
Everything is displayed correctly except the altitude and the number of satellites which remain at 0 on the display. However, it is the same NMEA frame ($GPGGA) that gives these values. There is probably an error that I have not found. If anyone has an explanation, I Thank them in advance.
Pierre

#include <Wire.h>
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <TinyGPS++.h>

LiquidCrystal lcd(5, 6, 7, 8, 9, 10);

char data;
double latitude;
double longitude;
double alt;
unsigned long nbre_sat;

TinyGPSPlus gps;
SoftwareSerial GPS(2, 3);
//*************************
void setup()
{
  lcd.begin(16, 2);
  GPS.begin(9600);  
}
//*************************
void loop()
{
     while (GPS.available())
    {
    data = GPS.read();
    gps.encode(data);
    if (gps.location.isUpdated())
    {
      latitude = gps.location.lat();
      longitude = gps.location.lng();
      alt = gps.altitude.meters();
      nbre_sat = gps.satellites.value();
      
    lcd.clear();
    lcd.setCursor(3, 0);
    lcd.print(gps.date.day());
    lcd.print("/");
    lcd.print(gps.date.month());
    lcd.print("/");
    lcd.print(gps.date.year());
    
    lcd.setCursor(3, 1);
    if (gps.time.hour() < 10) lcd.print("0");
    lcd.print(gps.time.hour());
    lcd.print(":");
    if (gps.time.minute() < 10) lcd.print("0");
    lcd.print(gps.time.minute());
    lcd.print(":");
    if (gps.time.second() < 10) lcd.print("0");
    lcd.print(gps.time.second()); 
    delay(4000);
    
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Lat : ");
    lcd.setCursor(6, 0);
    lcd.print(latitude, 6);
    
    lcd.setCursor(0, 1);
    lcd.print("Lon : ");
    lcd.setCursor(6, 1);
    lcd.print(longitude, 6);
    delay(4000);
    
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Alt : ");
    lcd.setCursor(6, 0);
    lcd.print(alt, 1);
    lcd.print(" m");
          
    lcd.setCursor(0, 1);
    lcd.print("Nbr Sat : ");
    lcd.setCursor(10, 1);      
    lcd.print(nbre_sat);    
    delay(4000);      
   }   
  } 
}

Are you conducting these tests outdoors, with a clear view of the sky?

See these comments from the TinyGPS++ web page:

Validity, Update status, and Age

You can examine an object’s value at any time, but unless TinyGPS++ has recently been fed from the GPS, it should not be considered valid and up-to-date. The isValid() method will tell you whether the object contains any valid data and is safe to query.

Similarly, isUpdated() indicates whether the object’s value has been updated (not necessarily changed) since the last time you queried it.

Lastly, if you want to know how stale an object’s data is, call its age() method, which returns the number of milliseconds since its last update. If this returns a value greater than 1500 or so, it may be a sign of a problem like a lost fix.

Thanks jrmington,

With another sketch, I decode the frames with 9 satellites in view, that's not the problem. On the LCD, I read perfectly the date, time, latitude, longitude but for the altitude and the nbr_sat, it's perpetually zero.

NMEA

One obvious problem is that all these delays are blocking gps.encode() and invalidating the data.

What code produced the screen shot? It does not seem relevant to the stated problem.

I second what @jremington said, get rid of the delays.

Change too;

if (gps.altitude.isUpdated() && gps.satellites.isUpdated() )

I tend to use;

if (gps.speed.isUpdated() && gps.satellites.isUpdated()) //ensures that GGA and RMC sentences have been received

You could try replacing your delay(...) calls with calls to something like:

void smartDelay(unsigned long ms) {
   unsigned long startTime = millis();
   
   while( millis() - startTime < ms ) {
      if( GPS.available() ) {
         gps.encode(GPS.read());
      }
   }
}
1 Like

Yes, its a standard problem.

You see, as I recall the location is updated when the RMC sentence is encoded, so your program then goes off and does a pile of stuff, but by the time the program gets back to reading the GPS characters the GGA sentence can have been missed.

So before updating a display printing stuff out etc., wait until you can be sure both RMC and GGA have been encoded.

Thank you all for your answers and suggestions.
I will do some tests tomorrow morning and let you know the results.

Hello everyone,

I tested the modification given by "srnet":

if (gps.altitude.isUpdated() && gps.satellites.isUpdated())

and I now have the indication of nbr_sat and altitude on the LCD.

I understand now why it was not working.

Thanks to all for your help and have a nice day.
Pierre

1 Like