How to Send float value from arduino to redbear


#1

Hi Team,

unsigned char buf[100] = {0};
unsigned char len = 0;

float value = 1.117;

len = sprintf(buff, “%d”, value);
ble_write_bytes(buff, len);

Expected output: 1.117
Actual output = 1

How to get 1.117 as output?


#2

Hi meghana,
Although it is not advise you to transfer float type because it occupy large memory, you can use this union way to have a test

float send = 1.117; 

union change
{
float d1;
unsigned char d2[4];
}r;

r.d1=send;
ble_write_bytes(r.d2, 4)

#3

Hi,
maybe it is a solution for you to multiply the float value by, let’s say 1000, to conserve three decimal places, then convert the resulting float to an integer so the remaining decimal places behind the comma get dropped. Now you have to convert the integer in char values. You can do that by splitting the integer value in 32/8=4 characters by bitwise shifting or convert the integer in a string with itoa() or something like that. Of course you have to undo these steps on the receiver side.
I know that it is not a very beautiful solution…