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...
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?
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.
"No data!"at any point. Is it supposed to be"Not encoded data!"?