简体   繁体   English

使用RFDuino通过BLE发送数据

[英]Send data over BLE with RFDuino

I've attached sensor to RFDuino and want to send readings over BLE to Android app. 我已将传感器连接到RFDuino,并希望通过BLE将读数发送到Android应用。

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 . 在控制台中,我看到类似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. 但是在应用程序中,它翻译为00-00-A4-41之类00-00-A4-41虽然在此进行了描述但我不明白其背后的逻辑是什么。 How do I properly convert the value sent from RFDuino? 如何正确转换从RFDuino发送的值?

RFduino sends floats in little endian. RFduino发送小尾数形式的浮点数。 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). 您的android应用将以4个字节的形式接收它,您需要通过反转字节顺序(并将其转换为float)将其转换为big endian。 There's several threads on the topic, eg. 例如,该主题有多个主题。 Converting Little Endian to Big Endian and the one you used. 将Little Endian转换为Big Endian以及您使用的那个。

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. 但基本上,4字节浮点型的字节以小尾数与大尾数相反的顺序存储。 When you reverse the 4 bytes in little endian then it becomes big endian and you're good to go for your situation. 当您以Little Endian反转4个字节时,它将变成Big Endian,您可以根据情况进行选择。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM