GPS Not Declared in this Scope

Hello there. I am currently trying to build a GPS Location tracking system with an ESP32 and a NEO-6M GPS Module. However, there is an error message showing that the 'gps' is not declared in this scope for the second 'gps' in void loop ( ) but there is no error for the first one.

What should I do to fix this problem?

Thank you.

Arduino: 1.8.20 Hourly Build 2022/04/25 09:33 (Windows 10), Board: "DOIT ESP32 DEVKIT V1, 80MHz, 921600, None"



C:\Users\User\Document\GPSModuleESP32\GPSModuleESP32.ino: In function 'void displayInfo()':

GPSModuleESP32:22:7: error: 'gps' was not declared in this scope

   if (gps.location.isValid()){

       ^~~

C:\Users\User\Document PC (Chuah Yee Jia)\GPSModuleESP32\GPSModuleESP32.ino: In function 'void loop()':

GPSModuleESP32:36:9: error: 'gps' was not declared in this scope

     if (gps.encode(Serial2.read()))

         ^~~

GPSModuleESP32:38:26: error: 'gps' was not declared in this scope

   if (millis() > 5000 && gps.charsProcessed() < 10)

                          ^~~

Using library TinyGPSPlus-1.0.3 at version 1.0.3 in folder: C:\Users\User\OneDrive\Documents\Arduino\libraries\TinyGPSPlus-1.0.3 

exit status 1

'gps' was not declared in this scope

Code

#include <TinyGPSPlus.h>

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

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 displayInfo()
{
  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"));
  }
}

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);
  }
}

You have #included TinyGPSPlus.h but have not declared an object named gps that you try to use later

Take a look at the TinyGPSPlus examples and you will see

TinyGPSPlus gps;

Yes, it worked for verification. However, another error popped out when I was trying to upload it. It seems like the board is not connected but I tried unplugging it and restart the laptop as well as tried with the drive update method but it still remains like this with the ESP32. It worked well to upload to the Arduino UNO Compatible board though. May I know how should I fix the problem?

Thank you very much.

Traceback (most recent call last):
esptool.py v3.3
  File "esptool.py", line 5387, in <module>
Serial port COM3
  File "esptool.py", line 5380, in _main
  File "esptool.py", line 4687, in main
  File "esptool.py", line 114, in get_default_connected_device
  File "esptool.py", line 320, in __init__
  File "serial\__init__.py", line 90, in serial_for_url
  File "serial\serialwin32.py", line 64, in open
serial.serialutil.SerialException: could not open port 'COM3': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)
[18036] Failed to execute script 'esptool' due to unhandled exception!
the selected serial port [18036] Failed to execute script 'esptool' due to unhandled exception!
 does not exist or your board is not connected

Please confirm that when you plug the ESP32 board into the PC that a new COM port is created and that you have selected that port in the IDE

Which version of the ESP32 board files have you got installed ?

Yes, the COM4 appears. DOIT ESP32 DEV V1

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