Working with frm_payload

I need to write a decoding program to format the payload and sending it to a webhook that I configured.
The program I need to write needs the hex string to decode it. So my question is how to you pull the frm_payload and the f_port out of the event details and into my script to decode it.

You should ordinarily only be trying to work with the ultimately validated and decrypted application payload that along with the port and other metadata is the ultimate output of TTN’s servers.

This is a totally routine day to day setup activity. You may want to check out the extensive documentation on Webhooks and Payload Formatters - links to docs at the bottom right of every console page.

I looked at the documentation but I don’t find the solution to my problem. I want the hex value for decoding but I only get the decimal value so now I’m trying to convert dec to hex but that’s not quite working yet. Is there a way to get the frm_payload or do I really need to convert it first to decimal and then back again to hexadecimal?

From the docs on Payload Formatters:

The Javascript decodeUplink() function is called when a data uplink message is received from a device. This function decodes the binary payload (frm_payload ) received from the end device to a JSON object (decoded_payload ) that gets send upstream to the application.

This gives you an array of numbers - how you look at them is up to you - you can think of them in hex, decimal, binary or any other number system - the numbers are still the same.

Perhaps you could show us what you are expecting from your device, what steps you’d take to change it and what the output should be.

Okay so a quick rundown on what I need to do.

The sensor I use sends a hex string which I need to use…

This is the payload that the sensor gives us:
7C328DE801000805010474736574023C00000A024AB72B0000

By example: The following hex values represent the device ID that I need to convert it to ASCII.
74 73 65 74 → test (LSB first)

Now I’m trying to convert everything one by one to decimals and then back again to hexadecimal but that doesn’t go so smoothly.

This is my code to convert the first hex value (7C) to a decimal number (124):

var decoded = {};

function Decoder(bytes, port) {
  if (port == 1) {
    decoded.test = bytes[0];
  }
  return decoded;
}

Now I have 2 questions:

  1. Is there a way to convert the decimal number back again to a hexadecimal. I searched the internet and try a lot of examples but always without success.

  2. Can I start with the hex string or the frm_payload so I don’t need to convert it first to a decimal.

I think the solution is probably quite simple but I keep overlooking it and I hope that someone can give my a push in the right direction.

Why - it is already in hex.

As well as binary, decimal, octal and any other number system you can think of. A number is a number is a number, it’s representation is just how we chose to look at it.

Why do you need to do this conversion - what is the output?

I know it is already in hex, but I don’t know how to get that value into a string by example so I can decode it.
That’s why I created the post., to ask for an example or an explanation to get the hex string.

I want to start very simple and put that hex value into a variable and return that variable so I can see the hex string in the output.

To be blunt, your question makes no sense without understanding what the expected output is and it also possibly reveals some gaps in knowledge about fundamental data formats.

To help further I’d need to see what you want to see - it’s clear the example you gave above starting 7C32 is not actually text, so it’s all a bit perplexing.

My knowledge is not very big on these things yet because I’m very new to TTN and I’m still learning everything.

I have this:
image

I want that payload (starting with 7C32) into a variable so I can make a script to decode it following the guidelines of the datasheet I got from the company of the sensor.

You always need to start small so I want in the output section to see 7C and not 124. I tried several things but always without success.

It would be so much easier to see if a payload formatter is available - what is the device and where can we see the data sheet?

I still don’t understand why it needs turning in to a string but Google has come up with so many results for “javascript byte to hex string” I don’t know where to look / start. Maybe you could review the options. Also, are you able to iterate over the array - if not, Google for ‘javascript for loop’ - not completely efficient but will get the job done.

This is not an appropriate payload for a LoRaWAN system such as TTN.

LoRaWAN already includes unique device identity in the protocol headers and crypto. Because airtime is precious identity should not be wastefully repeated in the packet payload.

Likely a lot of the rest is also inappropriately encoded