You’re multiplying by 100, and storing that in memory as an “unsigned 16 bits integer” (uint16
), hence losing all remaining decimals. For example: 12.34567 is stored and sent as 1234, which is probably just fine as most sensors really don’t have an accuracy of 4 decimals (or even 2 decimals). It even says this in your code:
// Read sensor values and multiply by 100 to effictively have 2 decimals
uint16_t humidity = dht.readHumidity(false) * 100;
As 2 bytes unsigned integers can only hold values 0 to 65535, you don’t really have room to multiply by, say, 1000 to get 3 decimals as then any value larger than 65.5 would cause an overflow with unexpected results.
Things are even more complicated if the values might be negative, in which case the JavaScript in the payload functions expects 4 bytes to do proper calculations for negative values:
// Signed 16 bits integer, -32767 up to +32767
int16_t temperature = dht.readTemperature(false) * 100;
…with in the payload function:
decodedValue3 = (bytes[0] & 0x80 ? 0xFFFF<<16 : 0) | bytes[0]<<8 | bytes[1];
More details on the above “sign-extension” in Decrypting messages for dummies (and more links to examples in Is there any documentation on payload functions?).
You’re also mixing the values. You’re sending 2 bytes for temperature, humidity and weight. But you’re are showing that as humidity, temperate and weight. So, 20.3
is your temperature. I’d clean up and use the same order in your code:
// For staging-v1
function(bytes) {
// temperature might be negative; sign-extend to get 4 bytes:
var t = (bytes[0] & 0x80 ? 0xFFFF<<16 : 0) | bytes[0]<<8 | bytes[1];
var h = bytes[2]<<8 | bytes[3];
var w = bytes[4]<<8 | bytes[5];
return {
temperature: t / 100,
humidity: h / 100,
weight: w / 100
}
}
I don’t understand why you’re getting a humidity of exactly 1. What does the false
in the formula do? I guess the result of dht.readHumidity(false)
is 1
or 1.00
, which multiplied by 100 is sent as 100
and then decoded back to 1
.
By the way, on the production environment the first line of a Decoder payload function needs to read:
function Decoder(bytes, port) {
And as Staging will be taken offline at the end of March, I strongly advise to use production.