BLE api 将负温度转换为正温度
BLE api converts negative temperature to positive
我想使用 ESP32 芯片从 BLE 信标中提取温度。我使用以下代码来执行此操作:
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
std::string strServiceData = advertisedDevice.getServiceData();
uint8_t cServiceData[100];
strServiceData.copy((char *)cServiceData, strServiceData.length(), 0);
if (advertisedDevice.getServiceDataUUID().equals(BLEUUID(beconUUID))==true) // found Eddystone UUID
{
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());advertisedDevice.getServiceDataUUID().bitSize(), advertisedDevice.getServiceDataUUID().toString().c_str(),strServiceData.length());
if (cServiceData[0]==0x20)
{
BLEEddystoneTLM oBeacon = BLEEddystoneTLM();
oBeacon.setData(strServiceData);
Serial.printf("Eddystone Frame Type (Unencrypted Eddystone-TLM) \n");
Serial.printf(oBeacon.toString().c_str());
}
else
{
}
}
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
}
void loop() {
BLEScanResults foundDevices = pBLEScan->start(scanTime);
delay(5000);
}
正值的温度输出很好,但是当温度变为负值时,我仍然得到一个异常的正数。
串行监视器输出:
Advertised Device: Name: , Address: ac:23:3f:85:2e:f3, serviceUUID: 0000feaa-0000-1000-8000-00805f9b34fb
Eddystone Frame Type (Unencrypted Eddystone-TLM)
Version
art_cpu0_default mV
Temperature 241.0 °C
Adv. Count 3782
Time 0000.00:30:34
从上面的调试消息中,这个特定的信标应该读取 -18 C 而不是 235.0 C。我知道这是因为 BLE 标签附带的 android 应用程序显示温度为 -18 C然后我将标签放入设置为 -18 C 的冰箱中。
任何人都可以帮助我,为什么我得到的浮点值应该是负的?
可以找到我正在使用的库here
根据用户的建议,我注意到我的问题不在于我的代码,而在于库。代码太长 post 但是上面提供了 link。问题似乎与 m_eddystoneData.temp 的声明和将 temp 转换为无符号值有关。因此输出将始终为正。
void BLEEddystoneTLM::setTemp(float temp) {
m_eddystoneData.temp = (uint16_t)temp;
} // setTemp
我查看了您链接到的库,其中包含以下温度信息:
void BLEEddystoneTLM::setTemp(float temp) {
m_eddystoneData.temp = (uint16_t)temp;
} // setTemp
和
BLEEddystoneTLM::BLEEddystoneTLM() {
beaconUUID = 0xFEAA;
m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE;
m_eddystoneData.version = 0;
m_eddystoneData.volt = 3300; // 3300mV = 3.3V
m_eddystoneData.temp = (uint16_t) ((float) 23.00);
m_eddystoneData.advCount = 0;
m_eddystoneData.tmil = 0;
} // BLEEddystoneTLM
两者似乎都将温度定义为 16 位的无符号短整数。我认为他们应该签名,因此 int16_t
我想使用 ESP32 芯片从 BLE 信标中提取温度。我使用以下代码来执行此操作:
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
std::string strServiceData = advertisedDevice.getServiceData();
uint8_t cServiceData[100];
strServiceData.copy((char *)cServiceData, strServiceData.length(), 0);
if (advertisedDevice.getServiceDataUUID().equals(BLEUUID(beconUUID))==true) // found Eddystone UUID
{
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());advertisedDevice.getServiceDataUUID().bitSize(), advertisedDevice.getServiceDataUUID().toString().c_str(),strServiceData.length());
if (cServiceData[0]==0x20)
{
BLEEddystoneTLM oBeacon = BLEEddystoneTLM();
oBeacon.setData(strServiceData);
Serial.printf("Eddystone Frame Type (Unencrypted Eddystone-TLM) \n");
Serial.printf(oBeacon.toString().c_str());
}
else
{
}
}
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
}
void loop() {
BLEScanResults foundDevices = pBLEScan->start(scanTime);
delay(5000);
}
正值的温度输出很好,但是当温度变为负值时,我仍然得到一个异常的正数。
串行监视器输出:
Advertised Device: Name: , Address: ac:23:3f:85:2e:f3, serviceUUID: 0000feaa-0000-1000-8000-00805f9b34fb
Eddystone Frame Type (Unencrypted Eddystone-TLM)
Version
art_cpu0_default mV
Temperature 241.0 °C
Adv. Count 3782
Time 0000.00:30:34
从上面的调试消息中,这个特定的信标应该读取 -18 C 而不是 235.0 C。我知道这是因为 BLE 标签附带的 android 应用程序显示温度为 -18 C然后我将标签放入设置为 -18 C 的冰箱中。
任何人都可以帮助我,为什么我得到的浮点值应该是负的?
可以找到我正在使用的库here
根据用户的建议,我注意到我的问题不在于我的代码,而在于库。代码太长 post 但是上面提供了 link。问题似乎与 m_eddystoneData.temp 的声明和将 temp 转换为无符号值有关。因此输出将始终为正。
void BLEEddystoneTLM::setTemp(float temp) {
m_eddystoneData.temp = (uint16_t)temp;
} // setTemp
我查看了您链接到的库,其中包含以下温度信息:
void BLEEddystoneTLM::setTemp(float temp) {
m_eddystoneData.temp = (uint16_t)temp;
} // setTemp
和
BLEEddystoneTLM::BLEEddystoneTLM() {
beaconUUID = 0xFEAA;
m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE;
m_eddystoneData.version = 0;
m_eddystoneData.volt = 3300; // 3300mV = 3.3V
m_eddystoneData.temp = (uint16_t) ((float) 23.00);
m_eddystoneData.advCount = 0;
m_eddystoneData.tmil = 0;
} // BLEEddystoneTLM
两者似乎都将温度定义为 16 位的无符号短整数。我认为他们应该签名,因此 int16_t