0

Anyone help? It shows incorrect time. Please help.

 /* Conexion pines
 Arduino GPS 
 D3 RX
 D4 TX
 */
 #include <SoftwareSerial.h> //incluimos SoftwareSerial
 #include <TinyGPS.h> //incluimos TinyGPS
 TinyGPS gps; //Declaramos el objeto GPS
 SoftwareSerial serialgps(10,11); //Declaramos el pin 4 (Tx del GPS) y 3 (Rx del GPS)
 //Declaramos la variables para la obtención de datos
 int year;
 byte month, day, hour, minute, second, hundredths;
 unsigned long chars;
 unsigned short sentences, failed_checksum;
 void setup()
 {
 Serial.begin(9600); //Iniciamos el puerto serie
 serialgps.begin(9600); //Iniciamos el puerto serie del gps
 //Imprimimos en el monitor serial:
 Serial.println("");
 }
 void loop()
 {
 while(serialgps.available()) 
 {
 int c = serialgps.read(); 
 if(gps.encode(c)) 
 {
 float latitude, longitude;
 gps.f_get_position(&latitude, &longitude);
 Serial.print("Latitud/Longitud: "); 
 Serial.print(latitude,5); 
 Serial.print(", "); 
 Serial.println(longitude,5);
 gps.crack_datetime(&year,&month,&day,&hour,&minute,&second,&hundredths);
 Serial.print("Date: "); Serial.print(day, DEC); Serial.print("/"); 
 Serial.print(month, DEC); Serial.print("/"); Serial.print(year);
 Serial.print(" Time: "); Serial.print(hour, DEC); Serial.print(":"); 
 Serial.print(minute, DEC); Serial.print(":"); Serial.print(second, DEC); 
 Serial.print("."); Serial.println(hundredths, DEC);
 Serial.print("Altitud (metros): "); Serial.println(gps.f_altitude()); 
 Serial.print("Rumbo (grados): "); Serial.println(gps.f_course()); 
 Serial.print("Velocidad(kmph): "); Serial.println(gps.f_speed_kmph());
 Serial.print("Satelites: "); Serial.println(gps.satellites());
 Serial.println();
 gps.stats(&chars, &sentences, &failed_checksum);
 delay(10000);
 }
 }
 }

enter image description here

asked Apr 14, 2018 at 14:22
9
  • how far was it off? Commented Apr 14, 2018 at 14:26
  • nope its not :( Commented Apr 14, 2018 at 14:33
  • I meant what was the time when you ran that code? What time should it have shown. Commented Apr 14, 2018 at 14:38
  • it should be 10:15 pm Commented Apr 14, 2018 at 14:42
  • And what time zone are you in? Commented Apr 14, 2018 at 15:04

1 Answer 1

1

Like Majenko's comments suggested, you must shift the UTC time to your local time. The Time library can help you do this.

There are some other problems:

  • You cannot wait 10 seconds without processing the characters. The Arduino receive buffer only has room for 64 characters. The GPS device could have sent 10000 characters during that time, so most of them will get dropped. This prevents the GPS library from parsing a GPS data successfully. It will eventually "see" a complete set of data, sometime after the 10 seconds. You may see data once every 11 seconds, not 10.

  • You are printing the several values even though they may not be valid. For example, if you are not moving, or you do not have good satellite reception, the GPS device may not provide a speed.

  • The Arduino millis() clock will not be synchronized with the GPS clock. You could use the GPS updates as an exact 1-second clock. Simply count 10 fixes as they arrive, and this will mean that 10 seconds have elapsed.

  • You should use a different serial port and/or library. This answer describes the various choices: HardwareSerial (i.e. Serial on pins 0 & 1), AltSoftSerial (8 & 9 on an UNO) or NeoSWSerial (any two pins).

Here is a NeoGPS version of your sketch that addresses these issues:

#include <NeoSWSerial.h>
#include <NMEAGPS.h>
NMEAGPS gps; //Declaramos el objeto GPS
gps_fix fix; // all GPS fields, including date/time, location, etc.
uint8_t fixCount;
NeoSWSerial serialgps(10,11);
void setup()
{
 Serial.begin(9600); //Iniciamos el puerto serie
 serialgps.begin(9600); //Iniciamos el puerto serie del gps
 //Imprimimos en el monitor serial:
 Serial.println( F("Test") );
}
void loop()
{
 // Check for GPS characters and parse them
 if (gps.available( serialgps )) 
 {
 // Once per second, a complete GPS fix structure is ready. Get it.
 fix = gps.read();
 // Count elapsed seconds
 fixCount++;
 if (fixCount >= 10)
 {
 fixCount = 0; // reset counter
 // adjust from UTC to local time
 if (fix.valid.time)
 adjustTime( fix.dateTime );
 printGPSdata();
 }
 }
}
void printGPSdata()
{
 Serial.print( F("Latitud/Longitud: ") ); 
 if (fix.valid.location) {
 Serial.print( fix.latitude(), 5 ); 
 Serial.print( F(", ") ); 
 Serial.print( fix.longitude(), 5 );
 }
 Serial.println();
 Serial.print( F("Date: ") );
 if (fix.valid.date) {
 Serial.print( fix.dateTime.date ); Serial.print('/'); 
 Serial.print( fix.dateTime.month ); Serial.print('/');
 Serial.print( fix.dateTime.year );
 }
 Serial.print( F(" Time: ") );
 if (fix.valid.time) {
 Serial.print( fix.dateTime.hours ); Serial.print(':'); 
 if (fix.dateTime.minutes < 10)
 Serial.print( '0' );
 Serial.print(fix.dateTime.minutes); Serial.print(':');
 if (fix.dateTime.seconds < 10)
 Serial.print( '0' );
 Serial.print( fix.dateTime.seconds ); 
 Serial.print('.');
 if (fix.dateTime_cs < 10)
 Serial.print( '0' );
 Serial.print( fix.dateTime_cs );
 }
 Serial.println();
 Serial.print( F("Altitud (metros): ") );
 if (fix.valid.altitude)
 Serial.print( fix.altitude() );
 Serial.println();
 Serial.print( F("Rumbo (grados): ") );
 if (fix.valid.heading)
 Serial.print( fix.heading() ); 
 Serial.println();
 Serial.print( F("Velocidad(kmph): ") );
 if (fix.valid.speed)
 Serial.print( fix.speed_kph() );
 Serial.println();
 Serial.print( F("Satelites: ") );
 if (fix.valid.satellites)
 Serial.println( fix.satellites );
 Serial.println();
 Serial.println();
}
//--------------------------
void adjustTime( NeoGPS::time_t & dt )
{
 NeoGPS::clock_t seconds = dt; // convert date/time structure to seconds
 // Offset to the local time
 const int32_t zone_hours = +8L;
 const NeoGPS::clock_t zone_offset = zone_hours * NeoGPS::SECONDS_PER_HOUR;
 seconds += zone_offset;
 dt = seconds; // convert seconds back to a date/time structure
} // adjustTime

If you want to try it, NeoGPS, AltSoftSerial and NeoSWSerial are available from the IDE Library Manager, under the menu Sketch -> Include Library -> Manage Libraries. NeoGPS is smaller, faster, more reliable and more accurate than all other GPS libraries.

Even if you don't use it, there are many suggestions on the Installation and Troubleshooting pages.

answered Apr 14, 2018 at 15:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.