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

Sending number over ble with ble_nus_string_send

Hi! Can anyone help me send values of temperature over ble? I tried and it appears like this,Capturar.PNG.

I'm using the code on the examples, ble_peripheral\ble_app_uart and ble_central\ble_app_uart, to comunicate over ble and changed this function on ble_peripheral to send values of temperature when i request in central:

static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length){		
  if(p_data[0]=='?'){	

	uint8_t temp;
	uint32_t err_code;

    err_code = sd_temp_get(&temp);
	APP_ERROR_CHECK(err_code);
														
    temp = (temp / 4);
	printf("Temperature: %d \n", temp);
																											
	ble_nus_string_send(&m_nus, &temp, 2);
	}													
}									

The temperature is correct when i print on the side of the peripheral but when i send it over ble to central it appears in hexadecimal i think, as you can see on the image above. I'm using two nrf52, pca10040, s132 and i have SDK 12.2.0.

Thanks.

  • This kind of question seems to get asked more often than I could believe possible.

    The number has been sent as a 16 bit number. It's little endian so the first byte is the lower 8 bits, the second the higher 8 bits, so the number received is ... 0x0017

    which is 23 (16 + 7 == 23)

    Whatever you have on the other side just needs to convert that. If you want to send it as a string instead, convert it to a string on the client and send that instead.

  • Since temp is a uint8_t, the pointer &temp points to a single byte with your temperature, and whatever the following byte in memory happens to be. If you want to send 2 bytes as you're doing now, declare temp to be a 16-bit integer. Also note that you'll probably get the low-order byte first because of the little-endian chip.

  • The temperature appers in hexadecimal, 23=17 in hexadecimal. I tried to send the temperature in a string and the result it's the same. I tried to change the type of variable but i can't because the function that gives me temperature uses that type and to change it i would have to change a lot of librarys and gives me a lot of errors. I can't neather pass from hexadecimal to int on the side of central because the value it's only in one bit. Can anyone help??

  • Hi JoanaBabo!

    If you want to send the temperature value as a string instead,
    you could try writing your code something like this:

    static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)  
    {     
      if(p_data[0]=='?')
      {   
    
      int32_t temp;
      uint8_t data[20]; 
      uint32_t err_code;
    
      err_code = sd_temp_get(&temp);
      APP_ERROR_CHECK(err_code);
    
      temp = (temp / 4);
      sprintf((char *)data, "Temperature: %d", temp); 
      printf("Temperature: %d \n", temp);
    
      ble_nus_string_send(&m_nus, data, sizeof(data));
      }                                                   
    }
    

    Let me know if that doesn't work out for you.

    Best regards,
    Joakim.

Related