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