GPS Serial Data is 99999999

Hi, so I am using the neo m8n GPS with the Arduino uno and I am trying to receive serial lat and long data form the GPS but the Output is constant at 99999999. what can I do I have tried the baud rate solution but no avail. Please Help. Here is the code I am using.

#include <SoftwareSerial.h>
#include <TinyGPS.h>
long lat=0, lon=0;
SoftwareSerial gpsSerial(2, 3);
TinyGPS gps;
void setup()
{
  Serial.begin(9600);
  gpsSerial.begin(4800);
}

void loop()
{
  if(gpsSerial.available())
  Serial.write(gpsSerial.read());
  while (gpsSerial.available())
  {
       //if (gps.encode(gpsSerial.read()))
   // {
      gps.get_position(&lat, &lon);
      Serial.println("Position: ");
      Serial.print("lat: ");
      Serial.println(lat);
      Serial.print("lon: ");
      Serial.println(lon);
      delay(500);
    //}
  }
}

Thankyou in advance

       //if (gps.encode(gpsSerial.read()))

You have commented out a critical bit of the code, so it can't possibly work. The entire GPS sentence has to be read in successfully before you can access the location variables.

Take a much closer look at the examples.

  if(gpsSerial.available())
  Serial.write(gpsSerial.read());
  while (gpsSerial.available())
  {
       //if (gps.encode(gpsSerial.read()))

Even if you uncomment that, you will NOT get good data.

Yuaeol snigeeyohrcarce ttefnto.

(You are only sending every other character to the function.)

Do NOT read a character and print it, then read a character and send it to the function.

You can read a character, store it in a variable, and then print the character AND send it to the function.

BTW, the NEO-8 series sends other “Talker IDs” that TinyGPS does not handle. This means your device will send a “GNRMC” instead of the typical “GPRMC”. There are other issues too, mostly with the example programs.

I wrote the NeoGPS to handle all talker IDs from these newer GPS devices, and its example programs are structured properly. You should be able to modify them without breaking them.

Here is your sketch, modified to use NeoGPS:

#include <SoftwareSerial.h>
SoftwareSerial gpsSerial(2, 3);

// It would be *MUCH* better to use AltSoftSerial on pins 8 & 9!
//#include <AltSoftSerial.h>
//AltSoftSerial gpsSerial;

//  If you really can't use those pins, NeoSWSerial is also
//  *MUCH* better than SoftwareSerial:
//#inclyde <NeoSWSerial.h>
//NeoSWSerial gpsSerial(2,3);

#include <NMEAGPS.h>

NMEAGPS gps;
gps_fix fix;

void setup()
{
  Serial.begin(9600);
  gpsSerial.begin(9600);
}

void loop()
{
  while (gps.available( gpsSerial )) {
    fix = gps.read();
    Serial.println( F("Position: ") );
    Serial.print( F("lat: ") );
    Serial.println( fix.latitudeL() );
    Serial.print( F("lon: ") );
    Serial.println( fix.longitudeL() );
  }
}

This version uses only 355 bytes of RAM, while the TinyGPS version uses 508 bytes of RAM. It also provides one more digit of precision. If you want to print these long integers as if they were floating-point numbers, please see the NMEAloc.ino example sketch.

Notice that the GPS baud rate is probably 9600, not 4800, and no delay is needed. In general, you should avoid using delay, especially with GPS devices.

I also put some comments in about not using SoftwareSerial, because it is very inefficient. It disables interrupts for long periods of time, which can interfere with other parts of your sketch, or with other libraries.

If you want to try it, NeoGPS is available from the Arduino IDE Library Manager, under the menu Sketch → Include Library → Manage Libraries. There is also a diagnostic program that you can use to auto-detect the baud rate of your GPS device, if it is not 9600 or 4800.

Cheers,
/dev