Skip to content

Instantly share code, notes, and snippets.

@felixerdy

felixerdy/TTN_decoder_example.js Secret

Last active Oct 8, 2020
Embed
What would you like to do?
/**
* 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;
var soilTemp1 = uint(bytes.slice(i, i+=2)) / 771 - 18;
soilTemp1 = +soilTemp1.toFixed(3);
var soilMoist1 = uint(bytes.slice(i, i+=2)) / 100;
var soilTemp2 = uint(bytes.slice(i, i+=2)) / 771 - 18;
soilTemp2 = +soilTemp2.toFixed(3);
var soilMoist2 = uint(bytes.slice(i, i+=2)) / 100;
var distance = bytes[i] | bytes[i+1]<<8 | bytes[i+2]<<16;
i = i + 3
// --- insert your sensebox sensor id's here --- //
// --------------------------------------------- //
// you can find the id's after the registration on the opensensemap
// or in the settings of your sensebox (replace SENSEBOX_ID in the link):
// https://opensensemap.org/account/SENSEBOX_ID/edit/sensors
decoded['SENSEBOX_TEMPERATURE_ID'] = temperature
decoded['SENSEBOX_HUMIDITY_ID'] = humidity
decoded['SENSEBOX_PRESSURE_ID'] = pressure
decoded['SENSEBOX_LUX_ID'] = lux
decoded['SENSEBOX_UV_ID'] = uv
decoded['SENSEBOX_PM10_ID'] = pm10
decoded['SENSEBOX_PM25_ID'] = pm25
decoded['SENSEBOX_SOIL_TEMP_1_ID'] = soilTemp1
decoded['SENSEBOX_SOIL_MOIST_1_ID'] = soilMoist1
decoded['SENSEBOX_SOIL_TEMP_2_ID'] = soilTemp2
decoded['SENSEBOX_SOIL_MOIST_2_ID'] = soilMoist2
decoded['SENSEBOX_DISTANCE_ID'] = distance
return decoded;
}
@silberzwiebel

This comment has been minimized.

Copy link

@silberzwiebel silberzwiebel commented Oct 8, 2020

It took me some time to realize that I have to add the sensor IDs into the decoded-array. Could you update the code with variables/explanations (e.g., where to find the sensor IDs and where to put them)?

@felixerdy

This comment has been minimized.

Copy link
Owner Author

@felixerdy felixerdy commented Oct 8, 2020

Thank you for your feedback @silberzwiebel. I just changed the file and hope it's getting a little bit clearer now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment