Skip to content

Instantly share code, notes, and snippets.

@terrillmoore
Created May 31, 2017 10:06
Show Gist options
  • Select an option

  • Save terrillmoore/bb712e2766f082e55e0bd9763344cb80 to your computer and use it in GitHub Desktop.

Select an option

Save terrillmoore/bb712e2766f082e55e0bd9763344cb80 to your computer and use it in GitHub Desktop.
Decoding Catena 4450 sensor1 packets for The Things Network
// Updated 2017-04-20 23:08 EDT tmm@mcci.com -- fix typos in comments
// Add this in your application's "payload formats" section of
// https://console.thethingsnetwork.org
function Decoder(bytes, port) {
// Decode an uplink message from a buffer
// (array) of bytes to an object of fields.
var decoded = {};
if (port === 1) {
cmd = bytes[0];
if (cmd == 0x14) {
// decode Catena 4450 M101 "sensor1" data
// test vectors:
// 14 01 18 00 ==> vBat = 1.5
// 14 01 F8 00 ==> vBat = -0.5
// 14 05 F8 00 42 ==> boot: 66, vBat: -0.5
// 14 0D F8 00 42 17 80 59 35 80 ==> adds one temp of 23.5, rh = 50, p = 913.48
// i is used as the index into the message. Start with the flag byte.
var i = 1;
// fetch the bitmap.
var flags = bytes[i++];
if (flags & 0x1) {
// set vRaw to a uint16, and increment pointer
var vRaw = (bytes[i] << 8) + bytes[i + 1];
i += 2;
// interpret uint16 as an int16 instead.
if (vRaw & 0x8000)
vRaw += -0x10000;
// scale and save in decoded.
decoded.vBat = vRaw / 4096.0;
}
if (flags & 0x2) {
var vRaw = (bytes[i] << 8) + bytes[i + 1];
i += 2;
if (vRaw & 0x8000)
vRaw += -0x10000;
decoded.vBus = vRaw / 4096.0;
}
if (flags & 0x4) {
var iBoot = bytes[i];
i += 1;
decoded.boot = iBoot;
}
if (flags & 0x8) {
// we have temp, pressure, RH
var tRaw = (bytes[i] << 8) + bytes[i + 1];
if (tRaw & 0x8000)
tRaw = -0x10000 + tRaw;
i += 2;
var pRaw = (bytes[i] << 8) + bytes[i + 1];
i += 2;
var hRaw = bytes[i++];
decoded.tempC = tRaw / 256;
decoded.error = "none"
decoded.p = pRaw * 4 / 100.0;
decoded.rh = hRaw / 256 * 100;
}
if (flags & 0x10) {
// we have lux
var luxRaw = (bytes[i] << 8) + bytes[i + 1];
i += 2;
decoded.lux = luxRaw;
}
}
}
return decoded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment