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

Send a float

I want to send a float as a value of a characteristic.

As I know is this all what I have to change to send a float...

float temperature;

uint16_t len = 20;

attr_char_value.init_len  = 4;
attr_char_value.init_offs = 0;
attr_char_value.max_len   = 4;
attr_char_value.p_value   = (uint8_t*)&temperature;

hvx_params.offset = 0;
hvx_params.p_len = &len;
hvx_params.p_data = (uint8_t*)&temperature;

But it doesnt work. I wont recieve any data.

Parents Reply Children
  • Should I change the size to maximum? I dont understand what you mean with the host didn't enable notification for that characteristic after connection and before you send data by sd_ble_gatts_hvx()

  • Oh very interesting! I didnt know that!! So there are two ways to send information from nRF51 to host. One way is to recieve a READ from the host. In this case the host reads the value of the Characterstic. The value of the characteristic will only be changed if I set a new value with sd_ble_gatts_value_set().

    But there is another way if I want that the value of the characteristic is automatically updated. The nRF51 will send the value of the Characteristic all the time. But the host will only recieve the value of the characteristic when the nRF51 recieves a notification or indication from the host.

    Do I understand something wrong?

  • Correct. The only small differences can be depending on your code in nRF51. For example if notification is not enabled and nRF51 tries to send data you may see call of APP_ERROR_CHECK() in some examples which may reset the chip in debug mode.

  • If I choose the way with sd_ble_gatts_value_set(). How can I send a float with 2 dots after the comma? I mean do I have to split the value in Bytes or will the sd_ble_gatts_value_set function do that for me. It doesnt have to be exatcly 2 dots after the comma. It can be also the hole float. But 2 dots after the comma are minimum.

  • You need to choose the format to send depending on the max value. Either send float and don't do any conversion in nRF51 but you may need to do the conversion on host side. Or convert your float to integer. for example 13.273 will become 1327, then you just /100 on the host to display result. You can also choose to send string "13.273" if you want. In that case nRF51 will do sprintf and host will just display the string. In any case sd_ble_gatts_value_set will set new array of bytes in characteristic, it is up to you how to represent data and number of bytes you will use in the end.

Related