My GPS U-blox NEO6M is returning NULL continuously instead of lat, long values

252 Views Asked by At

I have interfaced my ESP32 with a ublox GPS tracker NEO-6M module. It is continuously printing NULL on the serial monitor and even after 10 to 15seconds it prints NULL. What could be the possible reason and how to rectify that? Here is the code:

#include <TinyGPSPlus.h>

// The TinyGPSPlus object
TinyGPSPlus gps;
void displayInfo()
{
  Serial.print(F("Location: "));
  if (gps.location.isValid()){
    Serial.print("Lat: ");
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print("Lng: ");
    Serial.print(gps.location.lng(), 6);
    Serial.println();
  }  
  else
  {
    Serial.print(F("INVALID"));
  }
}

void updateSerial()
{
  // delay(500);
  while (Serial.available())
  {
    Serial2.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while (Serial2.available())
  {
    Serial.write(Serial2.read());//Forward what Software Serial received to Serial Port
  }
}

void setup() {
  Serial.begin(9600);
  Serial2.begin(9600);
  delay(3000);
}

void loop() {
  updateSerial();
  while (Serial2.available() > 0)
    if (gps.encode(Serial2.read()))
      displayInfo();
  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while (true);
  }
}
1

There are 1 best solutions below

0
Imran Mumtaz On

Thanks all for your support. I have found the answer. The GPS-NEO6M tracker works in open air environment not indoor or basement area. So I have used this code, gave it 5V power using Arduino UNO and still it was not connected to the satellite but as soon as I took it to the open air keeping the tracker position towards the sky, it started showing date and time and showed GPS coordinates in 3 to 4 minutes. The code that I used and is working finally is shown below:

#include<TinyGPS++.h>

#include<SoftwareSerial.h>

// Choose two Arduino pins to use for software serial
int RXPin = 16;
int TXPin = 17;

int GPSBaud = 9600;

// Create a TinyGPS++ object
TinyGPSPlus gps;

// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);

void displayInfo() {
  if (gps.location.isValid()) {
    Serial.print("Latitude: ");
    Serial.println(gps.location.lat(), 6);
    Serial.print("Longitude: ");
    Serial.println(gps.location.lng(), 6);
    Serial.print("Altitude: ");
    Serial.println(gps.altitude.meters());
  } else {
    Serial.println("Location: Not Available");
  }

  Serial.print("Date: ");
  if (gps.date.isValid()) {
    Serial.print(gps.date.month());
    Serial.print("/");
    Serial.print(gps.date.day());
    Serial.print("/");
    Serial.println(gps.date.year());
  } else {
    Serial.println("Not Available");
  }

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

  Serial.println();
  Serial.println();
  delay(1000);
}

void setup() {
  // Start the Arduino hardware serial port at 9600 baud
  Serial.begin(9600);

  // Start the software serial port at the GPS's default 
 //baud
  gpsSerial.begin(GPSBaud);
}

void loop() {
  // This sketch displays information every time a new 
    //sentence is correctly encoded.
  while (gpsSerial.available() > 0)
    if (gps.encode(gpsSerial.read()))
      displayInfo();

  // If 5000 milliseconds pass and there are no 
  //characters coming in
  // over the software serial port, show a "No GPS 
   //detected" error
  if (millis() > 5000 && gps.charsProcessed() < 10) {
    Serial.println("No GPS detected");
    while (true);
  }
}