Search code examples
c++arduinowirelessxbee

Transmitting Float Values Across Xbee Network


I am having trouble transmitting a float across a simple 2 node Xbee Network.

I am aware that the Xbee system transmits packages via bytes, so I can send a char, but I am having trouble sending anything more than that, and I can't seem to find any documentation anywhere.

Here is my current (basic) code.

Sender:

(... appropriate setup...)
void loop()
{
  sensorValue = analogRead(analogInPin);
  sensorValueTemp = sensorValue / 9.31; //LM35 measurement into Centigrade
  Serial.print(sensorValueTemp);
  delay(1000);
}

Receiver:

(...appropriate setup...)
void loop() { 
  lcd.setCursor(0, 0);
    if (Serial.available() > 0) {
    lcd.print(incomingByte);
  }
  delay(1000);
}

Any hint as to get the float to be transmitted successfully would be great? Or if it is already being transmitted properly, how to read it properly?

Thanks


Solution

  • You can send the float in bytes and reconstruct the float at the receiver.

    The following example may help you:

    Sender side:

    float x = 1128.476;
    
    char b[sizeof(x)];
    memcpy(b, &x, sizeof(x));
    
    // Iterate over b and send bytes
    // [...]
    

    Receiver side:

    float y = 0;
    
    char b[sizeof(x)];
    
    // Store 4 bytes representing the float into b
    // [...]
    
    // Rebuild the float
    memcpy(&y, b, sizeof(y));
    

    At the end, you have float y on the receiver side, which has the same representation of float x on the sender side.