How to assemble a float of two bytes?

I am currently working on a project where I need to read a DHT11 humidity and temperature sensor . The connection between the MCU and the serial device is quite low, but I managed to get the measured values ​​(humidity + temperature) as a 4-byte array (the fifth byte is the checksum):

The values ​​I get from the DHT11 sensor are:

- byte[0] = humidity integer part - byte[1] = humidity decimal part - byte[2] = temperature integer part - byte[3] = temperature decimal part - byte[4] = checksum of the first four bytes 

Now I would like to convert byte[0] and byte[1] to float and the same for temperature (byte [2] and byte [3]). What is the efficient way to accomplish this on an Arduino Mega 2560 in C / C ++?

Example:

 byte[0] = 20 and byte[1] = 12 => 20.12 [float] 
+5
source share
1 answer

Unfortunately, both examples in the linked data sheet send zero for the decimal part. However, the description shows that the data from the upper byte can be added to the data of the lower byte divided by 256 (the number of states in the decimal part of the data):

 const float scale = 256.0; float humidity = byte[0] + (byte[1] / scale); float temperature = byte[2] + (byte[3] / scale); 
+5
source

Source: https://habr.com/ru/post/1268045/


All Articles