Hey there!
I'm using NRF.setAdvertising() to advertise temperature readings from Puck.js. The examples in docs sadly only mention byte arrays, integers and string fragments as supported data:
// straight from official docs
setInterval(function() {
NRF.setAdvertising({
0x1809 : [Math.round(E.getTemperature())]
});
}, 30000);
What I'm trying to do is advertise the floating point temperature without rounding. Puck currently reports temperature with 0.25 accuracy, but I'm struggling with how to encode it in a way, that would make EspruinoHub understand it.
The node on puck doesn't sadly provide Buffer so I tried encoding with TypedArrays like so:
setInterval(function() {
NRF.setAdvertising({
0x1809 : temperatureAsArray()
});
}, 30000);
function temperatureAsArray() {
const a = new Float32Array(1);
a[0] = E.getTemperature();
const b = new Uint8Array(a.buffer);
const c = new Uint8Array(5);
c[0] = 0; // the first byte set to 0 means a single celsius measurement without timestamp
// I've also tried without this byte, didn't change anything
c.set(b, 1);
const result = [];
for (let x = 0; x < c.length; x ++) {
result[x] = c[x];
}
return result;
}
The resulting array is something like: [0, 0, 0, 236, 65] for temperature of around 21 deg, or [0, 0, 236, 65] if you don't include the "flags" byte and just have BE 32-bit float.
However, when I receive it in EspruinoHUB, the debug shows me:
It seems to struggle to decode those Float32 byte arrays.
When I sent something like [236, 65] (without the GATT flags and trimmed to just 2 bytes) then it shows me:
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hey there!
I'm using
NRF.setAdvertising()
to advertise temperature readings from Puck.js.The examples in docs sadly only mention byte arrays, integers and string fragments as supported data:
What I'm trying to do is advertise the floating point temperature without rounding. Puck currently reports temperature with
0.25
accuracy, but I'm struggling with how to encode it in a way, that would make EspruinoHub understand it.The official BLE GATT spec for thermometer actually supports extra flags in the first byte and temp. reading (default is Celsius) as a Float32.
The node on puck doesn't sadly provide
Buffer
so I tried encoding withTypedArrays
like so:The resulting array is something like:
[0, 0, 0, 236, 65]
for temperature of around 21 deg, or[0, 0, 236, 65]
if you don't include the "flags" byte and just have BE 32-bit float.However, when I receive it in EspruinoHUB, the debug shows me:
It seems to struggle to decode those Float32 byte arrays.
When I sent something like
[236, 65]
(without the GATT flags and trimmed to just 2 bytes) then it shows me:I have no idea how it got
168.75
from those bytes.Any idea what EspruinoHub/Noble/bleno expect as a format for floats?
Have anyone succeeded in encoding floats for BLE Adv. ?