Step by step help for extracting data from the payload in node-red?

Hello,

is there a step by step explanation how to get the data out from the payload to the ui/or something similar anywhere?

Software i use: node-red v0.16.2 / node-red-contrib-ttn v2.0.0 / node-red-dashboard

I can connect to the ttn backend with no problem and get following data in die debug window:

object

  • app_id: “ttn_hennef01”
  • dev_id: “ttn-hennef-badgerboard01”
  • hardware_serial: “70B3D5B020035CC6”
  • port: 19
  • counter: 302
    payload_raw: buffer[21]raw
  • [0 … 9]
  • [10 … 19]
  • [20 … 20]
    payload_fields: object
  • payload: “{“T”:24.82,“H”:39.46}”
    metadata: object
  • time: “2017-02-04T12:08:31.8886388Z”
  • frequency: 868.5
  • modulation: “LORA”
  • data_rate: “SF12BW125”
  • coding_rate: “4/5”
  • gateways: array[1]
    • 0: object
    • gtw_id: “eui-fb10b827eb9798fa”
    • timestamp: 950283428
    • time: “2017-02-04T12:08:31.869213Z”
    • channel: 2
    • rssi: -57
    • snr: 6.8
    • rf_chain: 1
    • latitude: 50.77577
    • longitude: 7.28401
    • altitude: 85
      payload: object
  • payload: “{“T”:24.82,“H”:39.46}”
    _msgid: “c4bdabc2.861d88”

My question is how to get the temperature and humidity out of the payload object? Is there any help on the net for my to learn how to do it? Most sites i find about decoding payloads have just a long bunch of text to copy & paste to node-red to create a hole bunch of nodes without explaining what they do… i am a bit lost (being new to node-red) with what to start and what to look for?

Thank you very much!

https://www.thethingsnetwork.org/docs/applications/nodered/quick-start.html

As an aside: it seems your node is sending plain text, even JSON? That’s a waste of bandwidth. (Is that an example from badgerboard.io?) If you ever change that to send a few bytes instead, then note that decoding can also be done in TTN’s payload functions, so any MQTT client will get ready-to-use data (including Node-RED).

The payload is already decoded in the ttn backend with the decoder function:

function Decoder(bytes, port) {
var decoded = {};
var result = “”;
for (var i = 0; i < bytes.length; i++) {
result += String.fromCharCode(parseInt(bytes[i]));
} return {
payload: result,
};
}

And i followed the quick start guide, but all it does is setting up the software and retrieving the complete data from the backend. The steps which would now follow (converting & splitting up the payload, etc.) are the once i would love to learn. In the example they have { led: false } - what to do with it now? Or in my case i got the {“T”:24.82,“H”:39.46} how to split the two datasets and get the numbers to be shown somewhere (dashboard).

for (var i = 0; i < bytes.length; i++) {
  result += String.fromCharCode(parseInt(bytes[i]));
}
return {
   payload: result
}

Yeah, that doesn’t really count :wink: This just changes a “byte array” of characters into a string value. And it could be replaced with just:

return {
   payload: String.fromCharCode.apply(null, bytes);
}

But still, each character takes one byte to send, so your payload is 21 bytes while the two values would easily fit in 4 bytes. But indeed, Badgerboard is to blame for a bad utility library (and a weird use of printf):

I hope their hardware is better. I guess I need to unwrap the Badgerboards I got and start creating pull requests for their library… :frowning:

All that said: the JSON text that is being sent by the node is still considered to be text after decoding. So, though it looks like it has some structure in it, it is really just text, and could also have been “the temperature is 24.82 and the humidity about 39.46”. But instead of text, you want it to be a JavaScript object. To convert JSON-formatted text to an object in the TTN payload function, use something like:

// Convert the bytes into a String, and convert the JSON string into an object:
// Test using 7b 22 54 22 3a 32 34 2e 38 32 2c 22 48 22 3a 33 39 2e 34 36 7d
return JSON.parse( String.fromCharCode.apply(null, bytes) );

Now it’s no longer just text, but an object with properties H and T. In Node-RED, you can now use a ttn message node to receive the node’s message, connect it to a function node and use something like msg.payload.T to read the value, and debug to print it. (Not tested.)

Taking one step back, to explain a bit more, you can already use that object in the payload function itself, to rewrite the result:

var data = JSON.parse( String.fromCharCode.apply(null, bytes) );
return {
  temperature: data.T,
  humidity: data.H
}

…and test with the same bytes. In Node-RED you now have properties temperature and humidity.

As for displaying and all: I don’t really know what’s easy to use, but see Visualize (and push) your IOT data.

2 Likes

Thank you very much, thats what i was looking for!

You got a link (or so) where i can find info how to better en/decode my stuff to reduce bandwidth? Thank you!

I’ve unwrapped my Badgerboard; see Getting Badgerboard to work with TTN. And see the links in my earlier answers above!

1 Like

Thank you sir for your explanation