Xbee - Transmit string data and convert it to a floating point number.

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.

void setup()
{
XBee.begin(9600);
Serial.begin(9600);
}

void loop()
{

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

}

}

How does this get the data into 'buf' ? DO = XBee.read ( ) ;

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);

}

The chibiGetData seems to put the data into the buf.

If I try to put "DO" directly into atof I get an error;

DO_float = atof(DO);

invalid conversion from char to const char*

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.

Update sending 4 bytes;

Send Code:

#include <stdio.h>
#include <SoftwareSerial.h>

float DO_float= 209.62;

SoftwareSerial XBee(2, 3); // RX, TX

void setup()
{
Serial.begin(9600); //enable the hardware serial port
XBee.begin(9600);
}

void loop(){

XBee.write((unsigned char*)&DO_float, 4);
Serial.print("DO:"); //print out "DO:"
Serial.print(" ");
Serial.println(DO_float,4);

delay(500); // delay half a second between transmission

}

Receive code:

//XBee.read returns a single character at a time - so we need to collect them all into an array.

#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.
int buf_index = 0;

void setup()
{
XBee.begin(9600);
Serial.begin(9600);
}

bool read_buf(char* buf, int read_size)
{
if (XBee.available()) {
buf[buf_index++] = XBee.read();
if (buf_index >= read_size)
{
buf_index = 0;
return true;
}
}
return false;
}

void loop()
{

if (read_buf((char*)&DO_float, 4))
{
Serial.println(DO_float);
}

}

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 ?