/*
NMEA Sentence Builder
https://www.sparkfun.com/datasheets/GPS/NMEA%20Reference%20Manual1.pdf
See ReadMe
*/
const char* MSG_IDS[6] = {
"$GPGGA",
"$GPGLL",
"$GPGSA",
"$GPGSV",
"$GPRMC",
"$GPVTG"
};
const char UTC_TIME[] = {"171530.123"}; // hhmmss.sss
char sentence[85];
char csBuffer[3];
//double latitude = 4110.3962; // ddmm.mmmm N
//double longitude = 07427.1141; // dddmm.mmmm W
//double longitude = 99999.1141; // dddmm.mmmm W
int latitude = 0;
int longitude = 0;
int intLat = 0;
int intLong = 0;
int degLat = 0;
int minLat = 0;
char secLat[] = {"1234"};
int degLong = 0;
int minLong = 0;
char secLong[] = {"1234"};
char* ns = {"N"};
char* ew = {"W"};
//Message ID, Mode 1, mode 2, Sats Used, PDOP, HDOP, VDOP
//$GPGSA,A,3,07,02,26,27,09,04,15,,,,,,1.8,1.0,1.5*33
//Message ID , Number of Messages, Message Number1, Sats in view, Sat ID, Elevation, Azimuth, SNR, ...*CS<CR+LF>
//$GPGSV,2,1,07,07,79,048,42,02,51,062,43,26,36,256,42,27,27,138,42*71
//$GPGSV,2,2,07,09,23,313,42,04,19,159,41,15,12,041,42*41
//Message ID, UTC, Status, Lat, N/S, Long, E/W, Speed, Course, Date, Mag, Mode
//$GPRMC,UTC,A,Lat,NS,Long,EW,0.13,309.62,150824,,*CS
//Message ID, Course, Ref, Course, Ref, Speed, Units, Speed, Units, Mode
//$GPVTG,309.62,T,,M,0.13,N,0.2,K,A*23
byte makeChecksum(char *msg) {
byte checksum = 0;
for (int i = 1; i < strlen(msg); i++) {
checksum = checksum ^ (byte)msg[i];
}
return checksum;
}
// Message ID, UTC Time, Lat, N/S, Long, E/W, Fix, Sats, HDOP, MSL Altitude, Units, Geoid Separation, Units, Age of Diff. Corr, Diff. Ref. Station ID, Checksum, CR+LF
// $GPGGA,hhmmss.sss,ddmm.mmmm,N,dddmm.mmmm,W,1,07,1.0,100,M,,,,0000*CS<CR+LF>
char* makeGPGGA() {
char latBuff[10];
char longBuff[11];
char* csBuff;
//char* msg;
strncpy(sentence, MSG_IDS[0], 6);
strncat(sentence, ",", 1);
strncat(sentence, UTC_TIME, 10);
strncat(sentence, ",", 1);
//dtostrf(latitude, 6, 4, latBuff);
//Serial.print("latBuff: ");
//Serial.println(latBuff);
//strncat(latBuff, "\0", 1);
//if (strlen(latBuff) < 8) {
//strncat(sentence, "00", 2);
//} else if (strlen(latBuff) < 9) {
//strncat(sentence, "0", 1);
//}
if (intLat < 1000) {
strncat(sentence, "00", 2);
} else if (intLat < 100) {
strncat(sentence, "0", 1);
}
strncat(sentence, itoa(intLat, latBuff, 10), 4);
strncat(sentence, ".", 1);
strncat(sentence, secLat, 4);
//Serial.println(intLat);////////////////////////////////////////
//strncat(sentence, latBuff, 10);
//strncat(latBuff, "\0", 1);
strncat(sentence, ",", 1);
strncat(sentence, ns, 1);
strncat(sentence, ",", 1);
if (intLong < 100) {
strncat(sentence, "000", 3);
} else if (intLong < 1000) {
strncat(sentence, "00", 2);
} else if (intLong < 10000) {
strncat(sentence, "0", 1);
}
strncat(sentence, itoa(intLong, longBuff, 10), 4);
strncat(sentence, ".", 1);
strncat(sentence, secLong, 4);
//dtostrf(longitude, 6, 4, longBuff);
//strncat(longBuff, "\0", 1);
//Serial.println(strlen(latLongBuff));
//if (strlen(longBuff) < 8) {
//strncat(sentence, "000", 3);
//} else if (strlen(longBuff) < 9) {
//strncat(sentence, "00", 2);
//} else if (strlen(longBuff) < 10) {
//strncat(sentence, "0", 1);
//}
//strncat(sentence, longBuff, 11);
strncat(sentence, ",", 1);
strncat(sentence, ew, 1);
strncat(sentence, ",", 1);
strncat(sentence, "1,07,1.0,100,M,,,,0000", 22);
//Serial.println(sentence);
int cs = makeChecksum(sentence);
Serial.print("GPGGA cs: ");
Serial.println(cs);
//csBuff = decToHexa(cs);
//strncat(csBuff, "\0", 1);
//Serial.println(csBuff);
//Serial.println(decToHexa(cs));
strncat(sentence, "*", 1);
csBuff = decToHexa(cs);
Serial.println(csBuff);
strncat(sentence, csBuff, 2);
strncat(sentence, "\r\n", 2);
strncat(sentence, "\0", 1);
//strncat(sentence, msg, 5);
//strncat(sentence, "\0", 1);
Serial.println(sentence);
return sentence;
}
// Message ID, Lat, N/S, Long, E/W, UTC, Status, Mode
// $GPGLL,Lat,NS,Long,EW,UTC,A,A*CS
char* makeGPGLL() {
char latBuff[12];
char longBuff[12];
char* csBuff;
strncpy(sentence, MSG_IDS[1], 6);
strncat(sentence, ",", 1);
dtostrf(latitude, 6, 4, latBuff);
if (strlen(latBuff) < 8) {
strncat(sentence, "00", 2);
} else if (strlen(latBuff) < 9) {
strncat(sentence, "0", 1);
}
Serial.println(latBuff);
strncat(sentence, latBuff, 9);
strncat(sentence, ",", 1);
strncat(sentence, ns, 1);
strncat(sentence, ",", 1);
dtostrf(longitude, 6, 4, longBuff);
//Serial.println(strlen(latLongBuff));
if (strlen(longBuff) < 8) {
strncat(sentence, "000", 3);
} else if (strlen(longBuff) < 9) {
strncat(sentence, "00", 2);
} else if (strlen(longBuff) < 10) {
strncat(sentence, "0", 1);
}
strncat(sentence, longBuff, 10);
strncat(sentence, ",", 1);
strncat(sentence, ew, 1);
strncat(sentence, ",", 1);
strncat(sentence, UTC_TIME, 10);
strncat(sentence, ",", 1);
strncat(sentence, "A,A", 3);
int cs = makeChecksum(sentence);
//Serial.print("setup cs: ");
//Serial.println(cs);
csBuff = decToHexa(cs);
//strncat(csBuff, "\0", 1);
//Serial.println(csBuff);
//Serial.println(decToHexa(cs));
strncat(sentence, "*", 1);
//csBuff = decToHexa(cs);
strncat(sentence, csBuff, 2);
strncat(sentence, "\r\n", 2);
strncat(sentence, "\0", 1);
//strncat(sentence, msg, 5);
//strncat(sentence, "\0", 1);
//Serial.println(sentence);
//strncat(sentence, "\0", 1);
//Serial.println(sentence);
return sentence;
}
char* decToHexa(int n) {
csBuffer[0] = intToHex(n / 16);
csBuffer[1] = intToHex(n % 16);
//csBuffer[0] = '1';
//csBuffer[1] = 'D';
csBuffer[2] = '\0';
return csBuffer;
}
char intToHex(int value) {
char hexChar;
if (value < 10) {
hexChar = value + 48;
} else {
hexChar = value + 55;
}
return hexChar;
}
void setup() {
Serial.begin(115200);
}
void loop() {
char* msg1;
char* msg2;
//char* csBuff;
degLat = map(analogRead(A1), 0, 1023, -90, 90);
minLat = 50;
//secLat = 1234;
degLong = map(analogRead(A0), 0, 1023, -180, 180);
minLong = 50;
//secLong = 1234;
if (degLat < 0) {
ns = "S";
degLat = abs(degLat);
} else {
ns = "N";
}
if (degLong < 0) {
ew = "W";
degLong = abs(degLong);
} else {
ew = "E";
}
intLat = (degLat * 100) + minLat;
intLong = (degLong * 100) + minLong;
Serial.println(latitude);
Serial.println(longitude);
msg1 = makeGPGGA();
Serial.println(msg1);
//msg2 = makeGPGLL();
//Serial.println(msg2);
//Serial.println(makeGPGLL());
/*
//char* sentence = {"$GPGGA,171530.123,4110.3960,N,07427.1143,W,1,07,1.0,100,M,,,,0000"};
//Serial.println(sentence);
int cs = makeChecksum(msg);
//int cs = 29;
Serial.print("setup cs: ");
Serial.println(cs);
csBuff = decToHexa(cs);
strncat(csBuff, "\0", 1);
Serial.println(csBuff);
//Serial.println(decToHexa(cs));
strncat(msg, "*", 1);
//csBuff = decToHexa(cs);
strncat(msg, csBuff, 2);
strncat(msg, "\0", 1);
*/
delay(3000);
}