Wisely Standard
Last updated
Last updated
// V1.0, 02.09.2021, DS
if(msg.data)
{
var decoded = decodeFromHex(msg.data);
decoded.ts = msg.ts;
decoded.rssi = msg.rssi;
decoded.snr = msg.snr;
decoded.toa = msg.toa;
decoded.frequency = msg.frequency;
decoded.dr = msg.dr;
decoded.bat = decodeBattery(msg.bat);
decoded.hex = msg.data;
return {
msg: decoded,
metadata: metadata,
msgType: msgType
};
}
else{
return {
msg: msg,
metadata: metadata,
msgType: msgType
};
}
//Functions
function decodeFromHex(data) {
// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
var decoded = {};
var bytes = parseHexString(data);
var a = bytes[1] << 8;
var b = bytes[2];
var barometricPressure = barometricPressure = (a|b)*10.0;
decoded.barometricPressure = barometricPressure;
var c = bytes[3] << 8;
var d = bytes[4];
var temperature = temperature = (c|d)/10.0;
decoded.temperature = temperature;
var humidity = bytes[5]/2.0;
decoded.humidity = humidity;
var e = bytes[6] << 8;
var f = bytes[7];
decoded.co2 = (e|f);
if (temperature > 0)
{
//*10^10
var satPressure = 1.40574 * Math.exp(-3928.5 / (temperature + 231.667))*10000000000;
}
else
{
var satPressure = 100 * 361.633 * 10^12 * Math.exp(-6150.6 / (temperature + 273.33))*10000000000;
}
//decoded.satPressure = satPressure;
decoded.absoluteHumidity = 0.6222 * satPressure * humidity/100 / ( barometricPressure - satPressure * humidity/100 ) * 1000
return decoded;
}
function getTimestamp(ddmmyy, hhmmss) {
day = parseInt(ddmmyy / 10000, 10);
month = parseInt(ddmmyy / 100 - day * 100, 10);
year = parseInt(ddmmyy - month * 100 - day * 10000 +
2000, 10);
hour = parseInt(hhmmss / 10000, 10);
minute = parseInt(hhmmss / 100 - hour * 100, 10);
second = parseInt(hhmmss - minute * 100 - hour * 10000,
10);
date = new Date(year, month - 1, day, hour, minute,
second)
return date.valueOf();
}
function parseHexString(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2) {
bytes.push(parseInt(hex.substr(c, 2), 16));
}
return bytes;
}
function decodeBattery(byte) {
if (byte == 0) {
return 'External power source';
} else if (byte > 0 && byte < 255) {
return byte / 254 * 100;
} else {
return 'Unknown battery state';
}
}
function parseHexStringToBytes(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
function parseHexStringToNibbles(hex) {
for (var nibbles = [], c = 0; c < hex.length; c += 1)
nibbles.push(parseInt(hex.substr(c, 1), 16));
return nibbles;
}
function convertToBinary(x) {
var bin = 0;
var rem, i = 1,
step = 1;
while (x != 0) {
rem = x % 2;
x = parseInt(x / 2);
bin = bin + rem * i;
i = i * 10;
}
return bin;
}
function decodeToJson(payload) {
// covert payload to string.
var str = decodeToString(payload);
// parse string to JSON
var data = JSON.parse(str);
return data;
}
function convertToBinaryString(x) {
var bin = "";
var rem, i = 1,
step = 1;
while (x != 0) {
rem = x % 2;
x = parseInt(x / 2);
bin = rem + bin;
i = i * 10;
}
if (bin == "")
{
return "0";
}
else
{
return bin;
}
}