Thanks all.
I have changed the startMarker "headerGPS " to '$'. - Thanks cattledog
I have also included the code from wildbill to parse the date and time
My Code so far:
#include <SoftwareSerial.h>
SoftwareSerial gpsSerial(10, 11); // RX, TX
const byte charsGPS = 72;
char receivedGPS[charsGPS];
char tempGPS[charsGPS]; // temporary array for use when parsing
// variables to hold the parsed data
char gpsData[charsGPS] = {0};
char gpsHeader[6];
char gpsTime[7];
char gpsValid[2];
float gpsLat = 0.0000;
char gpsNS[2];
float gpsLon = 0.0000;
char gpsEW[2];
float gpsKnots = 0;
float gpsTrack = 0;
char gpsDate[6];
boolean newGPS = false;
//============
void setup() {
Serial.begin(9600);
gpsSerial.begin(9600);
gpsSerial.println("$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29");// GPS only uotputs $GPRMC message
}
//============
void loop() {
receiveGPS();
if (newGPS == true) {
strcpy(tempGPS, receivedGPS);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseGPS() replaces the commas with \0
parseGPS();
showGPS();
newGPS = false;
}
}
//============
void receiveGPS() {
static boolean recvGPSinProgress = false;
static byte ndxGPS = 0;
char headerGPS = '
Serial Monitor output:
Received Data: GPRMC,154326.000,A,2551.3714,S,02810.7593,E,0.02,131.12,211018,,,A*7C
Header: 000
Time: 154326.000
Valid: A
Latitude: 2551.3713
North/South: S
Longitude: 2810.7590
East/West: E
Knots: 0.02
Track: 131.12
Date: 211018
------------------------------------------------
Day: 21
Month: 10
Year: 18
Hours: 15
Minutes: 43
Seconds: 26
------------------------------------------------
All looking great - many thanks to all.
Now I have a major challenge:
I must print gpsLat / gpsLon / gpsNS / gpsEW as HEX as required by Sigfox.
I can only send 12 Bytes and need to send as follows
gpsLat + gpsNS as 5 bytes
gpsLon + gpsEW as 5 bytes
Any suggestions or pointers?;
char endstrGPS = '\n';
char rxGPS;
while (gpsSerial.available() > 0 && newGPS == false) {
rxGPS = gpsSerial.read();
if (recvGPSinProgress == true) {
if (rxGPS != endstrGPS) {
receivedGPS[ndxGPS] = rxGPS;
ndxGPS++;
if (ndxGPS >= charsGPS) {
ndxGPS = charsGPS - 1;
}
}
else {
receivedGPS[ndxGPS] = '\0'; // terminate the string
recvGPSinProgress = false;
ndxGPS = 0;
newGPS = true;
}
}
else if (rxGPS == headerGPS) {
recvGPSinProgress = true;
}
}
}
//============
void parseGPS() { // split the data into its parts
Serial.print("Received Data: ");
Serial.println(tempGPS);
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempGPS, ","); // get the first part - the string
strcpy(gpsHeader, strtokIndx); // copy it to gpsTime
strtokIndx = strtok(NULL, ","); // get the first part - the string
strcpy(gpsTime, strtokIndx); // copy it to gpsTime
strtokIndx = strtok(NULL, ","); // get the first part - the string
strcpy(gpsValid, strtokIndx); // copy it to gpsValid
strtokIndx = strtok(NULL, ",");
gpsLat = atof(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ","); // get the first part - the string
strcpy(gpsNS, strtokIndx); // copy it to gpsData
strtokIndx = strtok(NULL, ",");
gpsLon = atof(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ","); // get the first part - the string
strcpy(gpsEW, strtokIndx); // copy it to gpsEW
strtokIndx = strtok(NULL, ",");
gpsKnots = atof(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
gpsTrack = atof(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ","); // get the first part - the string
strcpy(gpsDate, strtokIndx); // copy it to gpsDate
}
//============
void showGPS() {
int latHEX;
Serial.print("Header: ");
Serial.println(gpsHeader);
Serial.print("Time: ");
Serial.println(gpsTime);
Serial.print("Valid: ");
Serial.println(gpsValid);
Serial.print("Latitude: ");
Serial.println(gpsLat,4);
Serial.print("North/South: ");
Serial.println(gpsNS);
Serial.print("Longitude: ");
Serial.println(gpsLon,4);
Serial.print("East/West: ");
Serial.println(gpsEW);
Serial.print("Knots: ");
Serial.println(gpsKnots);
Serial.print("Track: ");
Serial.println(gpsTrack);
Serial.print("Date: ");
Serial.println(gpsDate);
Serial.println("------------------------------------------------");
char Day[3];
char Month[3];
char Year[3];
char Hours[3];
char Minutes[3];
char Seconds[3];
strncpy(Day,gpsDate,2);
Day[2]=0;
Serial.print("Day: ");
Serial.println(Day);
strncpy(Month,&gpsDate[2],2);
Month[2]=0;
Serial.print("Month: ");
Serial.println(Month);
strncpy(Year,&gpsDate[4],2);
Year[4]=0;
Serial.print("Year: ");
Serial.println(Year);
strncpy(Hours,gpsTime,2);
Hours[2]=0;
Serial.print("Hours: ");
Serial.println(Hours);
strncpy(Minutes,&gpsTime[2],2);
Minutes[2]=0;
Serial.print("Minutes: ");
Serial.println(Minutes);
strncpy(Seconds,&gpsTime[4],2);
Seconds[2]=0;
Serial.print("Seconds: ");
Serial.println(Seconds);
Serial.println("------------------------------------------------");
Serial.println();
}
Serial Monitor output:
§DISCOURSE_HOISTED_CODE_1§
All looking great - many thanks to all.
Now I have a major challenge:
I must print gpsLat / gpsLon / gpsNS / gpsEW as HEX as required by Sigfox.
I can only send 12 Bytes and need to send as follows
gpsLat + gpsNS as 5 bytes
gpsLon + gpsEW as 5 bytes
Any suggestions or pointers?