Passing a hex string to a payload decoder in Node-RED

Hi,
I’m very new to TTN and am doing some fiddling with Node-Red to convert a hex payload from a TTN water level sensor. Whilst the Javascript code in the TTN console (decoder) works fine, I need to make it work in plain old Node-Red.

When I pass in a HEX string into the decode, it doesn’t work - and I’ve figured out that its because (I think) I’m sending a string (not 8 bits of hex) into the function.

Can anyone tell me what I’m doing wrong?

function Decoder(bytes, port) {
  var decoded = {};

  var sensor_range = 2000;
  var liquid_density = 1;
  var packet_type = bytes[2];
  
  switch (packet_type)
  {
  case 0:
    var pressure = (bytes[3] << 8) + bytes[4];
    decoded = {
      "packet_type": packet_type,
      "pressure" : pressure,
      "temperature" : ((bytes[5] << 8) + bytes[6]) / 1000,
      "voltage" : bytes[7] / 10,
      "level" : Math.round((pressure - 1638.3) * sensor_range / (13106.4 * liquid_density))
    };
  break;
  case 1:
  break;
  default:
  }
   return decoded;

}
msg.payload = Decoder("0065000CF856A022")
return msg;

I’m assuming I’m not representing the hex string properly - and the switch is getting strings not integers returned (so its just defaulting out and doing nothing)

Any thoughts on what I’m doing wrong?

The real output for this payload would be;

{
  "level": 257,
  "packet_type": 0,
  "pressure": 3320,
  "temperature": 22.176,
  "voltage": 3.4
}

Thx
JP

This may help?

And indeed, when using Node-RED already, I’d not use the payload formats in TTN Console anymore.

Aside, note that a LoRaWAN application payload is really an array/buffer/list of bytes, so: values from 0 to 255. Hexadecimal is just one (of the many) human-readable representations of that.

Aside: your decoder does not support negative temperatures. See the post I quoted from above for a fix.

Hi @arjanvanb
Thanks for this, all I needed to do was change the last few lines to pass in the data to the function correctly;

const buf2 = Buffer.from(‘0065000CF856A022’, ‘hex’);
msg.payload = Decoder(buf2)

Thx
JP

However, you shouldn’t be processing a human readable hexadecimal text string. TTN’s MQTT Data API and HTTP Integration will give you msg.payload.payload_raw in Node-RED (if you converted the JSON text into a JavaScript object) with the Base64-encoded raw data, not some human readable hexadecimal text.

If you’re somehow getting a hexadecimal text payload from TTN, then your sensor is basically sending text, and even worse: text that only uses 16 out of the possible 256 characters. (But I assume you’re converting the binary payload into a hexadecimal text yourself, somewhere?)

Yes understand.

For now I’m just testing code that translates the ellenex sensor payload to JSON locally in prep for some other projects. I’m not actually ingesting this data from TTN with this method (I’m just using the NNT Node-red node), just using the code examples to familiarise myself with the sensor.

Thanks for your help!
JP

Assuming you meant “TTN”: what parts of that are you using? Beware it’s no longer supported.

(And did you see my comment about negative temperatures?)