Go Down

Topic: Send a float value via Xbee - AT (Read 2112 times) previous topic - next topic

Xeyow

May 06, 2015, 07:26 pm Last Edit: May 06, 2015, 07:40 pm by Xeyow
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

PaulS

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

Quote
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.
The art of getting good answers lies in asking good questions.

laoonatic

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):
Code: [Select]

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.

C-F-K

#3
May 07, 2015, 04:45 pm Last Edit: May 07, 2015, 04:50 pm by C-F-K
What does the X-bee returns after xbee.read()?


I don't think:

Code: [Select]
(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:

Code: [Select]
((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) :

Code: [Select]
memcpy(&value, &c, 4);

Evil Base of Operations: Holland

C-F-K

And then, on the drive home, I thought "This won't work right?":
Code: [Select]
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]?
Evil Base of Operations: Holland

cattledog

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.

laoonatic

And then, on the drive home, I thought "This won't work right?":
Code: [Select]
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.

Quote
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.

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

http://forum.arduino.cc/index.php?topic=211723.0
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.

Go Up