I'm transmitting: Temperature: 28.22C
and on the receiving end I'm receiving the whole tranmission as a string. I'm trying to spilt Temperature to leave it as a string and 28.22 as a float to compare, how do I do that ?
TX end hardware: (Arduino Uno board with Xbee S1) to computer RX end hardware: (Arduino Uno board with Xbee S2) to computer
-
1Is the "receiving end" an Arduino or a computer? If it's a computer, you should tell which language you use and probably ask your question on stackoverflow.jfpoilpret– jfpoilpret2015年06月10日 04:28:31 +00:00Commented Jun 10, 2015 at 4:28
-
Hi, the receiving end is an Arduino Uno board which is hooked up to the computer. The transmitting end is also hooked up to an Arduino Uno board. Both communication goes through Xbee S1 carduser10566– user105662015年06月10日 08:02:46 +00:00Commented Jun 10, 2015 at 8:02
2 Answers 2
Okk very simple approach :-
Suppose this is your string variable inStr = "Temperature: 28.22C"
String inStr = "Temperature: 28.22C";
int indexOfSpace = inStr.indexOf(' ');
//this stores the address of the space character in the string.
int indexOfC = inStr.indexOf('C');
String Temp = inStr.substring(indexOfScace+1,indexOfC);
//as the parameter for the function is supposed to be the starting point of the string to
be extracted and one more than its ending point.
//Now to convert to float
float TempFlo = Temp.toFloat();
-
@user10566 if this answer worked please consider marking it as accepted so that the SE system does not bump it up as an unanswered question.RSM– RSM2016年06月04日 19:47:27 +00:00Commented Jun 4, 2016 at 19:47
I suppose your communication is working... In order to retrieve a floating number from a string you might use toFloat() function. You can watch the reference page here to understand how does it work.