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()

  • My first idea was to convert the float to an integer. And then do divide by 100 on the side of the host. But send the value as string is also a good solution! If the temp has the value 18.53°C. This would be 1853 as integer. So I would need 3 Bytes. But I need reserve for higher temperature so that would be 4 Bytes. Is my calculation right? How many Bytes would I have to send for 18.53 as string?

Reply
  • My first idea was to convert the float to an integer. And then do divide by 100 on the side of the host. But send the value as string is also a good solution! If the temp has the value 18.53°C. This would be 1853 as integer. So I would need 3 Bytes. But I need reserve for higher temperature so that would be 4 Bytes. Is my calculation right? How many Bytes would I have to send for 18.53 as string?

Children
No Data
Related