String prefix="";
String GPRMC="";
String GPGGA="";
long lattitude; // imma make these the angle x 10^6 to avoid using floats
long longitude;
long altitude;
long fixtime; // this is time in seconds x 1000
long fixdate;
int dltm=209; /* the delay in us between serial reads,
used just for some troubleshooting .
So far I believe it is (number of us in a second)/(baud x 8)
= 1000000/(38400*8) = 208.3333
*/
void setup(){
Serial.begin(38400);
digitalWrite(13,HIGH);
}
void loop(){
if(Serial.available()){
prefix="";
for(int i=0;i<6;i++){
prefix+=byte(Serial.read());
delayMicroseconds(dltm);
}
/* okay for this 'while' bit, the code looks to see if
prefix == "$GPRMC" ; otherwise it shifts all the
characters along to the left by one and adds the next
serial character on and compares again. It keeps doing
this until $GPRMC is found or the serial buffer is
empty. Then if prefix is equal to GPRMC or GPGGA, well
I'm sure you can guess that it reads the appropriate data into
the apropriate string. /
while(Serial.available()){
if(prefix=="$GPGGA"){
GPGGA="";
while(Serial.available()){
// this adds bytes to GPGGA until * is reached,
// signalling the end of the GPGGA part.
GPGGA+=byte(Serial.read());
if(GPGGA.endsWith("")){
break;
}
delayMicroseconds(dltm);
}
Serial.println("$GPGGA"+GPGGA);
}
if(prefix=="$GPRMC"){
GPRMC="";
while(Serial.available()){
// this adds bytes to GPRMC until there are none
// left, since its the last part.
GPRMC+=byte(Serial.read());
delayMicroseconds(dltm);
}
Serial.println("$GPMRC"+GPRMC);
}
else{
prefix=prefix.substring(1)+byte(Serial.read());
delayMicroseconds(dltm);
}
}
/* okay, now lets interpret the GPGGA and GPRMC strings!
Since the bt-359 GPS wierdly only sends a GPMRC 4 times every
5 seconds, I will be taking the position from the GPGGA
string which is updated every second, and then the speed and
date from GPRMC since GPGGA doesn't have this info.
If you are using a better GPS that doesn't have this freakout,
you might want to just get everything from the GPRMC for
simplicity. */
/* since the NMEA gives your position in degrees minutes and seconds
and I simply hate that, these minute/seconds stupid things will
be converted to decimal, I don't know if this is the best way but
yeah, whatevs */
/* all the commented Serial.print's are for debugging purposes,
I am quite a noob at this! */
String lattdecstrng1=GPGGA.substring(14,16);
String lattdecstrng2=GPGGA.substring(17,19);
String lattdecstrng3=GPGGA.substring(19,21);
// Serial.print(lattdecstrng1);
// Serial.print(lattdecstrng2);
// Serial.println(lattdecstrng3);
char lattdecchar1[3];
char lattdecchar2[3];
char lattdecchar3[3];
lattdecstrng1.toCharArray(lattdecchar1, 3);
lattdecstrng2.toCharArray(lattdecchar2, 3);
lattdecstrng3.toCharArray(lattdecchar3, 3);
// Serial.print(lattdecchar1);
// Serial.println(lattdecchar2);
// Serial.println("now the above mapped =");
int lattdecunmapped1=atoi(lattdecchar1);
int lattdecunmapped2=atoi(lattdecchar2);
int lattdec3=atoi(lattdecchar3);
// lattdecstrng3 is already in a base 10 from
long lattdec1=lattdecunmapped11000000/60;
long lattdec2=lattdecunmapped2lattdec32.7778; // same as (lat210^6)/((lat3/100)*3600)
// Serial.println(lattdec1);
// Serial.println(lattdec2);
long lattdec=lattdec1+lattdec2;
// wow all that shit there just for the DECIMAL part of lattitude
String lattintstrng=GPGGA.substring(13,15);
char lattintchar[3];
lattintstrng.toCharArray(lattintchar, 3);
long lattint=atoi(lattintchar)*1000000;
lattitude=lattint+lattdec;
Serial.print("lattitude in degrees x 10^6 : ");
Serial.println(lattitude);
// OKAY now for longitude
String longdecstrng1=GPGGA.substring(27,29);
String longdecstrng2=GPGGA.substring(30,32);
String longdecstrng3=GPGGA.substring(32,34);
char longdecchar1[3];
char longdecchar2[3];
char longdecchar3[3];
longdecstrng1.toCharArray(longdecchar1, 3);
longdecstrng2.toCharArray(longdecchar2, 3);
longdecstrng3.toCharArray(longdecchar3, 3);
int longdecunmapped1=atoi(longdecchar1);
int longdecunmapped2=atoi(longdecchar2);
int longdec3=atoi(longdecchar3);
// lattdecstrng3 is already in a base 10 from
long longdec1=longdecunmapped11000000/60;
long longdec2=longdecunmapped2longdec3*2.7778;
// Serial.println(longdec1);
// Serial.println(longdec2);
long longdec=longdec1+longdec2;
// wow all that shit there just for the DECIMAL part of longitude
String longintstrng=GPGGA.substring(24,27);
char longintchar[4];
longintstrng.toCharArray(longintchar, 4);
long longint=atoi(longintchar)*1000000;
longitude=longint+longdec;
Serial.print("longitude in degrees x 10^6 : ");
Serial.println(longitude);
// phew, what a bloody handful.
// okay,
}
}