Skip to content

Instantly share code, notes, and snippets.

@ParkWardRR
Created January 26, 2025 06:42
Show Gist options
  • Save ParkWardRR/fd211c2252ec397f7143489f4298aaf3 to your computer and use it in GitHub Desktop.
Save ParkWardRR/fd211c2252ec397f7143489f4298aaf3 to your computer and use it in GitHub Desktop.
/*
* ESP32-H2 GPS Parser
* Description:
* This script reads NMEA sentences from a GPS module connected to an ESP32-H2 board,
* parses the data using the TinyGPS++ library, and outputs clean, human-readable GPS data
* such as latitude, longitude, date, time, altitude, speed, number of satellites, and HDOP.
*
* Features:
* - Real-time GPS data parsing and display.
* - Clear and organized serial output.
* - Error handling for missing or invalid data.
*
* Dependencies:
* 1. Install TinyGPS++ library in Arduino IDE:
* - Go to Sketch > Include Library > Manage Libraries.
* - Search for "TinyGPS++" and install the latest version.
*
* Hardware Requirements:
* 1. ESP32-H2 Development Board (e.g., ESP32-H2-DEVKIT-LIPO).
* 2. GPS module (e.g., GT-504GG-N or similar).
* 3. Jumper wires for connections.
*
* Wiring Instructions:
* - Connect the GPS module's TX pin to GPIO14 on the ESP32-H2 (RX).
* - Connect the GPS module's RX pin to GPIO13 on the ESP32-H2 (TX).
* - Connect the GPS module's VCC to 3V3 on the ESP32-H2.
* - Connect the GPS module's GND to GND on the ESP32-H2.
*
* Usage:
* 1. Upload this script to your ESP32-H2 using Arduino IDE.
* 2. Open the Serial Monitor at 115200 baud rate.
* 3. Place the GPS module in an open area with a clear view of the sky for best results.
*/
#include <TinyGPS++.h>
#include <HardwareSerial.h>
// Create TinyGPS++ object
TinyGPSPlus gps;
// Initialize UART1 for GPS communication
HardwareSerial GPS(1); // Use UART1
void setup() {
Serial.begin(115200); // Serial Monitor baud rate
GPS.begin(9600, SERIAL_8N1, 14, 13); // RX=14, TX=13 for GPS module
Serial.println(F("===================================="));
Serial.println(F(" ESP32-H2 GPS Parser "));
Serial.println(F("===================================="));
Serial.println();
}
void loop() {
// Process incoming GPS data
while (GPS.available() > 0) {
char c = GPS.read();
if (gps.encode(c)) {
displayGPSData(); // Call function to display parsed data
}
}
// Check if no valid data has been received for a while
if (millis() > 5000 && gps.charsProcessed() < 10) {
Serial.println(F("WARNING: No GPS data received. Check connections or signal."));
delay(5000); // Wait before rechecking
}
}
void displayGPSData() {
// Only display data if location is updated
if (gps.location.isUpdated()) {
Serial.println(F("===================================="));
Serial.println(F(" GPS Data Received "));
Serial.println(F("===================================="));
// Display Latitude and Longitude
Serial.print(F("Latitude: "));
Serial.print(gps.location.lat(), 6);
Serial.print(F("° "));
Serial.println(gps.location.rawLat().negative ? "S" : "N");
Serial.print(F("Longitude: "));
Serial.print(gps.location.lng(), 6);
Serial.print(F("° "));
Serial.println(gps.location.rawLng().negative ? "W" : "E");
// Display Date and Time
if (gps.date.isValid() && gps.time.isValid()) {
Serial.print(F("Date: "));
Serial.printf("%02d/%02d/%04d", gps.date.day(), gps.date.month(), gps.date.year());
Serial.println();
Serial.print(F("Time (UTC): "));
Serial.printf("%02d:%02d:%02d", gps.time.hour(), gps.time.minute(), gps.time.second());
Serial.println();
} else {
Serial.println(F("Date/Time: Not Available"));
}
// Display Altitude
if (gps.altitude.isValid()) {
Serial.print(F("Altitude: "));
Serial.print(gps.altitude.meters());
Serial.println(F(" meters"));
} else {
Serial.println(F("Altitude: Not Available"));
}
// Display Speed
if (gps.speed.isValid()) {
Serial.print(F("Speed: "));
Serial.print(gps.speed.kmph());
Serial.println(F(" km/h"));
} else {
Serial.println(F("Speed: Not Available"));
}
// Display Number of Satellites
if (gps.satellites.isValid()) {
Serial.print(F("Satellites in Use: "));
Serial.println(gps.satellites.value());
} else {
Serial.println(F("Satellites in Use: Not Available"));
}
// Display HDOP (Horizontal Dilution of Precision)
if (gps.hdop.isValid()) {
Serial.print(F("HDOP: "));
Serial.println(gps.hdop.value());
} else {
Serial.println(F("HDOP: Not Available"));
}
Serial.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment