-1

I have an Arduino Duemilanove and em408.

My code is this:

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

#define GPS_RX_PIN 2
#define GPS_TX_PIN 3

TinyGPS gps;
SoftwareSerial ss(GPS_RX_PIN, GPS_TX_PIN);

void setup()
{
  Serial.begin(9600);
  ss.begin(4800); 
  }

void loop()
{
  while (ss.available())
  {
    char c = byte(ss.read());
    // Here i tried Serial.write(c); and i succesully saw the NMEA data in my serial monitor
    if (gps.encode(c))
    {
      long lat, lon;
     unsigned long fix_age;
    gps.get_position(&lat, &lon, &fix_age);
    if (fix_age == TinyGPS::GPS_INVALID_AGE )
      Serial.println("No fix ever detected!");
    else if (fix_age > 2000)
      Serial.println("Data is getting STALE!");
    else
      Serial.println("Latitude and longitude valid!");

      Serial.print("Lat: "); 
      Serial.print(lat);
      Serial.print(" Lon: "); 
      Serial.println(lon);

    }
    else Serial.println("No data!");
  }
}

My output is this:

No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
No data!
Latitude and longitude valid!
Lat: 32758239
 Lon: 15637489
No data!
No data!
No data!
No data!
No data!
No data!
No data!

And then i continue getting No data! forever...

So...

  1. Why does gps.encode() only manage to return true only 1 time? I have gotten the raw data from the gps (the commented code on my code snippet) and it's ok. Why it returns true only one time?

  2. Why the longitude/latidude data is like this? As far as i know, this isnt valid, correct?

EDIT: Also tried with TinyGPS++.. but no luck in encoding too...

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


#define GPS_RX_PIN 2
#define GPS_TX_PIN 3

TinyGPSPlus gps;
SoftwareSerial ss(GPS_RX_PIN, GPS_TX_PIN);

void setup()
{
  Serial.begin(9600);
  ss.begin(4800); 
  }

void loop()
{
  while (ss.available()>0)
  {
    char c = byte(ss.read());
    // Here i tried Serial.write(c); and i succesully saw the NMEA data in my serial monitor
    if (gps.encode(c)) //i previously used if(gps.encode(ss.read())); but no luck then
    {
      Serial.print(F("Location: ")); 
  if (gps.location.isValid())
  {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid())
  {
    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.print(F(" "));
  if (gps.time.isValid())
  {
    if (gps.time.hour() < 10) Serial.print(F("0"));
    Serial.print(gps.time.hour());
    Serial.print(F(":"));
    if (gps.time.minute() < 10) Serial.print(F("0"));
    Serial.print(gps.time.minute());
    Serial.print(F(":"));
    if (gps.time.second() < 10) Serial.print(F("0"));
    Serial.print(gps.time.second());
    Serial.print(F("."));
    if (gps.time.centisecond() < 10) Serial.print(F("0"));
    Serial.print(gps.time.centisecond());
  }
  else
  {
    Serial.print(F("INVALID"));
  }

  Serial.println();

    }
    else Serial.println("No data!");
  }
}

New code:

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


#define GPS_RX_PIN 2
#define GPS_TX_PIN 3

TinyGPSPlus gps;
SoftwareSerial ss(GPS_RX_PIN, GPS_TX_PIN);

void setup()
{
  Serial.begin(9600);
  ss.begin(4800); 
}

char clat[11];
char clng[11];

void loop()
{
    //if (buttons || tilt) {
    //if (buttons & BUTTON_SELECT) {

        bool isGpsLocationValid = false;
        do
        {  
            while (ss.available()>0)
            {
                char c = byte(ss.read());
                if (gps.encode(c)) //i previously used if(gps.encode(ss.read())); but no luck then
                {    
                    if (gps.location.isValid())
                    {
                        dtostrf(gps.location.lat(), 11, 6, clat);
                        dtostrf(gps.location.lng(), 11, 6, clng);
                        //send-print
                        //delay (just in case multiple bumps)
                        isGpsLocationValid = true;
                    }
                }   
             }
        } while (isGpsLocationValid == false);
        Serial.write(clat);
        Serial.println();
        Serial.write(clng);
       //}
    //}
}

EDIT: The newest code i have posted works 100%! So if you have similar problems with that aweful gps, use this!

Apparently, TinyGPS and TinyGPS++ don't work well with this gps. To be precice, there is a problem with the gps.encode(c) function.

So using if (gps.encode(c)) most of the times returns false, except some rare occasions which returns true and then you can do some processing...

My last code takes care of that problem by checking continuously, using a do...while loop. If if (gps.location.isValid()) returns true, which means i have valid data, then i do the processing i want and end the do...while loop.

8
  • Your code doesn't seem to output "No data!" at any point. Is it supposed to be "Not encoded data!"? Commented Jun 9, 2014 at 8:33
  • Ok yeah. I modified the code Commented Jun 9, 2014 at 11:25
  • Try to change the baud rate of Serial.begin() to 4800 and in the serial monitor change the same. Commented Jun 9, 2014 at 14:57
  • 1
    If you're getting proper, locked GPS data if you output the raw NMEA stream, apparently there is an issue in the TinyGPS library, possibly as a result of an interaction with your particular GPS. Welcome to the world of open source. The TinyGPS library is a project someone did in their spare time, and released for free. They do not have to bother to support it at all. Since the source is available why don't you try to fix it yourself? Commented Jun 11, 2014 at 9:58
  • 1
    Try having tinyGPS parse the serial messages from the PC and then sending a valid NMEA string in the serial monitor yourself. That way you can be sure if the issue is with the library, or the GPS. Commented Jun 14, 2014 at 18:44

2 Answers 2

2

The newest code i have posted works 100%! So if you have similar problems with that aweful gps, use this!

Apparently, TinyGPS and TinyGPS++ don't work well with this gps. To be precice, there is a problem with the gps.encode(c) function.

So using if (gps.encode(c)) most of the times returns false, except some rare occasions which returns true and then you can do some processing...

My last code takes care of that problem by checking continuously, using a do...while loop. If if (gps.location.isValid()) returns true, which means i have valid data, then i do the processing i want and end the do...while loop.

NOTE: This code is for TinyGPS++ library. Same theory will work for TinyGPS too of course...

Here it is:

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


#define GPS_RX_PIN 2
#define GPS_TX_PIN 3

TinyGPSPlus gps;
SoftwareSerial ss(GPS_RX_PIN, GPS_TX_PIN);

void setup()
{
  Serial.begin(9600);
  ss.begin(4800); 
}

char clat[11];
char clng[11];

void loop()
{
    bool isGpsLocationValid = false;
    do
    {  
        while (ss.available()>0)
        {
            char c = byte(ss.read());
            if (gps.encode(c)) //i previously used if(gps.encode(ss.read())); but no luck then
            {    
                if (gps.location.isValid())
                {
                    dtostrf(gps.location.lat(), 11, 6, clat);
                    dtostrf(gps.location.lng(), 11, 6, clng);                        
                    isGpsLocationValid = true;
                }
            }   
        }
    } while (isGpsLocationValid == false);
    Serial.write(clat);
    Serial.println();
    Serial.write(clng);
}
2
  • Are you sure about while (isGpsLocationValid = false);? That should be while (isGpsLocationValid == false);, otherwise, you won't ever go out of this while loop! Commented Jun 17, 2014 at 4:57
  • oops, good observation Commented Jun 17, 2014 at 8:40
0

Thaks very much!!! You have already given me the solution for my problem, I`ve been trying to get this data for 3 days!! When I was using just the GPS module, it was fine, I could get the data, but when I tried to use GSM module + GPS, I could not get the data from GPS!! U gave me the solution

bool isGpsLocationValid = false;
        do
        {  
            while (ss.available()>0)
            {
                char c = byte(ss.read());
                if (gps.encode(c)) //i previously used if(gps.encode(ss.read())); but no luck then
                {    
                    if (gps.location.isValid())
                    {
                        dtostrf(gps.location.lat(), 11, 6, clat);
                        dtostrf(gps.location.lng(), 11, 6, clng);                        
                        isGpsLocationValid = true;
                    }
                }   
             }
        } while (isGpsLocationValid == false);
        Serial.write(clat);
        Serial.println();
        Serial.write(clng);

Congratulations!! Brazilian Hugs for you ; )

2
  • No problem... Just vote up my post, if you found it useful (becauce its -3 at the moment) Commented Sep 15, 2016 at 8:33
  • Is the conversion to byte what solves the issue? Because otherwise it doesn't make sense that this would work and the old code not since you still have to get through the encoding in order to execute the body of the IF. Commented Nov 3, 2019 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.