Decoder Functions

Decoder Functions Overview

When a LoRaWAN device sends an uplink message, it contains data that needs to be extracted and converted into a format that can be used by your application. This is where a decoder function comes in.

Function Schema

A decoder function is a function that takes an uplink message as input and returns a JSON object with the decoded fields.

function decoder(input){}

Function paramaters

The Qubitro decoder function has a single parameter called input. The input parameter is supplied by Qubitro automatically and has three properties:

  • bytes: A byte array that contains the raw data from the uplink message.
  • fPort: The LoRaWAN fPort that was used to send the uplink message.
  • metadata: A dynamic message that comes from uplink messages from LNS providers. The contents of metadata vary depending on the LNS provider.

The following function shows how to access these properties:

decoder.js
function decoder(input){
    var bytes = input.bytes // Get the raw bytes
    var fPort = input.fPort // Get the port
    var metadata = input.metadata // Get the metadata
    return {};
}

Return Values

The decoder function must return a JSON object. For complete compatibility with the no-code suite, it has to be a key-value JSON object.

For example, if the bytes in the bytes property represent the temperature, then the value of the temperature can be calculated by converting the bytes to a binary number and then interpreting the binary number as a decimal number.

Here is an example of a decoder function that decodes a LoRaWAN uplink message that contains the temperature:

function decoder(input) {
  // Extract the bytes, fPort, and metadata properties from the input object.
  const bytes = input.bytes;
  const fPort = input.fPort;
  const metadata = input.metadata;
 
  // Calculate the temperature from the bytes.
  const temperature = ((bytes[0] << 8) + bytes[1]) / 100;
 
  // Create a JSON object with the decoded fields.
  const json = {
    "bytes": bytes,
    "fPort": fPort,
    "temperature": temperature,
    "metadata": metadata,
  };
 
  // Return the JSON object.
  return json;
}

logo

Start building today

Collect, process, and activate device data. Scale from one device to thousands.

Qubitro Logo

Product

Decision Aids

Resources

Company

© 2024 Qubitro, Inc. All rights reserved