How do I decode data from different nodes?

Hi

I have several LoRa nodes; ttn-node, ST nodes, UNO nodes and others.
To start with I have the ttn-node running and have the payload format as this,

{
function Decoder(bytes, port) {
  var decoded = {};
  var events = {
    1: 'setup',
    2: 'interval',
    3: 'motion',
    4: 'button'
  };
  decoded.event = events[port];
  decoded.battery = ((bytes[0] << 8) + bytes[1])/1000;
  decoded.light = (bytes[2] << 8) + bytes[3];
  if (bytes[4] & 0x80)
    decoded.temperature = ((0xffff << 16) + (bytes[4] << 8) + bytes[5]) / 100;
  else
    decoded.temperature = ((bytes[4] << 8) + bytes[5]) / 100;
  return decoded;
}

Which seems to work OK, I got this from somewhere on the ttn pages a long time ago showing how to use the ttn-node.

But now I’m exploring adding additional nodes that give different data and so need to be interpreted differently.

For example the ttn example ‘send’ sketch for the UNO node after setting up is this,

void loop()
{
  debugSerial.println("-- LOOP");

  // Read the sensors
  data.motion = true;
  data.water = 682;
  data.temperature_celcius = 30;
  data.temperature_fahrenheit = 86;
  data.humidity = 97;

  // Encode the selected fields of the struct as bytes
  byte *buffer;
  size_t size;
  TheThingsMessage::encodeDeviceData(&data, &buffer, &size);

  ttn.sendBytes(buffer, size);

  delay(10000);
}

Now if use the UNO node the data it sends goes through the same payload format as the ttn-node obviously the data is then presented wrong.
How do I use different payload formats for the ttn-node and the UNO node??
Knowing that would help getting other nodes working too.

Many thanks for any help or pointers

Did you see Multiple sensors with different payload decoding TTN?

1 Like

Hi

No I haven’t, I did search but there didn’t appear to be anything relevant, obviously I used the wrong words… :slight_smile:

I had tried looking for a way to test which node it came from by trying to use the Device EUI or the Device ID but I failed at that too.

Going for a look now.

Thanks

You won’t have access to the DevEUI in a Payload Format, but other APIs do expose it. How to best write an application that contains many nodes with different measure data types might help (this was deleted until a few minutes ago).

1 Like

Hi

Had a read and in there it suggests that to set the port number when sending data from a node (in arduino cpp) you just need to add the port number to the end of the data send line like this

ttn.sendBytes(buffer, sizeof(buffer), 1);

The number 1 being the port number.

My code had this as the data send line;

ttn.sendBytes(buffer, size);

So I added the 1 to the line.
My line is now changed to;

ttn.sendBytes(buffer, size, 1);

But the port used is still random.
Any idea what I am doing wrong?

Regards

If your data formats can never occur as options from the same device, you can make them specific to different Applications.

If your data formats are a variety of possibilities which might occur in some combination on the same devices now or in the future, tell them apart by embedded information. The LoRaWAN port number is the most obvious choice: since it is (most of) a byte you “pay” to send regardless if you use it or not, you might as well use it and not waste it.

In the unlikely even that you run out of distinct ports you could could embedded other information, or even use the packet length as an additional distinction allowing you to re-use port numbers between brief and longer form formats.

(But remember of course that the maximum packet length supportable depends on the region and spreading factor; most LoRaWAN stacks will refuse to send overly long packets rather than shorten them)

That code looks okay according to the API documentation. Where are you seeing that random port number? And it seems it was random without that 3rd parameter as well? It should default to 1 anyhow.

(Aside: the Markdown formatting uses backticks for the fenced code block; see How do I format my forum post? [HowTo] for more details.)

2 Likes

Sorry…

Stupidly I had a dozens of windows open looking at data, writing code, programming nodes and messaging.

I was looking at the wrong data window.
It is working thank you. Changing that number does change the port the data is sent.

Sorry about the formatting with the code I’ll try harder next time.

Thanks again.

2 Likes