I've been trying for the past few weeks to get the Arduino GPS Shield that I am using to return its location, however everytime I do return any data from Serial 1-3, the data can't be encoded by the TinyGPS Plus library. I'm currently using an Arduino Mega 2560. You can view my code and my most recent debugging below.
#include <TinyGPS++.h>
const long COOLDOWN = 10000;
long start = 0;
TinyGPSPlus gps;
bool serial1_availability = false;
bool serial2_availability = false;
bool serial3_availability = false;
void setup() {
start = millis();
Serial1.begin(9600); //make sure Baud rate is correct
Serial2.begin(9600); //make sure Baud rate is correct
Serial3.begin(9600); //make sure Baud rate is correct
Serial.begin(9600); //make sure Baud rate is correct
Serial.println("====================");
Serial.println(" >> GPS START <<");
Serial.println("====================");
}
void loop() {
if(millis() - start >= COOLDOWN){
Serial.print("*");
start = millis();
}
while (Serial1.available()) {
if(!serial1_availability){
Serial.println("");
Serial.println("Serial1 Available!");
serial1_availability = true;
gps.encode(Serial1.read());
if (gps.location.isUpdated()){
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
}
}
printData(Serial1.read());
}
while (Serial2.available()) {
if(!serial2_availability){
Serial.println("");
Serial.println("Serial2 Available!");
serial2_availability = true;
gps.encode(Serial2.read());
if (gps.location.isUpdated()){
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
}
}
printData(Serial2.read());
}
while(Serial3.available()) {
if(!serial3_availability){
Serial.println("");
Serial.println("Serial3 Available!");
serial3_availability = true;
gps.encode(Serial3.read());
if (gps.location.isUpdated()){
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
}
}
printData(Serial3.read());
}
if(serial1_availability){
Serial.println("Serial1 Is No Longer Available!");
serial1_availability = false;
}
if(serial2_availability){
Serial.println("Serial2 Is No Longer Available!");
serial2_availability = false;
}
if(serial3_availability){
Serial.println("Serial3 Is No Longer Available!");
serial3_availability = false;
}
}
void printData(char c) {
Serial.write('\\'); // Put in a marker...
Serial.write(c+ 0x20); // ... and shift it up to the printable characters
Serial.write('\\'); // Put in another marker.
}
I would expect the gps to be on a single serial port. Even if you have multiple gps units, each of them will need to be handled by it's own TinyGPSPlus instance.
I too cannot work out why you would want to read all 3 serial ports on the Mega, when the GPS is only connected to one.
In addition every time you receive one character (@9600baud) from the GPS you print out 20 characters at 9600baud to the serial console, cant quite see how the serial console keeps up ...............
I'm currently using an Arduino Mega 2560 with an Arduino GPS Shield (NEO-6M).
Antenna: ANT-555
I'm not too familiar with the Arduino Mega 2560. I was told I should utilize the "special" serial ports that the Mega provides instead of the SoftwareSerial ports. I wasn't sure which port to use so I tested all three to see which one returned any data at all during my debugs. To my surprise all three would occassionally return data (see debug from the link provided in my original post). I've set the baud rate to 9600 since it has so far worked for me and is recommended for the GPS Shield I am using.
Hopefully this clears a few things up.
Is there a specific serial port I should use though? I assume there's three for each of their own uses. I don't understand why I would receive data from all three at different intervals. Also I have tried sitting outside for up to half an hour usually, I get most of my data usually around the 20 minute mark, however the TinyGPS library is unable to encode the data. I've been trying to figure out what I've been doing wrong.
Try this lengthy program, it reads characters from the GPS and sends them to the IDE serial monitor, its essential to check that the GPS is working and it gets a fix.
/*
*******************************************************************************************************************************
HardwareSerial1_GPS_Echo
Stuart Robinson
www.LoRaTracker.uk
Reads characters from a GPS using the Arduinos Hardware Serial1 port and echos them to the Arduino Serial Monitor at
115200 baud.
*******************************************************************************************************************************
*/
#define GPSBaud 9600 //GPS Baud rate
#define Serial_Monitor_Baud 115200 //this is baud rate used for the Arduino IDE Serial Monitor
void loop()
{
while (Serial1.available() > 0)
Serial.write(Serial1.read());
}
void setup()
{
Serial.begin(Serial_Monitor_Baud); //setup Serial monitor ouput
Serial.println("GPS Serial1 Echo Starting");
Serial1.begin(GPSBaud); //start Hardware serial for GPS at defined baud rate
}
srnet:
Try this lengthy program, it reads characters from the GPS and sends them to the IDE serial monitor, its essential to check that the GPS is working and it gets a fix.
Gave the code a try. It prints "GPS Serial1 Echo Starting" successfully however I'm not sure how that would help prove if my GPS Shield is infact working or not since I am not printing any of the data that is being read from Serial1 when Serial1 port is available. I modified my code to a similar setup with what you had, I printed the data being read, but I'm not getting much information. I'm simply reading a value of -1 once in a while. I'm not receiving though lines of data which I would expect.
#include <TinyGPS++.h>
const long COOLDOWN = 10000;
long start = 0;
TinyGPSPlus gps;
void setup() {
start = millis();
Serial.begin(115200);
Serial1.begin(9600); //make sure Baud rate is correct
Serial.println("====================");
Serial.println(" >> GPS START <<");
Serial.println("====================");
}
void loop() {
if(millis() - start >= COOLDOWN){
Serial.print("*");
start = millis();
}
while (Serial1.available() > 0) {
Serial.write(Serial1.read());
Serial.print(Serial1.read());
Serial.print(Serial.read());
Serial.println("o");
}
gps.encode(Serial1.read());
if (gps.location.isUpdated()){
Serial.print("Latitude= ");
Serial.print(gps.location.lat(), 6);
Serial.print(" Longitude= ");
Serial.println(gps.location.lng(), 6);
}
}
void printData(char c) {
Serial.write('\\'); // Put in a marker...
Serial.write(c+ 0x20); // ... and shift it up to the printable characters
Serial.write('\\'); // Put in another marker.
}
Just go back to srnet's basic sketch. Run separate versions of it for serial1, serial2 & serial3. Adjust the baud rate for the gps if necessary. Until you see NMEA sentences in the serial monitor, there is no point trying to move on to gps.encode.
Furthermore, when you are sending data to the gps object, send all of it. I'm unclear what your code is trying to do, but you're sabotaging any chance it might have of reading the GPS data by throwing some of it away.
Why are you doing three reads below?
What if Serial1.available returns 1, indicating that exactly one character is available?
Why do you do one Serial.write() and two Serial.prints()?
while (Serial1.available() > 0) {
Serial.write(Serial1.read());
Serial.print(Serial1.read());
Serial.print(Serial.read());
Hint: documentation is available. Please spend some time actually reading it.