# Decoding Jeelib binary packets

**URL:** <https://discourse.nodered.org/t/decoding-jeelib-binary-packets/6577>\
**Category:** General\
**Created:** [8 January 2019 19:30 UTC](https://discourse.nodered.org/t/decoding-jeelib-binary-packets/6577 "2019-01-08T19:30:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [8 January 2019 19:30 UTC](https://discourse.nodered.org/t/decoding-jeelib-binary-packets/6577/1 "2019-01-08T19:30:42Z")

</div>

Using a jeelabs HAT on a Raspberry Pi to receive RF data from remote sensors, which can then be picked up by a 'serial in node' from `/dev/ttyAMA0`.

The payload received by the serial node takes the following format;  
`OK 10 22 1 0 0 0 0 237 94 188 4 (-61)` where `OK` is the acknowledgement, `10` is the respective device node, and `(-61)` is the RSSI of the device.

The remaining 10 bytes are data pairs, which then need decoding into 5 device sensor readings as follows;

```auto
x= ([1st byte] + ([2nd byte] * 2^8));
      if (x > 2^15) {
            x = (x - (2^16)); 
            }
     return x

```

Each device follows the same format but may have a different number of byte pairs, for example a temperature node may have 3 byte pairs - temperature, humidity & battery.

The goal would be to get the data into a usable format, something like??  
 ![byte](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/20f981b3db47bc3ea9c9e4d483142d4205d11dd9.png)

I've looked through existing nodes in the library, and can't see anything specifically for jeelib, but there a `node-red-contrib-binary` although it doesn't seem to handle this format.

I really don't know where to start with this, so any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 January 2019 19:56 UTC](https://discourse.nodered.org/t/decoding-jeelib-binary-packets/6577/2 "2019-01-08T19:56:14Z")

</div>

If it’s all space separated I would check it starts with OK then split on space to create an array. Then shift off the ok and the first (node) value, pop the Rossi from the other end. Then iterate over the remaining length/2 times to create the values using the formula.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [8 January 2019 21:01 UTC](https://discourse.nodered.org/t/decoding-jeelib-binary-packets/6577/3 "2019-01-08T21:01:03Z")

</div>

> [@dceejay](#):
>
> If it’s all space separated I would check it starts with OK then split on space to create an array. Then shift off the ok and the first (node) value, pop the rssi from the other end...

I've got this far, but no idea how to _"Then iterate over the remaining length/2 times to create the values using the formula"_

This is what I have so far;

```auto
if (msg.payload.startsWith("OK")) {
  var x = msg.payload.split(" ");
  var ack = x.shift();
  var node = x.shift();
  var rssi = (x.pop().slice(1,-1));
    msg.ack = ack;
    msg.node = node;
    msg.rssi = rssi;
    msg.payload = x;
    return msg;
  } else {
    return null;
    }

```

...or my example flow -

```auto
[{"id":"a7703a1f.7323e8","type":"debug","z":"c53060.842a0fa","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":380,"y":910,"wires":[]},{"id":"7799b6ac.121f08","type":"inject","z":"c53060.842a0fa","name":"","topic":"","payload":"OK 10 22 1 0 0 0 0 237 94 188 4 (-61)","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":910,"wires":[["bb91aa6e.1e7dd8"]]},{"id":"bb91aa6e.1e7dd8","type":"function","z":"c53060.842a0fa","name":"","func":"if (msg.payload.startsWith(\"OK\")) {\nvar x = msg.payload.split(\" \");\nvar ack = x.shift();\nvar node = x.shift();\nvar rssi = (x.pop().slice(1,-1));\n msg.ack = ack;\n msg.node = node;\n msg.rssi = rssi;\n msg.payload = x;\n return msg;\n } else {\n return null;\n }","outputs":1,"noerr":0,"x":250,"y":910,"wires":[["a7703a1f.7323e8"]]}]

```

...and again - the formula is;

```auto
x= ([1st byte] + ([2nd byte] * 2^8));
      if (x > 2^15) {
            x = (x - (2^16)); 
            }
     return x

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 January 2019 22:14 UTC](https://discourse.nodered.org/t/decoding-jeelib-binary-packets/6577/4 "2019-01-08T22:14:09Z")

</div>

try this

```auto
if (msg.payload.startsWith("OK")) {
  var x = msg.payload.split(" ");
  msg.ack = x.shift();
  msg.node = x.shift();
  msg.rssi = parseInt(x.pop().slice(1,-1));
  
  for (var i=0; i < x.length; i++) {
      msg[i/2] = parseInt(x[i]) + parseInt(x[i+1]) * 256;
      if (msg[i/2] > 32768) { msg[i/2] = msg[i/2] - 65536; }
      i++;
  }
  delete msg.payload;
  return msg;
} 
return null;

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [8 January 2019 23:13 UTC](https://discourse.nodered.org/t/decoding-jeelib-binary-packets/6577/5 "2019-01-08T23:13:27Z")

</div>

Thank you, almost there!  
Using live data, I'm seeing 6 values in the array, instead of 5. The 6th is NaN.

The test data (which was obtained via the debug panel)

![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4132fa481ec917fee195fd03e2c8e72ee493b5a3.png)

..which works OK, however,  
using 'live' data, I'm getting

![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/09dd1061622f341c4d239d36f7a2c3f8f67a5ec2.png)

...which has added the additional NaN to the array, and the payload is defined differently...

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 January 2019 23:17 UTC](https://discourse.nodered.org/t/decoding-jeelib-binary-packets/6577/6 "2019-01-08T23:17:55Z")

</div>

maybe some extra hidden chars at the end of the line or something ?

maybe `var x = msg.payload.trim().split(" ");`

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [8 January 2019 23:42 UTC](https://discourse.nodered.org/t/decoding-jeelib-binary-packets/6577/7 "2019-01-08T23:42:13Z")

</div>

Yep! that has sorted it.  
Thanks for your help Dave, I've learnt a lot through this....  
never done 'shift' or 'pop' before......

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [9 January 2019 08:45 UTC](https://discourse.nodered.org/t/decoding-jeelib-binary-packets/6577/8 "2019-01-09T08:45:08Z")

</div>

learning is what it's all about 🎉
