I am trying to get GPS data from my GY-NEO6MV2, but the results are not readable with SoftwareSerial or other libraries. I have used the following code that has worked for others on the net, but I don't get any results from it. The GPS data appears perfectly in the serial monitor, but the interfaces such as SoftwareSerial fail to receive it. Instead, I receive a bunch of -1
s. Here is the code:
#include "SoftwareSerial.h"
// Constants
#define txPin 1 //tx pin in GPS connection
#define rxPin 2 //rx pin in GPS connection
// Set up the GPS serial port
SoftwareSerial gps = SoftwareSerial(rxPin, txPin);
// Variables
byte byteGPS = 0;
int i = 0;
int state = 0;
char dataGPG[100] = "";
char *pch;
char *GGA[15];
int sat = 0;
void setup() {
//setup for Serial Port
Serial.begin(9600);
Serial.flush();
//setup for GPS Serial Port
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
gps.begin(4800);
gps.flush();
//setup satellites signal
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
// Turn off the led until a satellite signal
}
void loop() {
gps.flush();
Serial.flush();
Serial.println("Waiting GPS data...");
// Prepare all for reading GPS Serial Port
memset(dataGPG, 0, sizeof(dataGPG)); // Remove previous readings
byteGPS = 0; // Remove data
byteGPS = gps.read(); // Read the byte that is in the GPS serial port
delay(1000);
// Find the desired string
while (byteGPS != '$') {
byteGPS = gps.read();
/******* Here I just get -1 *************/
}
// Save the string in an array
i = 1;
dataGPG[0] = '$';
while (byteGPS != '*' ) {
byteGPS = gps.read();
dataGPG[i] = byteGPS;
i++;
}
dataGPG[i] = '\0';
string(); // Call to the function that manipulates our string
}
/*
This function will allow us to identify the data we need to get the longitude, latitude ...
*/
void string() {
i = 0;
memset(GGA, 0, sizeof(GGA)); // Remove previous readings
pch = strtok (dataGPG, ",");
// Analyze the saved interval in pch to see if it the needed string
if (strcmp(pch, "$GPGGA") == 0) {
while (pch != NULL) {
pch = strtok (NULL, ",");
GGA[i] = pch;
i++;
}
plot(GGA, "$GPGGA");
// Call to the function that is going to display the data
}
}
/*
This function organize the gps data received for printing in the serial monitor.
*/
void plot(char **GGAPrint, char *trama) {
state = atoi(GGAPrint[5]);
sat = atoi(GGAPrint[6]);
if (trama == "$GPGGA" && state == 1) {
digitalWrite(13, HIGH);
// Then there are satellites, the LED switch ON
Serial.println("");
Serial.println("----------------------------------------------");
Serial.print("UTC Hour -> ");
Serial.println(GGAPrint[0]);
Serial.print("Latitude -> ");
Serial.print(GGAPrint[1]);
Serial.println(GGAPrint[2]);
Serial.print("Longitude -> ");
Serial.print(GGAPrint[3]);
Serial.println(GGAPrint[4]);
Serial.print("GPS quality: 0=null; 1=GPS fixed -> ");
Serial.println(GGAPrint[5]);
Serial.print("Number of satellites -> ");
Serial.println(sat);
Serial.print("Horizontal Dilution of Precision -> ");
Serial.println(GGAPrint[7]);
Serial.print("Antenna altitude -> ");
Serial.print(GGAPrint[8]);
Serial.println(GGAPrint[9]);
Serial.print("Geoid Separation -> ");
Serial.print(GGAPrint[10]);
Serial.println(GGAPrint[11]);
Serial.println("----------------------------------------------");
Serial.println("");
} else {
// If no satellite connection
digitalWrite(13, LOW); // Turn off the LED
Serial.println("");
Serial.println("-----------------------------");
Serial.print("|--- Satellites Used -->");
Serial.print(sat);
Serial.println(" |");
Serial.println("|----Waiting location----|");
Serial.println("-----------------------------");
Serial.println("");
}
}
The result wasn't correct and I only saw this sentence:
Waiting GPS data...
I updated the code by adding gps.available()
and delay(2)
before every gps.read()
and it worked. I still don't know why, but this worked:
#include "SoftwareSerial.h"
// Constants
#define txPin 2 //tx pin in GPS connection
#define rxPin 3 //rx pin in GPS connection
char gps_data[200];
SoftwareSerial gps = SoftwareSerial(rxPin, txPin);
char byteGPS = 0;
void setup() {
Serial.begin(9600);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
gps.begin(9600);
}
void loop() {
byteGPS = '0'; // Remove data
memset(gps_data, '\0', 200);
while (gps.available())
{
while (gps.available())
{
byteGPS = gps.read(); // Read the char that is in the GPS serial port
if (byteGPS == '$') break;
}
int i=0;
while (gps.available()) // Available3
{
delay(1);
gps_data[i++] = byteGPS;
byteGPS = gps.read();
if ((byteGPS == '\n')) break;
}
if (i > 1)
{
delay(1);
parse_gps_data();
}
}
}
The code in parse_gps_data()
parses the data from gps. It works fine.
gps.flush()
. If you clear the input buffer that is receiving the GPS data every second or so its possible you will loose data. – Code Gorilla Feb 3 '17 at 13:19