This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Best way to send float over BLE

What is the Best way to send float over BLE?

Values range between -2.000volt and +2.000Volt (-2000mV / +2000mV) Do I break it into two uint8_t and manipulate the +/- sign in separate 8-bit byte?

I took the ble_sdk_app_proximity example, and commented out what I do not need. Then used the examples from the tutorials, to add three services. So I added x3 services, based on the BLE Characteristics, a beginner's tutorial, and it is working fine. (nrf5-ble-tutorial-characteristic-CompletedCode ported for SDK 13.zip)

So I need to send the Voltage either as a float, or manipulate it to look like uint8_t, and keep track of the sign.

Any help appreciated.

  • Yvan, 2 questions:

    1. The IAR compiler does not understand the union definition. Is there something missing. It is not clear how this gets implemented.
    2. It looks like the code simply breaks the 32-bits up into 4x bytes of 8-bits. It is not IEEE-754 32-bit Single-Precision Floating-Point Number conversion?

    Any help appreciated

  • I'm quite surprised that IAR doesn't know what a union is. Here is a link on IAR that uses union endianness

    This is a IEEE 32b float. You just need to make sure the float get correctly re-assembled on the other side.

    float gsr = bytesToFloat(characteristic.getValue()[1],
                                     characteristic.getValue()[2],
                                     characteristic.getValue()[3],
                                     characteristic.getValue()[4]);
    
    private float bytesToFloat(byte b0, byte b1, byte b2, byte b3) {
        int asInt = (b0 & 0xFF)
                |  ((b1 & 0xFF) << 8)
                |  ((b2 & 0xFF) << 16)
                |  ((b3 & 0xFF) << 24);
        return  Float.intBitsToFloat(asInt);
    }
    
  • Normally there are no floats in digital (sic?) system

    This is clearly nonsense: many digital systems support - and use - floating point!

    en.wikipedia.org/.../IEEE_floating_point

    The ARM Cortex-M4F, on which the Nordic nRF52 is based, has a hardware floating-point unit built into the silicon.

    Perhaps you meant to say "embedded system" - rather than "digital system" ?

    Even then it is not true - especially in larger embedded systems.

    But it is quite often true of small embedded systems, especially when using simple 8-bit microcontrollers; possibly also when using a Cortex-Mo - such as the nRF51

  • Values range between -2.000volt and +2.000Volt (-2000mV / +2000mV)

    You have shown there yourself how to avoid the need for floating point at all: just express it in millivolts!

  • @awneil I was not precise. What I meant is that "float" is just interpretation during arithmetic. Unless you do such manipulations (and there is normally no reason to do it) there is no difference between float and int, just bunch of bytes. Also what I meant by "no float in digital system" is that normally there is no natural way of "creating" floats in digital system, there is simple A2D conversion which results in some sample with given bit-length, interpreting it as float doesn't (normally) make sense unless you want to display it to humans or unless some very specific algorithm demands that instead of working on "raw" data (which are obvious integer). But I'm keen to learn if my experience is false and there are some legit and "natural" uses of floats during signal or other processing.

Related