HOW TO DETECT AND PARSE INCOMING GPS DATA FROM SERIAL1 - Arduino MEGA x SIM808

Hi guys. I need help regarding on parsing the incoming GPS(GPSGGA) from Serial1.

My first code to try parsing was this:

#include <SoftwareSerial.h>

String GPSGGA="+GPSRD:$GPGGA,074315.000,1446.78990,N,12052.76583,E,1,03,2.2,-0.0,M,M,00006B\n$GPRMC,074315.000,A,1446.78990,N,12052.76583,E,3.25,312.71,200217,A6D\n$GPVTG,312.71,T,M,3.25,N,6.01,K,A*38";

int FirstIndex=GPSGGA.indexOf(’,’);
int SecondIndex=GPSGGA.indexOf(’,’,FirstIndex+1);
int ThirdIndex=GPSGGA.indexOf(’,’,SecondIndex+1);
int FourthIndex=GPSGGA.indexOf(’,’,ThirdIndex+1);
int FifthIndex=GPSGGA.indexOf(’,’,FourthIndex+1);
int SixthIndex=GPSGGA.indexOf(’,’,FifthIndex+1);
int SeventhIndex=GPSGGA.indexOf(’,’,SixthIndex+1);
int EighthIndex=GPSGGA.indexOf(’,’,SeventhIndex+1);
int NinthIndex=GPSGGA.indexOf(’,’,EighthIndex+1);
int TenthIndex=GPSGGA.indexOf(’,’,NinthIndex+1);
int EleventhIndex=GPSGGA.indexOf(’,’,TenthIndex+1);
int TwelfthIndex=GPSGGA.indexOf(’,’,EleventhIndex+1);
int ThirteenthIndex=GPSGGA.indexOf(’,’,TwelfthIndex+1);
int FourteenthIndex=GPSGGA.indexOf(’,’,ThirteenthIndex+1);
int FifteenthIndex=GPSGGA.indexOf(’,’,FourteenthIndex+1);
int SixteenthIndex=GPSGGA.indexOf(’\n’,FifteenthIndex+1);

String GPS_Type=GPSGGA.substring(0,FirstIndex);
String GPS_UTC=GPSGGA.substring(FirstIndex+1,SecondIndex);
String GPS_Latitude=GPSGGA.substring(SecondIndex+1,ThirdIndex);
String GPS_LatitudeNS=GPSGGA.substring(ThirdIndex+1,FourthIndex);
String GPS_Longitude=GPSGGA.substring(FourthIndex+1,FifthIndex);
String GPS_LongitudeEW=GPSGGA.substring(FifthIndex+1,SixthIndex);
String GPS_Quality=GPSGGA.substring(SixthIndex+1,SeventhIndex);
String GPS_SatellitesNumber=GPSGGA.substring(SeventhIndex+1,EighthIndex);
String GPS_HDOP=GPSGGA.substring(EighthIndex+1,NinthIndex);
String GPS_AntennaASL=GPSGGA.substring(NinthIndex+1,TenthIndex);
String GPS_AntennaHeightUnit=GPSGGA.substring(TenthIndex+1,EleventhIndex);
String GPS_GeoidalSeparation=GPSGGA.substring(EleventhIndex+1,TwelfthIndex);
String GPS_GeoidalSeparationUnit=GPSGGA.substring(TwelfthIndex+1,ThirteenthIndex);
String GPS_Age=GPSGGA.substring(ThirteenthIndex+1,FourteenthIndex);
String GPS_DifferentialReferenceStationID=GPSGGA.substring(FourteenthIndex+1,FifteenthIndex);
String GPS_Checksum=GPSGGA.substring(FifteenthIndex+1,SixteenthIndex);

void setup() {
Serial.begin(9600);

String Latitude1=GPS_Latitude.substring(0,2);
String Latitude2=GPS_Latitude.substring(2,4);
String Latitude3=GPS_Latitude.substring(4,10);
String Longitude1=GPS_Longitude.substring(0,3);
String Longitude2=GPS_Longitude.substring(3,5);
String Longitude3=GPS_Longitude.substring(5,11);

int Lat1=Latitude1.toInt();
int Lat2=Latitude2.toInt();
float Lat3=Latitude3.toFloat();
int Lon1=Longitude1.toInt();
int Lon2=Longitude2.toInt();
float Lon3=Longitude3.toFloat();

float Lat32=Lat360;
float Lon32=Lon3
60;

Serial.print(“GPS Data\n”);
Serial.print(“UTC: “);
Serial.println(GPS_UTC);
Serial.print(”\nLatitude: “);
Serial.println(GPS_Latitude+”,”+GPS_LatitudeNS);
Serial.print("\nLongitude: “);
Serial.println(GPS_Longitude+”,"+GPS_LongitudeEW);

Serial.print("\n\nDATA CONVERSION:");
Serial.print("\nLatitude: ");
Serial.println(Latitude1+"deg "+Latitude2+"min “+Lat32+“sec”+”, "+GPS_LatitudeNS);
Serial.print("Longitude: ");
Serial.println(Longitude1+"deg "+Longitude2+"min “+Lon32+“sec”+”, "+GPS_LongitudeEW);
}

void loop() {}

// -------------------------------------

This give me the results on Serial Monitor like this:

GPS Data
UTC: 074315.000

Latitude: 1446.78990,N

Longitude: 12052.76583,E

DATA CONVERSION:
Latitude: 14deg 46min 47.39sec, N
Longitude: 120deg 52min 45.95sec, E

// ------------

but what I need is the String GPSGGA=""; is currently reading the GPS data from Serial1, then once the GPS Quality (GPSGGA) detects a value greater than “0”. That was the time it will print the converted data to the Monitor or on a LCD.

I’m experimenting on some codes but I can’t make it work.

this is my latest code:

//SETUP & CONFIGURATION

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>

SoftwareSerial GSMSerial(19, 18);
LiquidCrystal lcd(8,10,4,5,6,7);

//Declaration

String GPSGGA="";
char CTRLZ=0x1A;

void setup() {
Serial.begin(9600);
Serial1.begin(38400);
lcd.begin(16,2);
delay(40000);
Serial1.print("\n");
Serial1.print(“AT\r\n”);
delay(1000);

Serial1.print(“AT+CMGF=1\r\n”);
delay(1500);
Serial1.print(“AT+CMGS=”“09276037794"”\r\n");
delay(1000);
Serial1.print(“Hello pows\r\n”);
delay(500);
Serial1.print(CTRLZ);

delay(5000);
Serial1.print(“AT+GPS=1\r\n”);
delay(5000);
Serial1.print(“AT+GPSRD=1\r\n”);
delay(3000);

}

void loop()
{
GPSGGA=((String)Serial1.read());

int FirstIndex=GPSGGA.indexOf(’,’);
int SecondIndex=GPSGGA.indexOf(’,’,FirstIndex+1);
int ThirdIndex=GPSGGA.indexOf(’,’,SecondIndex+1);
int FourthIndex=GPSGGA.indexOf(’,’,ThirdIndex+1);
int FifthIndex=GPSGGA.indexOf(’,’,FourthIndex+1);
int SixthIndex=GPSGGA.indexOf(’,’,FifthIndex+1);
int SeventhIndex=GPSGGA.indexOf(’,’,SixthIndex+1);
int EighthIndex=GPSGGA.indexOf(’,’,SeventhIndex+1);
int NinthIndex=GPSGGA.indexOf(’,’,EighthIndex+1);
int TenthIndex=GPSGGA.indexOf(’,’,NinthIndex+1);
int EleventhIndex=GPSGGA.indexOf(’,’,TenthIndex+1);
int TwelfthIndex=GPSGGA.indexOf(’,’,EleventhIndex+1);
int ThirteenthIndex=GPSGGA.indexOf(’,’,TwelfthIndex+1);
int FourteenthIndex=GPSGGA.indexOf(’,’,ThirteenthIndex+1);
int FifteenthIndex=GPSGGA.indexOf(’,’,FourteenthIndex+1);
int SixteenthIndex=GPSGGA.indexOf(’\n’,FifteenthIndex+1);

String GPS_Type=GPSGGA.substring(0,FirstIndex);
String GPS_UTC=GPSGGA.substring(FirstIndex+1,SecondIndex);
String GPS_Latitude=GPSGGA.substring(SecondIndex+1,ThirdIndex);
String GPS_LatitudeNS=GPSGGA.substring(ThirdIndex+1,FourthIndex);
String GPS_Longitude=GPSGGA.substring(FourthIndex+1,FifthIndex);
String GPS_LongitudeEW=GPSGGA.substring(FifthIndex+1,SixthIndex);
String GPS_Quality=GPSGGA.substring(SixthIndex+1,SeventhIndex);
String GPS_SatellitesNumber=GPSGGA.substring(SeventhIndex+1,EighthIndex);
String GPS_HDOP=GPSGGA.substring(EighthIndex+1,NinthIndex);
String GPS_AntennaASL=GPSGGA.substring(NinthIndex+1,TenthIndex);
String GPS_AntennaHeightUnit=GPSGGA.substring(TenthIndex+1,EleventhIndex);
String GPS_GeoidalSeparation=GPSGGA.substring(EleventhIndex+1,TwelfthIndex);
String GPS_GeoidalSeparationUnit=GPSGGA.substring(TwelfthIndex+1,ThirteenthIndex);
String GPS_Age=GPSGGA.substring(ThirteenthIndex+1,FourteenthIndex);
String GPS_DifferentialReferenceStationID=GPSGGA.substring(FourteenthIndex+1,FifteenthIndex);
String GPS_Checksum=GPSGGA.substring(FifteenthIndex+1,SixteenthIndex);

String Latitude1=GPS_Latitude.substring(0,2);
String Latitude2=GPS_Latitude.substring(2,10);

String Longitude1=GPS_Longitude.substring(0,3);
String Longitude2=GPS_Longitude.substring(3,11);

float Lat1=Latitude1.toFloat();
float Lat2=Latitude2.toFloat();
float Lon1=Longitude1.toFloat();
float Lon2=Longitude2.toFloat();

float Lat22=Lat2/60;
float Lon22=Lon2/60;

float FinalLat=Lat1+Lat22;
float FinalLon=Lon1+Lon22;

int Quality=GPS_Quality.toInt();

if (Quality>=1)
{
Serial1.print("\n");
Serial1.print(“AT+GPSRD=0\r\n”);
lcd.print(FinalLat,8);
lcd.print(", “+GPS_LatitudeNS);
lcd.setCursor(0,2);
lcd.print(FinalLon,8);
lcd.print(”, "+GPS_LongitudeEW);
}
}

Sorry for the grammar and type of thread I created. It was my first time. Please consider it :slight_smile: THANKS!!

LCD_OUT_PARSING_DECIMAL_FORM.ino (2.97 KB)

sketch_feb23h.ino (3.37 KB)

No offense, but your sketches look ridiculous. Usually when that happens, you are not using good coding techniques.

Tip: There are several NMEA parsing libraries out there. Just find one that works for you and use it.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

Robin2: It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

Oh sorry, I didn't know that much. I just started programming about less than a month. That advise would be very helpful! :)