Simple payload conversion

Hi all,
sorry for the dumb question, but i really tried to figure out and failed. I am receiving a hex-string from my Mote and want to decode it in the ttn dashboard with a payload-function.
The hex-code i receive is for instance “3131312030313400” which translates to: Light:111 Temp:14
Can someone provide me a simple function for this conversion or give me a hint?
I tried a couple of hex2string functions etc. but none of them produced the expected result.
Thanks,
Stephan

Instead of sending two numbers, the node is sending the text 111 014 with a trailing null-character (a null-terminated string).

So, mandatory first hint: please don’t send text. This is probably just for testing, so, well, okay. But even just using 8 bytes to send two values that would probably need 2 to 4 bytes in total (depending on the range of the measurements, and if negative values can be sent too), is just not a good idea.

That said, you can use a payload function like:

function Decoder(bytes, port) {

    // Get the string "111 014" out of the bytes 3131312030313400
    var text = String.fromCharCode.apply(null, bytes);

    // Split into an array of 2 strings, on the space character
    var values = text.split(" ");

    // Return as true numbers
    return {
      light: parseInt(values[0]),
      temp: parseInt(values[1])
    }
}
1 Like

Thank you very much! To my apologies - the payload is created by the default-application of the microchip mote…

I was having issues with the payload decoder function for my project. Made some changes to your code based on my payload and now it works a charm. Thank you!

1 Like

But if you based your solution on my text-based, for testing-only code, then it seems you missed the most important part of my answer?