Skip to content

Instantly share code, notes, and snippets.

@felixerdy
Last active October 8, 2020 12:34
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/04f8ee955a4f5828be97eb981e5b2d27 to your computer and use it in GitHub Desktop.
Save felixerdy/04f8ee955a4f5828be97eb981e5b2d27 to your computer and use it in GitHub Desktop.
/**
* 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
Copy link

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
Copy link
Author

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