Skip to content

Commit

Permalink
Incorporating avbentem's fixes for negative temperatures and PIR hand…
Browse files Browse the repository at this point in the history
…ling, fixes #1
  • Loading branch information
eggfriedrice committed Jan 29, 2019
1 parent 750c53c commit bac236d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -28,3 +28,8 @@ This should be turned into a JSON object that looks like this:
(actual data from my particularly chilly office)

This code is MIT licenced, and we don't claim it to be excellent, pull requests are encouraged! This isn't yet complete, it doesn't yet support accelerometer payloads for example. Again, pull request!

## Contributors
Many thanks to:
* [avbentem](https://github.com/avbentem) for suggesting fixes to negative temperature and PIR handling

8 changes: 5 additions & 3 deletions tektelic-kona-home-ttn-decoder.js
Expand Up @@ -22,7 +22,9 @@ function Decoder(bytes, port) {
for (var i = 0; i < bytes.length; i++) {
// Handle temperature
if(0x03 === bytes[i] && 0x67 === bytes[i+1]) {
params.temperature = 0.1 * ((bytes[i+2] << 8) | bytes[i+3]);
// Sign-extend to 32 bits to support negative values, by shifting 24 bits
// (16 too far) to the left, followed by a sign-propagating right shift:
params.temperature = (bytes[i+2]<<24>>16 | bytes[i+3]) / 10;
i = i+3;
}

Expand All @@ -41,11 +43,11 @@ function Decoder(bytes, port) {
// Handle PIR activity
if(0x0A === bytes[i] && 0x00 === bytes[i+1]) {
params.activity = true;
i = i+2;
i = i+1;
}
else if(0x0A === bytes[i] && 0xFF === bytes[i+1]) {
params.activity = false;
i = i+2;
i = i+1;
}

// Handle reed switch state
Expand Down

0 comments on commit bac236d

Please sign in to comment.