Home/Support/Support Forum/Xbee - Transmit string data and convert it to a floating point number.

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

0 votes
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 a link to the serial monitor output. https://plus.google.com/u/0/photos/100758297290516264231/albums/6156285809144715009

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


}


}
asked Jun 4, 2015 in RF Solutions and XBee by dspfun New to the Community (0 points)

Please log in or register to answer this question.

...