Hey guys,
For my project I need to get GPS data from one arduino to another. Lets say that arduino that's sending its GPS is called Tx while the one that's receiving it is Rx.
So the GPS data on Tx comes as a string and it's in this format.
e.g. 43⸮12'89.09"N
Now the Arduino Rx reads it via XBee S1 Pro and displays it as a string on the serial monitor.
Once this is done, i need to use the GPS data in maths to calculate the distance between them.
So what I've decided is that I, Arduino Tx, I get the string and parse it into two ints (degrees and minutes) and one float (seconds). This way the Arduino Rx will get GPS data split into degrees, minutes and seconds, parse its own GPS data and then compare the two.
The problem is that for some reason I can get the degrees and minutes but not the seconds, or I can get the degrees and seconds, but not the minutes. I don't understand why this is but I've tried changing the delimiters and I still am not able to get all three variables.
This is the code that I'm using:
// Example 5 - Receive with start- and end-markers combined with parsing
const byte numChars = 35;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
int integer1FromPC= 0;
int integer2FromPC = 0;
float floatFromPC = 0.0;
boolean newData = false;
//============
void setup()
{
Serial.begin(9600);
Serial.println("This demo expects 3 pieces of data - text, an integer and a floating point value");
//Serial.println("Enter data in this style <HelloWorld, 12, 24.7> ");
Serial.println();
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '\n';
char endMarker = 'N';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(receivedChars,"⸮"); // get the first part - the string
integer1FromPC = atoi(strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, "⸮"); // this continues where the previous call left off
integer2FromPC = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, "N");
floatFromPC = atof(strtokIndx); // convert this part to a float
}
//============
void showParsedData() {
Serial.print("Degree ");
Serial.println(integer1FromPC);
Serial.print("Minutes ");
Serial.println(integer2FromPC);
Serial.print("Seconds ");
Serial.println(floatFromPC);
}
And this is the result on the serial monitor when 43⸮12'89.09"N was entered:
Degree 43
Minutes 12
Seconds 0.00
Can anyone tell me what I'm doing wrong and what I can do to fix this?
Let me know if you guys need more information, and apologies if I missed something..
I'm using an Arduino Mega
Digilent PMODGPS
Xbee S1 Pro 802.15.4
THANKS A LOT!
High Regards