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

How do I send float data via nus_data_send() function

Hi, 

I know that the NUS service ble_data_send() can send string and integer data via BLE to an app, but can it be used to transmit 24 bit float data
If not, what other way would you suggest that I display my data up to 4 decimal places in the NRF Toolbox logging app? 

This is what I'm doing right now: 

m_rx_buf [3] ;  

// m_rx_buf received 3 BYTE HEX data via SPI communication from an ADC
// m_rx_buf shows data in format "0A207E" , follows ADC output data format 
// The fullscale value of ADC is 7FFFFF. Following is processing of ADC digital data to analog value
 float sensordata = 0; 
    uint24_t fullscale = 0x7FFFFF; 
      sensordata = float((m_rx_buf/fullscale)*gain);
      
      
      // How do I change ble_nus_data_send() so that I can replace m_rx_buf with float type sensordata?
      
      
      if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
      {

          err_code = ble_nus_data_send(&m_nus, m_rx_buf, &ble_buff_len, m_conn_handle);
          if ((err_code != NRF_ERROR_INVALID_STATE) &&
              (err_code != NRF_ERROR_RESOURCES) &&
              (err_code != NRF_ERROR_NOT_FOUND))
          {
              APP_ERROR_CHECK(err_code);
          }

Thank you. 


Parents
  • You can only send byte-arrays over BLE, so will have to typecast the float to a uint8_t array and then assemble it on the central side. 

    I have usually done this when I want to see the data sent to the nRF Toolbox app. 

     double temp = read_temperature();
    
    // Place the temperature measurement into the data array
    uint32_t err_code;
    uint8_t data[20];
    sprintf((char *)data, "Temperature: %f", temp);
    
    //Send temperature measurement to nRF Toolbox app
    err_code = ble_nus_string_send(&m_nus, data, sizeof(data));
    APP_ERROR_CHECK(err_code);

    It should be displayed like this 

Reply
  • You can only send byte-arrays over BLE, so will have to typecast the float to a uint8_t array and then assemble it on the central side. 

    I have usually done this when I want to see the data sent to the nRF Toolbox app. 

     double temp = read_temperature();
    
    // Place the temperature measurement into the data array
    uint32_t err_code;
    uint8_t data[20];
    sprintf((char *)data, "Temperature: %f", temp);
    
    //Send temperature measurement to nRF Toolbox app
    err_code = ble_nus_string_send(&m_nus, data, sizeof(data));
    APP_ERROR_CHECK(err_code);

    It should be displayed like this 

Children
Related