Nano 33 BLE, Bluetooth values

Hello everyone,

I am trying to understand how to use correctly the BLE on the NANO 33 BLE board.
I run this code to understand the basics and see my values :


#include <ArduinoBLE.h>


#define BLE_Temperature_Service_UUID     "859862e5-7ebf-4992-ae0c-4995d8eb271f"

#define BLE_Float_Val_UUID               "39605f73-4d22-4660-bc38-6840efef94d5"
#define BLE_Temperature_UUID             "5b3900d8-a713-4eae-b472-0340c4dcdc72"
#define BLE_IntVal_UUID                  "5b3902d5-a253-4eae-b472-0340c4dcdc72"

BLEService temperatureService( BLE_Temperature_Service_UUID );  // User defined service

BLEFloatCharacteristic temperatureCharacteristic( BLE_Temperature_UUID ,BLERead ); // remote clients will only be able to read this
BLEFloatCharacteristic floatValueCharacteristic( BLE_Float_Val_UUID  ,BLERead ); // remote clients will only be able to read this
BLEIntCharacteristic intValueCharacteristic(BLE_IntVal_UUID ,BLERead );

float floatValue = 0.0;
float stored_temp = 32.9874;
int intVAL = 32;

void setup() {

  Serial.begin(9600);    // initialize serial communication
  while (!Serial);

  if (!BLE.begin()) {   // initialize BLE
    Serial.println("starting BLE failed!");
    while (1);
  }

  BLE.setLocalName("Nano33BLE");  // Set name for connection
  

   BLE.setAdvertisedService(temperatureService); // Advertise service
  
  //BLE and characteristics
  temperatureService.addCharacteristic(temperatureCharacteristic); // Add characteristic to service
  temperatureService.addCharacteristic(floatValueCharacteristic);
  temperatureService.addCharacteristic(intValueCharacteristic);
  
  //add Service
  BLE.addService(temperatureService); 
  
  //set the initial value for charasteristics
  temperatureCharacteristic.writeValue( stored_temp);
  floatValueCharacteristic.writeValue( floatValue);
  intValueCharacteristic.writeValue( intVAL);

  BLE.advertise();  // Start advertising
  Serial.print("Peripheral device MAC: ");
  Serial.println(BLE.address());
  Serial.println("Waiting for connections...");
}

void loop() {
  BLEDevice central = BLE.central();  // Wait for a BLE central to connect

  // if a central is connected to the peripheral:
  if (central) {
    Serial.print("Connected to central MAC: ");
    // print the central's BT address:
    Serial.println(central.address());
  

    while (central.connected()){} // keep looping while connected
    

    Serial.print("Disconnected from central MAC: ");
    Serial.println(central.address());
  }
}

I connect to my phone with the nRF Connect app and this is what I get:


The value (0x)19-F3-03-42 should represent the stored_temp = 32.9874;
According to the documentation I found:

So I thought my stored_temp would be either on IEEE-754 or IEEE-11073 but neither of those returns to me 32.9874.
Am I missing something?

Yes, the order of the bytes. The Bluetooth Core Specification is little endian transmission.

My iee -754 floating point converter shows 0x4203f319 for 32.9874

1 Like

I would recommend you avoid floating types for characteristics. As you can see there are multiple floating-point standards. Usually, a compiler or board only supports one standard. If it is the wrong one, you need to do conversion. Floating point is good for computation with large dynamic range but not for interfaces. It forces the use and inclusion of floating-point support even if fixed-point would be a better choice.

Have a look at how the BLE specification provides some nice suggestion on how to do this. Here is the XML file for the temperature characteristic.

https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Characteristics/org.bluetooth.characteristic.temperature.xml

It is a 16-bit signed integer with a resolution of 0.01 degrees Celsius. So, 2150 represents 21.5 °C.

Also, when you look at the presentation format descriptor the BLE specification has included a Exponent field to allow floating point values in fixed point data types. So, the engineers who wrote the specification knew what they were doing.

https://www.bluetooth.com/wp-content/uploads/Sitecore-Media-Library/Gatt/Xml/Descriptors/org.bluetooth.descriptor.gatt.characteristic_presentation_format.xml

2 Likes