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

  • 1853 only requires 2 bytes, not 3. 2 bytes gives you everything from 0 - 65535. 18.53 as a string would be 5 bytes, one for each character (if you didn't send the final \0 which you don't really need).

    But all of that requires converting things on the nrf51822 chip to float, then 'printing' it to a string, all of which it can do but it's lots of library code, lots of floating point emulation and all on the low power device. Why don't you just send the raw value you read from the sensor, which is probably a 16 or 32 bit integer and do all the work on the receiving end? Your code will be smaller, will run quicker and use less power.

Reply
  • 1853 only requires 2 bytes, not 3. 2 bytes gives you everything from 0 - 65535. 18.53 as a string would be 5 bytes, one for each character (if you didn't send the final \0 which you don't really need).

    But all of that requires converting things on the nrf51822 chip to float, then 'printing' it to a string, all of which it can do but it's lots of library code, lots of floating point emulation and all on the low power device. Why don't you just send the raw value you read from the sensor, which is probably a 16 or 32 bit integer and do all the work on the receiving end? Your code will be smaller, will run quicker and use less power.

Children
No Data
Related