I am using a WiFi shield plus Mega to try to receive what was once floating point data from a java application running on a separate machine. The application is pretty simple, the mega/shield are the server receiving a string of char's representing the floating point value from the client (I know it's backwards, but there is still data going back the otherway, but that's not really pertinent to the question, though). After playing with as many member functions of the DataOuputStream class in java to find an easy way to send the floating point data, I resorted to writeBytes(String) after converting the float to a string.
The client.read() funciton does a great job of reading this data in byte at a time and lets me create a String (big "S"). Because this byte method of sending the data is not very friendly, I have to delimit the end of each numerical value with a special character: "!". So data comes in looking like: 51!123.21!... and I can successfully create a String from each one (eg. 51, 123.21,..). My problem/question: How do I cast this back into a float?
I'm hoping that someone will tell me how to work with Strings since they are the most friendly, but if I have to completely rethink this, then so be it. Any advice/help would be appreciated.
atof() is the function you want. It takes a char array though, so you will have to convert your String into a string first, then use atof() to convert it to a float:
Ah, yes! I think this was the boost I needed to get over the top. I recall good old atof() from my C++ days,... so it was just what I needed. However, I think you meant to say:
char temp[33];
myData.toCharArray(temp, myData.length());
float v = atof(temp);
as toCharArray doesn't return the string, nonetheless it was exactly what I needed. Thanks.
Ah, yes! I think this was the boost I needed to get over the top. I recall good old atof() from my C++ days,... so it was just what I needed. However, I think you meant to say:
char temp[33];
myData.toCharArray(temp, myData.length());
float v = atof(temp);
as toCharArray doesn't return the string, nonetheless it was exactly what I needed. Thanks.
I might have done, yes. I'm used to using myString.c_str(), not the Arduino strangeness of .toCharArray(). Mind, I never ever use Strings on the Arduino...