Hi all, I having trouble with my Mega 2560. I have an NEO-6M GPS module connected up to Serial 1 pins (RX18 TX19) and a Nextion LCD to Serial 3 (RX15 TX14). My issue seems that I cannot correctly read the GPS. If I run the below code, it seems that I'm only getting the time and date once. The time and date I get are then repeated as the same values over and over. If I change the code and upload onto an UNO, everything works with no issues. I believe the issue is how I'm using the serials on the Mega. No matter how I set the Serials by define or leaving it as the actual names, I can't get any further with this and I'm looking for a fresh pair of eyes on the code. the code is part of a much larger project. So what I have done is created a new project and start with just getting the GPS informations. When I add the Nextion stuff, this is where I seems to have an issues. Then serial outputs to the Nextion work every time.
I have searched the forums and I can't seem to see an issue like mine.
#include <TinyGPS++.h>
#include "EasyNextionLibrary.h" // Include EasyNextionLibrary
// The TinyGPS++ object
TinyGPSPlus gps;
#define nexSer Serial3
EasyNex myNex(nexSer); // Create an object of EasyNex class with the name < myNex >
// Serial Ports RX = 0 TX = 1
// Serial 1 Ports RX = 19 TX = 18
// Serial 2 Ports RX = 17 TX = 16
// Serial 3 Ports RX = 15 TX = 14
// GPS Ports are RX=15 and TX = 14 (Serial 3 Ports)
#define GPSSerial Serial1 // Setup name of Serial 1 to be used by GPS
static const uint32_t GPSBaud = 9600;// GPS Buad Rate
static const uint32_t DeBugBaud = 115200;// GPS Buad Rate
#define BugSerial Serial // Setup name of Serial for debugging
String MyFullTime = "", MyFullDate = "", MyGPSVersion = "Estate GPS Libary Version: ";
void setup() {
MyGPSVersion += TinyGPSPlus::libraryVersion();
BugSerial.begin(115200);
while (!BugSerial);
BugSerial.println(F("Serial: Serial open"));
GPSSerial.begin(GPSBaud);
while (!GPSSerial);
BugSerial.println(F("Serial: GPSSerial open"));
nexSer.begin(9600);
while (!nexSer);
BugSerial.println(F("Serial: LCDSerial open"));
BugSerial.print(F("Information: "));
BugSerial.print(F("Estate GPS Libary Version: ")); Serial.print(TinyGPSPlus::libraryVersion());
BugSerial.println();
myNex.writeStr("tGPSVersion.txt", MyGPSVersion);
myNex.writeStr("tInformation.txt", "Information: Powered on and waiting GPS Lines");
myNex.writeStr("tStatus.txt", "Information: Waiting on GPS module");
}
void loop() {
while (GPSSerial.available()) {
gps.encode(GPSSerial.read());
BugSerial.println(F("Information GPS: Reading GPS module"));
myNex.writeStr("tStatus.txt", "Information: Reading GPS module");
GetMyTimeDate();
GetMyTime();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
BugSerial.println(F("Warning: No GPS detected: check wiring."));
myNex.writeStr("tStatus.txt", "Information: No new lines");
myNex.writeStr("tInformation.txt", "Warning GPS: No new line in over 5 seconds. Is the GPS module flashing yet?");
while (true);
}
}
}
void GetMyTimeDate() {
if (gps.date.isValid() == true) {
BugSerial.println(F("Information GPS: GPS date valid"));
myNex.writeStr("tStatus.txt", "Information: Updating Date");
char buffer [10] = "";
sprintf(buffer, "%02d/%02d/%04d", gps.date.day(), gps.date.month(), gps.date.year());
MyFullDate = buffer;
myNex.writeStr("tGPSDate.txt", MyFullDate);
buffer [10] = "";
BugSerial.print(F("Information GPS: GPS Date: "));
BugSerial.println(MyFullDate);
myNex.writeStr("tStatus.txt", "Information: Date updated");
}
}
void GetMyTime() {
BugSerial.println(F("Information GPS: GPS time valid"));
myNex.writeStr("tStatus.txt", "Information: Updating time");
gps.encode(GPSSerial.read());
char buffer [13] = "";
sprintf(buffer, "%02d:%02d:%02d", gps.time.hour(), gps.time.minute(), gps.time.second());
MyFullTime = buffer;
myNex.writeStr("tGPSTime.txt", MyFullTime);
BugSerial.print(F("Information GPS: GPS Time: "));
BugSerial.println(MyFullTime);
myNex.writeStr("tStatus.txt", "Information: Time updated");
}