Send a float value via Xbee - AT

Hello.

I have 2 arduino unos and 2 xbees. I configured them in AT mode. I want to send a float value (this float value comes from a sensor) from one arduino to the other. How can i do that? I managed to send ints and chars, and it worked great. But i can`t manage to send a float value via xbee...

Any help? Ty!

Chiser Alex

this float value comes from a sensor

No, it doesn't. It may be computed from a value read from a sensor.

How can i do that?

How are you communicating with the XBee? If you are using Serial, or a SoftwareSerial instance, Serial.print() handles floats just fine.

Yes, the values are computed from the sensor. We're sending a value of the energy of a signal measured over 1s. But this is less important. We are using SoftwareSerial and I don't know what you mean when you say that it handles the values just fine, because xbee.read() returns a char. That means, a byte of what it receives. Our questions regards reassembling the bytes read into a variable.

For example, we have tried the following (at the receiver):

char c[4]; // float is stored on 4B
float value = 0;
int i = 0;

void loop ()
{
    if (xbee.available()) {
        c[i++] = xbee.read();
    }

    if (i == 4) {
        value = (c[0] & 0x00FF) | ((c[1] & 0xFF00) << 8) | ((c[2] & 0xFF0000) << 16) | ((c[3] & 0xFF000000) << 24);
        Serial.println(value, BIN);
        i = 0;
    }
}

However, we're getting nothing right.

We're getting a bit frustrated because we do not understand how the data is buffered when the XBee is receiving it and how to reassemble it into what we want.

What does the X-bee returns after xbee.read()?

I don't think:

(c[0] & 0x00FF) | ((c[1] & 0xFF00) << 8) | ((c[2] & 0xFF0000) << 16) | ((c[3] & 0xFF000000) << 24)

is correct...
If it really returns 'char' than it's only 1 byte, so (C[3] & 0xFF0000) doesn't compute... try:

((c[0]) | (c[1]) << 8) | (c[2]<< 16) | (c[3]<< 24))

You could change a 'char' to normal value with (C[]- '0'). It outputs the int value of the character...

Also: A float is computed out of 4 bytes, but with a very complex formula. To create a float out of 4 normal bytes of data the quickest way (BUT ONLY IF YOU KNOW THE 4 BYTES ARE A NON-COMPUTED FLOAT) :

memcpy(&value, &c, 4);

And then, on the drive home, I thought "This won't work right?":

c[i++]

You'll start at c[1] the first time you call the function...
But i'm not 100% sure...

Can you give a serial feed of c[0] t0 c[3]?

You might find this thread on sending and receiving floats with XBees to be useful.

http://forum.arduino.cc/index.php?topic=211723.0

It covers two methods of transmitting and reconstructing the data. 1)sending and receiving the 4 bytes of the float using a union, and 2)sending and receiving a delimited character array and using atof.

C-F-K:
And then, on the drive home, I thought "This won't work right?":

c[i++]

You'll start at c[1] the first time you call the function...

In that expression, i is incremented after the expression is evaluated, so it's starting from c[0] and then i = 1.

Also: A float is computed out of 4 bytes, but with a very complex formula. To create a float out of 4 normal bytes of data the quickest way (BUT ONLY IF YOU KNOW THE 4 BYTES ARE A NON-COMPUTED FLOAT) :

I understand, I thought that when sending something, it actually sends that data byte by byte, so at the receiving point you can just reassemble the bits, and the float type will interpret them accordingly. But maybe my assumption is wrong. This is why I said I don't understand very well how the communication works.

Unfortunately, I can't post right now the output, as I'm working with my colleague and I don't have the two Arduinos to check it.

You might find this thread on sending and receiving floats with XBees to be useful.

Problem for convert data received from XBee - Networking, Protocols, and Devices - Arduino Forum

Thank you, I'll check it out again, I've seen it before but It didn't seem so helpful at that time, but I'll read it again more carefully.