I would like to transmit a string from one Arduino/Xbee to another and convert the string to a floating point number at the second Arduino for processing using AT. The Arduino will send the information as a string and display it to the monitor. If I try to convert the string to a floating point number on the receiving Arduino the atof command does not work. I have tried various different formats for the string data but the output is the same.
I've attached photos of the serial monitor output.
Here is the sending code;
#include <stdio.h> #include <SoftwareSerial.h>
float DO_float= 9.62;
SoftwareSerial XBee(2, 3); // RX, TX
void setup()
{
Serial.begin(9600); //enable the hardware serial port
XBee.begin(9600);
}
void loop(){
XBee.println(DO_float);
Serial.print("DO:"); //print out "DO:"
Serial.print(" ");
Serial.println(DO_float,4);
delay(500); // delay half a second between transmission
}
Receiving code;
#include <SoftwareSerial.h> #include <stdio.h>
SoftwareSerial XBee(2, 3); // RX, TX
float DO_float=0; //used to hold a floating point number that is the D.O.
if (XBee.available()) { // If data comes in from XBee, send it out to serial monitor
char DO, buf[100];
DO = XBee.read();
DO_float = atof((char *)buf);
Serial.print(DO); //Serial.println(DO_float); //uncomment to print floating point result
I'm not sure, I modified the example code I was using with a Freakduino to accomplish the same task.
if (chibiDataRcvd() == true)
{
byte DO, buf[100];
//DO_float=atof(DO); //Uncomment this line to turn the string into to floating pint value.
DO = chibiGetData(buf);
if (DO == 0) return; // if no len, its a dupe packet. discard.
DO_float= atof((char *)buf);
DO seems to be used for the number of returned bytes. It is not the value of the floating point number. Can you find a better example ? or explanation about the XBee library. Which XBee library do you use ?
I would transmit it as a binairy value. A 'float' is 4 bytes, so I would transmit 4 bytes.
Yes, that is the way to do it. Perhaps a timeout can be added.
Is the function XBee.readBytes() available ?
Can you give a link to the XBee library that you use ?