Skip to content

Instantly share code, notes, and snippets.

@felixerdy
Created January 4, 2020 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixerdy/f8434ee0c081f1b6e711439cb6a001b7 to your computer and use it in GitHub Desktop.
Save felixerdy/f8434ee0c081f1b6e711439cb6a001b7 to your computer and use it in GitHub Desktop.
TTN Decoder senseBox Full Setup
/**
* Convert the array of bytes to an unsigned integer, LSB.
*
* BEWARE: This is only safe up to 0x1FFFFFFFFFFFFF, so: 6 bytes.
*/
function uint(bytes) {
return bytes.reduceRight(function(acc, b) {
// We expect an unsigned value, so to support more than 3 bytes
// don't use any bitwise operators, which would always yield a
// signed 32 bits integer instead.
return acc * 0x100 + b;
}, 0);
}
function Decoder(bytes) {
var i = 0;
var decoded = {};
// The index in the bytes array that needs to be handled next;
// use along with "i++" which returns the current value, and
// then increments it for the next usage
// Temperature with 0.0013 precision, -18.000 through 67.000, LSB
var temperature = uint(bytes.slice(i, i+=2)) / 771 - 18;
// Unary plus operator to cast string result of toFixed to number
temperature = +temperature.toFixed(3);
// Relative humidity with 0.01 precision, 0.00 through 100.00, LSB
var humidity = uint(bytes.slice(i, i+=2)) / 100;
// Pressure with 0.012 precision, 300.00 through 1100.00, LSB
var pressure = uint(bytes.slice(i, i+=2)) / 81.9187 + 300;
pressure = +pressure.toFixed(2);
var lux = bytes[i] | bytes[i+1]<<8 | bytes[i+2]<<16;
i = i + 3
var uv = bytes[i] | bytes[i+1]<<8 | bytes[i+2]<<16;
i = i + 3
var pm10 = uint(bytes.slice(i, i+=2)) / 10;
var pm25 = uint(bytes.slice(i, i+=2)) / 10;
decoded['5e0f91d2b0c088001b89ff2f'] = temperature
decoded['5e0f91d2b0c088001b89ff2e'] = humidity
decoded['5e0f91d2b0c088001b89ff2d'] = pressure
decoded['5e0f91d2b0c088001b89ff2c'] = lux
decoded['5e0f91d2b0c088001b89ff2b'] = uv
decoded['5e0f91d2b0c088001b89ff2a'] = pm10
decoded['5e0f91d2b0c088001b89ff29'] = pm25
return decoded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment