Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Chirpstack Uplink Decoder

Code Block
languagejs
// Decode uplink function.
//
// Input is an object with the following fields:
// - bytes = Byte array containing the uplink payload, e.g. [255, 230, 255, 0]
// - fPort = Uplink fPort.
// - variables = Object containing the configured device variables.
//
// Output must be an object with the following fields:
// - output = Object representing the decoded payload.
function decodeUplink(input)
{
  return {
    data: Decode(input.fPort, input.bytes, input.variables)
  };
}


function Decode(fPort, bytes)
{
  var output = {};
  switch (fPort)
  {
    case 1:
    {
      output.NTC1_Raw_Value = bytes[0] * 0.5;
      output.NTC1_Temperature = bytes[1] * 0.5;
      output.NTC2_Raw_Value = bytes[2] * 0.5;
      output.NTC2_Temperature = bytes[3] * 0.5;
      output.Battery_Low = bytes[4] >> 4 & 0x01;
      output.Radio_Communication_Error = bytes[4] >> 3 & 0x01;
      output.Received_Signal_Strength = bytes[4] >> 2 & 0x01;
      output.NTC2_Sensor_Error = bytes[4] >> 1 & 0x01;
      output.NTC1_Sensor_Error = bytes[4] >> 0 & 0x01;
      output.Storage_Voltage = Number((bytes[5] * 0.02).toFixed(2));
      break;
    }
    case 2:
    {
      {
        var REV_Major = (bytes[0] & 0xF).toString();
        var REV_Minor = ((bytes[0] >> 4) & 0xF).toString(16);
        output.REV = REV_Major + "." + REV_Minor;
      }
      {
        var HW_Major = (bytes[1] & 0xF).toString();
        var HW_Minor = ((bytes[1] >> 4) & 0xF).toString();
        output.HW = HW_Major + "." + HW_Minor;
      }
      {
        var FW_Year = bytes[2].toString();
        var FW_Month = bytes[3].toString();
        var FW_Day = bytes[4].toString();
        var FW_Minor = bytes[5].toString();
        output.FW = "20" + FW_Year + "." + FW_Month + "." + FW_Day + "." + FW_Minor;
        break;
      }
    }
    default:
      return {
        errors: ['unknown FPort'], };
  }
  return output;

}

Uplink Decoder Example

...

Chirpstack Downlink Encoder

Code Block
languagejs
// Encode downlink function.
//
// Input is an object with the following fields:
// - output = Object representing the payload that must be encoded.
// - variables = Object containing the configured device variables.
//
// Output must be an object with the following fields:
// - bytes = Byte array containing the downlink payload.
// Possible Options:
// radioInterval     : 5 or 10 or 15 or 20 or 30 or 60 or 120 or 480 (minutes)
function encodeDownlink(input)
{
  let radioInterval = input.data.radioInterval;
  let bytes = [0];

  switch (radioInterval)
  {
    case 5:
      bytes[0] = 0; // Radio interval 5 minutes
      break;
    case 10:
      bytes[0] = 1; // Radio interval 10 minutes
      break;
    case 15:
      bytes[0] = 2; // Radio interval 15 minutes
      break;
    case 20:
      bytes[0] = 3; // Radio interval 20 minutes
      break;
    case 30:
      bytes[0] = 4; // Radio interval 30 minutes
      break;
    case 60:
      bytes[0] = 5; // Radio interval 60 minutes
      break;
    case 120:
      bytes[0] = 6; // Radio interval 120 minutes
      break;
    case 480:
      bytes[0] = 7; // Radio interval 480 minutes
      break;
    default:
      throw new Error("Invalid radio interval");
  }

  return { "bytes": bytes };
}

Downlink Encoder Example

Code Block
languagejson
{
"radioInterval":20
}