Search code examples
androidbluetootharduinorfduino

Send data over BLE with RFDuino


I've attached sensor to RFDuino and want to send readings over BLE to Android app.

const int sensorPin = 2;
int sensorValue = 0;

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

void loop() {
  sensorValue = analogRead(sensorPin);
  float voltage= sensorValue * (3.3 / 1023.0);
  Serial.print("uv sensor = ");
  Serial.println(voltage);
  RFduinoBLE.sendFloat(voltage);
  delay(1000);
}

In console I see values like 0.2. But in app it translates to something like 00-00-A4-41 It's described here but I don't get what's the logic behind it. How do I properly convert the value sent from RFDuino?


Solution

  • RFduino sends floats in little endian. Your android app will receive it as 4 bytes and you will need to convert it to big endian by reversing the order of the bytes (and converting to float). There's several threads on the topic, eg. Converting Little Endian to Big Endian and the one you used.

    It should just be a quick read up on endianness and floating point format to understand the logic behind it. But basically, the 4 byte float has the bytes stored in reverse order of each other in little vs. big endian. When you reverse the 4 bytes in little endian then it becomes big endian and you're good to go for your situation.

    http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/endian.html http://en.wikipedia.org/wiki/Single-precision_floating-point_format