I am currently gathering info from a BLE device. I made a thread earlier where I successfully split a string consisting of 5 letters to their unique chars.
However from this BLE device i will also send some different information from time to time where I send 3 different values with a space after each one. So for example looking like this:
-0.1 0.51 0.07
So what I want to do now is to store this three numbers in different doubles and what I have done so far is this:
First i store the recieved message:
String recievedMessage;
recievedMessage += (char)BTLEserial.read();
Now I have to determine if the value sent is the 5 letters or if it is the numbers with a space between each value.
if (recievedMessage.length() > 5) //so if the recievedmessage is more than 5 words then it is the 3 values that is being sent.
{
char * charTheMessage = recievedMessage.c_str();
char *str;
str = strtok(charTheMessage, " ");
double one = str[0];
double two = str[1];
double three = str[2];
Serial.println (one);
Serial.println (two);
Serial.println (three);
}
if (recievedMessage.length() == 5)
{
//http://arduino.stackexchange.com/questions/36227/how-can-i-split-a-string-into-multiple-parts/36237?noredirect=1#comment72313_36237
//Here I split the letters into 5 different values if the value sent is 5.
}
But this values do not come out right when I print them in the serial monitor. I have also read that strtok is not reliable when data is sent fast so is strtok the right way to go you think?
1 Answer 1
You're almost there. Strtok returns the first token in the string only. You then have to convert that to a double yourself. You then call strtok again with NULL as the source string to get the next token. And so on.
I have written a good strtok example on here already. Look around, you may find it.
-
I am out and about and using my phone right now so can't do a complex answer.Majenko– Majenko2017年03月27日 09:53:17 +00:00Commented Mar 27, 2017 at 9:53
-
Here is one example: arduino.stackexchange.com/questions/31126/…Majenko– Majenko2017年03月27日 12:31:18 +00:00Commented Mar 27, 2017 at 12:31
-
Trying it right now! :) Will strncmp also work with spaces?Martman– Martman2017年03月27日 12:50:58 +00:00Commented Mar 27, 2017 at 12:50
-
strncmp sees space as just another characterMajenko– Majenko2017年03月27日 12:51:28 +00:00Commented Mar 27, 2017 at 12:51
-
ok, let me try to see if i can work it out!Martman– Martman2017年03月27日 12:54:18 +00:00Commented Mar 27, 2017 at 12:54