The examples in the sketch generator already send sensor data in a proper way. Like when selecting “Moisture, Temp and Light” then you’ll see:
void build_data() {
dataTX[0] = (light << 4) | (tempK / 256);
dataTX[1] = tempK;
dataTX[2] = moisture / 256;
dataTX[3] = moisture;
}
This sends a total of 4 bytes (32 bits) for 3 sensors:
- 4 bits for light in a scale from 0 to 15 (hexadecimal
0x0
-0xF
) - 12 bits for temperature in Kelvin with one decimal, in a scale from 0.0 to 409.5 (
0x000
-0xFFF
) - 16 bits for moisture in a scale from 0 to 65535 (
0x0000
-0xFFFF
)
If you’d send that as human-readable text, you’d need many more bytes (like 14 characters, so 14 bytes, when sending separated with semi-colons as 15;409.5;65535
, or 0x31353B3430392E353B3635353335
when displayed as hexadecimal ).
It even explains how to decode:
/* **************************************************************
* build data to transmit in dataTX
*
* Suggested payload function for this data
*
* var light = (bytes[0] & 0xF0) >> 4;
* var temp = (((bytes[0] & 0x0F) <<8 | bytes[1]) - 2731) / 10;
* var moisture = bytes[2] << 8 | bytes[3];
*
* return { payload: light + ";" + temp + ";" + moisture };
*
* *************************************************************/
…which you could use to decode and then return the values any way you want:
function Decoder(bytes, port) {
// Test with 0A470000 for 0, -10, 0, or FB78FFFF for 15, 20.5, 65535
var light = (bytes[0] & 0xF0) >> 4;
var temp = (((bytes[0] & 0x0F) << 8 | bytes[1]) - 2731) / 10;
var moisture = bytes[2] << 8 | bytes[3];
return {
light: light,
celcius: temp,
// Unary plus-operator to cast string result of toFixed to a number:
fahrenheit: +(32 + 1.8 * temp).toFixed(1),
moisture: moisture
}
}