Skip to content

BLE Beacon Scanner example might not handle negative temperatures? #7618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task done
Humancell opened this issue Dec 23, 2022 · 7 comments · Fixed by #7791
Closed
1 task done

BLE Beacon Scanner example might not handle negative temperatures? #7618

Humancell opened this issue Dec 23, 2022 · 7 comments · Fixed by #7791
Assignees
Labels
Area: BT&Wifi BT & Wifi related issues Type: Example Issue is related to specific example.
Milestone

Comments

@Humancell
Copy link

Board

Olimex ESP32 PoE ISO

Device Description

PoE PCB using the ESP32 WROOM 32UE

Hardware Configuration

Nothing special

Version

latest master (checkout manually)

IDE Name

Arduino IDE

Operating System

All

Flash frequency

80Mhz

PSRAM enabled

no

Upload speed

115200

Description

https://github.com/espressif/arduino-esp32/blob/master/libraries/BLE/examples/BLE_Beacon_Scanner/BLE_Beacon_Scanner.ino#L118

When low temperatures occur, and go below 0 C, the calculations do not seem to be correct. It appears that using these 16-bit values in a 32-bit INT doesn't detect/use twos complement to handle the negative numbers properly.

In my final code I moved to a "signed short" which seems to fix the issue.

Sketch

signed short value;

          // extract the temperature
          value = (cServiceData[TEMP_MSB] << 8) + cServiceData[TEMP_LSB];

Debug Message

No debug ... just wrong calculated values.

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@Humancell Humancell added the Status: Awaiting triage Issue is waiting for triage label Dec 23, 2022
@SuGlider SuGlider self-assigned this Dec 28, 2022
@mrengineer7777
Copy link
Collaborator

Your sketch doesn't match the example. Note the (int) cast on each payload byte. Also we don't have your hardware. It would be very helpful to know the actual bytes received in ServiceData[] as well as the actual temperature.

@SuGlider
Copy link
Collaborator

SuGlider commented Dec 29, 2022

This seems to be just a casting matter.
uint32_t with just 16 bits in the LSB will be always positive.

Casting it to whatever struct or type is an application subject, not related to the BLE Beacon Scanner example.

@SuGlider
Copy link
Collaborator

I'll add this fix. Thanks.

@SuGlider SuGlider added Type: Example Issue is related to specific example. Area: BT&Wifi BT & Wifi related issues and removed Status: Awaiting triage Issue is waiting for triage labels Dec 29, 2022
@VojtechBartoska VojtechBartoska added this to the 2.0.7 milestone Jan 2, 2023
@SuGlider
Copy link
Collaborator

SuGlider commented Feb 2, 2023

@PilnyTomas

The temperature is represented in signed 8.8 fixed-point notation and measured in Celsius.
https://github.com/google/eddystone/blob/master/eddystone-tlm/tlm-plain.md 

When Temperature is not supported BLE will return 0x8000, which means -128 °C.
In that case, it could be nice to check this value and send out the correct message, instead of reporting the temperature.

Note: All multi-byte values are big-endian --> MSB:LSB

@SuGlider
Copy link
Collaborator

SuGlider commented Feb 3, 2023

As we talked, the change shall be in
https://github.com/espressif/arduino-esp32/blob/master/libraries/BLE/examples/BLE_Beacon_Scanner/BLE_Beacon_Scanner.ino#L118-L120

I think that it just has to use signed short, like

          // BIG ENDIAN payload in signed 8.8 fixed-ppint notation. Unit is Celsius. 
          int16_t temp_payload = payLoad[16] + (payLoad[15] << 8);
          if (payLoad[15] == 0x80 && payLoad[16] == 0) {
            Serial.printf("This device does not support measuring temperature.\n");
          } else {
            float calcTemp = temp_payload / 256.0f;
            Serial.printf("Reported temperature from data: %.2fC\n", calcTemp);
          }

But it shall be verified by testing some cases, like below:

For instance:

Signed Short Value -32768 = 0x8000 shall be calculated as -128 °C
Signed Short Value -9024 = 0xDCC0 shall be calculated as -35.25 °C
Signed Short Value 9024 = 0x2340 shall be calculated as +35.25 °C
Signed Short Value 384 = 0x0180 shall be calculated as +1.50 °C
Signed Short Value -1024 = 0xFC00 shall be calculated as -4.00 °C

@SuGlider
Copy link
Collaborator

SuGlider commented Feb 3, 2023

@PilnyTomas - The exactly same issue also happens with the code in
https://github.com/espressif/arduino-esp32/blob/master/libraries/BLE/src/BLEEddystoneTLM.cpp

This also needs to be fixed.

@PilnyTomas
Copy link
Contributor

@Humancell could you please try the change in #7791 ?
I don't have the devices to test this.

@VojtechBartoska VojtechBartoska moved this from Todo to In Review in Arduino ESP32 Core Project Roadmap Feb 6, 2023
@VojtechBartoska VojtechBartoska added Status: Review needed Issue or PR is awaiting review and removed Status: Review needed Issue or PR is awaiting review labels Feb 15, 2023
me-no-dev pushed a commit that referenced this issue Feb 20, 2023
* Changed data type of temperature

* Changed data type in EddystoneTLM class and example

* Revert "Changed data type in EddystoneTLM class and example"

This reverts commit 1f3a941.

* Draft of Eddystone TLM example

* Adds MACROs to convert beacon temperature 

2 Macros
EDDYSTONE_TEMP_U16_TO_FLOAT(tempU16)  - takes the TLM BigEndian 8.8 fixed point representation and returns its float value 
EDDYSTONE_TEMP_FLOAT_TO_U16(tempFloat)  - takes a float (temperature) and returns its BigEndian 8.8 fixed point representation

* Fixed temp

* Changed to conform with PR comments

* Fixed comment on closing bracket

* Prints negative TEMP big endian as just 2 bytes

* Extacts correct Eddyston Service Data

* Fixes BLEEddystoneTLM::toString() negative temp

* Fixes URL field length

* Fixes Eddystone URL decoding

* Fixes MSB for iBeacon UUID

iBeacons use big endian BLE fields.

* Fix to detect iBeacon that also has Service UUID

This fix makes the BLE_iBeacon.ino to work correctly with the BLE_Beacon_Scanner.ino example

---------

Co-authored-by: Rodrigo Garcia <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: BT&Wifi BT & Wifi related issues Type: Example Issue is related to specific example.
Projects
Development

Successfully merging a pull request may close this issue.

5 participants