I have this code:
#include <SoftwareSerial.h>
// Define the GPS module connections
#define GPS_TX_PIN 10
#define GPS_RX_PIN 11
// Create a software serial object to communicate with the GPS module
SoftwareSerial gpsSerial(GPS_TX_PIN, GPS_RX_PIN);
void setup() {
// Initialize the serial communication
Serial.begin(9600);
gpsSerial.begin(9600);
// Debugging
Serial.println("GPS Module Initialized");
}
void loop() {
// Check if data is available from the GPS module
if (gpsSerial.available()) {
// Read the data from the GPS module
char c = gpsSerial.read();
// Check if the received character is the start of a GPS sentence
if (c == '$') {
String sentence = gpsSerial.readStringUntil('\n');
// Debugging: Print raw GPS data
Serial.println(sentence);
// Check if the sentence is a GGA sentence
if (sentence.startsWith("$GPGGA")) {
// Split the sentence into individual parts using commas as separators
int commaIndex = sentence.indexOf(',');
int i = 1;
while (commaIndex != -1) {
commaIndex = sentence.indexOf(',', commaIndex + 1);
if (commaIndex != -1) {
i++;
}
}
// Extract the latitude and longitude from the GGA sentence
String latitude = getValue(sentence, ',', i + 1);
String longitude = getValue(sentence, ',', i + 3);
// Print the coordinates to the serial monitor
Serial.print("Latitude: ");
Serial.println(latitude);
Serial.print("Longitude: ");
Serial.println(longitude);
}
}
}
}
// Helper function to extract a value from a comma-separated string
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = { 0, -1 };
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
It is connected to an Arduino Mega2560 clone. VCC is connected to 5v (actually 4.81v), GND to GND, RX to pin 11, TX to pin 10.
The LED on the module does not power on.
The Serial monitor returns this:
GPS Module Initialized
GPRMC,,V,,,,,,,,,,N*53
GPVTG,,,,,,,,,N*30
GPGGA,,,,,,0,00,99.99,,,,,,*48
GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30
GPGSV,1,1,00*79
GPGLL,,,,,,V,N*64
It loops, returning slightly different numbers every time.
I have taken it to a wide cloud free sky and it still doesn't work, doesn't work indoors either.
This is my module:
It is brand new, this is the first time it has been used.
Does anyone have any ideas?