Skip to content

Instantly share code, notes, and snippets.

@Thiemann96
Created October 10, 2019 12:23
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 Thiemann96/90979aa82820e8adbe42e2788817a239 to your computer and use it in GitHub Desktop.
Save Thiemann96/90979aa82820e8adbe42e2788817a239 to your computer and use it in GitHub Desktop.
Payload function corresponding to the GROVE_CO2.ino Sketch
function Decoder(bytes, port) {
// bytes is of type Buffer.
'use strict';
var TEMPSENSOR_ID,
HUMISENSOR_ID,
PRESSURESENSOR_ID,
LUXSENSOR_ID,
UVSENSOR_ID,
PM10_ID,
PM25_ID,
CO2_ID;
switch (port) {
case 1:
TEMPSENSOR_ID = '';
HUMISENSOR_ID = '';
PRESSURESENSOR_ID = '';
LUXSENSOR_ID = '';
UVSENSOR_ID = '';
PM10_ID = '';
PM25_ID = '';
CO2_ID = '';
break;
default:
throw new Error('No configured sensor IDs for uplink on port: ' + port);
}
var bytesToInt = function (bytes) {
var i = 0;
for (var x = 0; x < bytes.length; x++) {
i |= +(bytes[x] << (x * 8));
}
return i;
};
var uint8 = function (bytes) {
if (bytes.length !== uint8.BYTES) {
throw new Error('int must have exactly 1 byte');
}
return bytesToInt(bytes);
};
uint8.BYTES = 1;
var uint16 = function (bytes) {
if (bytes.length !== uint16.BYTES) {
throw new Error('int must have exactly 2 bytes');
}
return bytesToInt(bytes);
};
uint16.BYTES = 2;
var humidity = function (bytes) {
if (bytes.length !== humidity.BYTES) {
throw new Error('Humidity must have exactly 2 bytes');
}
var h = bytesToInt(bytes);
return h / 1e2;
};
humidity.BYTES = 2;
var decode = function (bytes, mask, names) {
var maskLength = mask.reduce(function (prev, cur) {
return prev + cur.BYTES;
}, 0);
if (bytes.length < maskLength) {
throw new Error('Mask length is ' + maskLength + ' whereas input is ' + bytes.length);
}
names = names || [];
var offset = 0;
return mask
.map(function (decodeFn) {
var current = bytes.slice(offset, offset += decodeFn.BYTES);
return decodeFn(current);
})
.reduce(function (prev, cur, idx) {
prev[names[idx] || idx] = cur;
return prev;
}, {});
};
var bytesToSenseBoxJson = function (bytes) {
var json;
try {
json = decode(bytes,
[
uint16,
humidity,
uint16,
uint8,
uint16,
uint8,
uint16,
uint16,
uint16,
uint16
],
[
TEMPSENSOR_ID,
HUMISENSOR_ID,
PRESSURESENSOR_ID,
LUXSENSOR_ID + '_mod',
LUXSENSOR_ID + '_times',
UVSENSOR_ID + '_mod',
UVSENSOR_ID + '_times',
PM10_ID,
PM25_ID,
CO2_ID
]);
//temp
json[TEMPSENSOR_ID] = parseFloat(((json[TEMPSENSOR_ID] / 771) - 18).toFixed(1));
//hum
json[HUMISENSOR_ID] = parseFloat(json[HUMISENSOR_ID].toFixed(1));
// lux
json[LUXSENSOR_ID] = (json[LUXSENSOR_ID + '_times'] * 255) + json[LUXSENSOR_ID + '_mod'];
delete json[LUXSENSOR_ID + '_times'];
delete json[LUXSENSOR_ID + '_mod'];
// uv
json[UVSENSOR_ID] = (json[UVSENSOR_ID + '_times'] * 255) + json[UVSENSOR_ID + '_mod'];
delete json[UVSENSOR_ID + '_times'];
delete json[UVSENSOR_ID + '_mod'];
// pressure
if (json[PRESSURESENSOR_ID] !== '0') {
json[PRESSURESENSOR_ID] = parseFloat(((json[PRESSURESENSOR_ID] / 81.9187) + 300).toFixed(1));
} else {
delete json[PRESSURESENSOR_ID];
}
// rain
// pm10
json[PM10_ID] = parseFloat((json[PM10_ID] / 10).toFixed(1));
// pm25
json[PM25_ID] = parseFloat((json[PM25_ID] / 10).toFixed(1));
json[CO2_ID] = parseFloat((json[CO2_ID] / 10).toFixed(1));
} catch (e) {
json = { payload: bytes };
}
return json;
};
return bytesToSenseBoxJson(bytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment