GPS cant get lat long

hi all. i'm a newbie on arduino and esp. im using esp32 with neo6m gps module to get lat long for geofencing. i found a program online, but i couldnt get the lat long and keep getting "No GPS data is available". any suggestion? thanks

this is my code to get the lat long

void getGps(float& latitude, float& longitude)
{
  // Can take up to 60 seconds
  boolean newData = false;
  for (unsigned long start = millis(); millis() - start < 2000;){
    while (Serial2.available()){
      if (gps.encode(Serial2.read())){
        newData = true;
        break;
      }
      }
    }
  
  if (newData) //If newData is true
  {
    latitude = gps.location.lat();
    longitude = gps.location.lng();
    newData = false;
  }
  else {
    Serial.println("No GPS data is available");
    latitude = 0;
    longitude = 0;
  }
}

Welcome to the forum

Does the GPS get a lock on enough satellites ?

Please post your full sketch

Is the GPS outdoors with a good view of the sky ?

Run this program for a couple of minutes then show us the output the GPS is producing;

void loop()
{
  while (Serial2.available())
  {
    Serial.write(Serial2.read());
  }
}


void setup()
{
  Serial2.begin(9600);
  Serial.begin(115200);
  Serial.println();
  Serial.println("GPS_Echo_Hardware_Serial Starting");
}

yes, the LED is blinking

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

TinyGPSPlus gps;
#define GPS_BAUDRATE 115200

const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {19, 18, 5, 17};
byte colPins[COLS] = {16, 4, 0, 2};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

const int buzzer = 27;
const int led = 26;
const int ledm = 25;
const int kunci = 13;

String code = "";
String kodeotp[] = {
"123A", "456B", "789C",
"215D", "378A", "491B",
"324C", "587D", "916A",
"431B", "692C", "874D",
"567A", "239B", "148C",
"689D", "745A", "312B",
"741C", "826D", "593A",
"856B", "194C", "327D",
"978A", "563B", "412C",
"124D", "895A", "637B",
"111A", "0A"}; // buat tes
bool OTP = false;
int errorCount = 0;

//==========================================================================================

// Geo-Fence (in meters)
const float maxDistance = 30;

// Initial Lat Long
float initialLatitude = -7.320905905572536; //puslet
float initialLongitude = 112.76811401532106;

float latitude, longitude;

//=================================================================

void setup() {
  Serial.println("Initializing");
  Serial.begin(115200);
  pinMode(buzzer, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(ledm, OUTPUT);
  pinMode(kunci, OUTPUT);

  Serial.println("NEO6M Serial Initialize");
  Serial.begin(115200);
  Serial2.begin(GPS_BAUDRATE);

  //gps.begin(115200); //neogps
  }

//=================================================================

void loop() { 
  char key = keypad.getKey();
  if (key) {
    Serial.println(key); 
    if (key == '#') {
      for (String correctCode : kodeotp) {
        if (code == correctCode) {
          OTP = true;
          break;
        }
      }
      if (OTP) {
        digitalWrite(buzzer, HIGH);
        delay(90);
        digitalWrite(buzzer, LOW);
        delay(90);
        digitalWrite(buzzer, HIGH);
        delay(90);
        digitalWrite(buzzer, LOW);
        digitalWrite(led, HIGH);
        digitalWrite(kunci, HIGH);
        Serial.println("Silahkan Ambil Sepeda");
        delay(3000);
        digitalWrite(led, LOW);
        digitalWrite(kunci, LOW);
        errorCount = 0;
      } else {
        Serial.println("OTP Salah");
        for (int i = 0; i < 3; i++) {
          digitalWrite(buzzer, HIGH);
          digitalWrite(ledm, HIGH);
          delay(90);
          digitalWrite(buzzer, LOW);
          digitalWrite(ledm, LOW);
          delay(90);
        }
        errorCount++;
        if (errorCount >= 3){
          Serial.println("Sepeda Diblokir Hubungi Admin");
          for (int i = 0; i < 20; i++) { //jangan lupa diganti 50x ya
          digitalWrite(buzzer, HIGH);
          digitalWrite(ledm, HIGH);
          delay(350);
          digitalWrite(buzzer, LOW);
          digitalWrite(ledm, LOW);
          delay(150);
          }
          errorCount = 0;
        }
      }
      code = ""; // Reset code
      OTP = false;
    } else if (key == '*') {
      code = "";
      Serial.println("----------");
    } else {
      code += key; 
    }
  }

//=================================================================
  
  getGps(latitude, longitude);
  
  float distance = getDistance(latitude, longitude, initialLatitude, initialLongitude);
  
  Serial.print("Latitude= "); Serial.println(latitude, 6);
  Serial.print("Lngitude= "); Serial.println(longitude, 6);
  Serial.print("initialLatitude= "); Serial.println(initialLatitude, 6);
  Serial.print("initialLngitude= "); Serial.println(initialLongitude, 6);
  Serial.print("current Distance= "); Serial.println(distance);

if(distance > maxDistance) {
      digitalWrite(buzzer, HIGH);
    }
    else{
    digitalWrite(buzzer, LOW);
  }
}

//==========================================================================================

// Calculate distance between two points
float getDistance(float flat1, float flon1, float flat2, float flon2) {

  // Variables
  float dist_calc=0;
  float dist_calc2=0;
  float diflat=0;
  float diflon=0;

  // Calculations
  diflat  = radians(flat2-flat1);
  flat1 = radians(flat1);
  flat2 = radians(flat2);
  diflon = radians((flon2)-(flon1));

  dist_calc = (sin(diflat/2.0)*sin(diflat/2.0));
  dist_calc2 = cos(flat1);
  dist_calc2*=cos(flat2);
  dist_calc2*=sin(diflon/2.0);
  dist_calc2*=sin(diflon/2.0);
  dist_calc +=dist_calc2;

  dist_calc=(2*atan2(sqrt(dist_calc),sqrt(1.0-dist_calc)));
  
  dist_calc*=6371000.0; //Converting to meters

  return dist_calc;
}


//==========================================================================================

void getGps(float& latitude, float& longitude)
{
  // Can take up to 60 seconds
  boolean newData = false;
  for (unsigned long start = millis(); millis() - start < 2000;){
    while (Serial2.available()){
      if (gps.encode(Serial2.read())){
        newData = true;
        Serial.println("newData is true");
        break;
      }
      else {
       Serial.println("newData is false");
      }
      }
    }
  
  if (newData) //If newData is true
  {
    latitude = gps.location.lat();
    longitude = gps.location.lng();
    newData = false;
  }
  else {
    Serial.println("No GPS data is available");
    latitude = 0;
    longitude = 0;
  }
}

Thats unusual, a Neo6M normally defaults to 9600 baud.

yes. thanks ill try

this is the result

⸮36,16,20,11,061,0674
$GPGSV,3,3,12,24,74,134,18,25,23,213,23,29,50,283,17,32,09,232,7D
$GPGLL,0719.26195,S,11246.08777,E,091731.00,A,A
70
⸮
⸮⸮@⸮⸮⸮ktꩠ`⸮(⸮⸮⸮⸮5
$GPGGA,091733.00,0719.26206,S,11246.08728,E,1,06,1.16,10.2,M,17.1,M,,70
$GPGSA,A,3,24,29,11,15,12,25,,,,,,,2.52,1.16,2.23
0F
$GPGSV,3,1,12,05,12,035,06,06,06,136,,11,36,110,⸮

You have a lot of communication errors and most, if not all of those messages are invalid sentences. Check the wiring!

Here are examples of what those sentences should look like: GPS: GGA, RMC, and GLL NMEA sentence structure - Shady Electronics

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.