Simple payload conversion

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