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
  • First of all you are adding characteristic with exact size 4. It means that you have to set len = 4 while sending data. But your original problem most likely in the fact that the host didn't enable notification for that characteristic after connection and before you send data by sd_ble_gatts_hvx()

  • 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.

Reply
  • 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.

Children
No Data
Related