CONTENTS


Introduction

When Milesight gateway work as embedded network server, it supports to forward data to third party server via MQTT/HTTPS and it will send every data as below JSON format:

{
  "applicationID": "1",
  "applicationName": "cloud",
  "data": "A2fqAARoTwUAAA==",
  "devEUI": "24e124136b502217",
  "deviceName": "EM300-TH",
  "fCnt": 128,
  "fPort": 85,
  "rxInfo": [
    {
      "altitude": 0,
      "latitude": 0,
      "loRaSNR": 13.8,
      "longitude": 0,
      "mac": "24e124fffef54092",
      "name": "Local Gateway",
      "rssi": -51,
      "time": "2022-12-27T07:21:27.078763Z"
    }
  ],
  "time": "2022-12-27T07:21:27.078763Z",
  "txInfo": {
    "adr": true,
    "codeRate": "4/5",
    "dataRate": {
      "bandwidth": 125,
      "modulation": "LORA",
      "spreadFactor": 7
    },
    "frequency": 868100000
  },
  "metadata": {
    "mqtt_topic": "/mqtttest"
  }
}

 This article will guide you how to custom uplink and downlink contents via Payload Codec feature.


 

Requirement

  • Milesight LoRaWAN Gateway: UG65/UG67(v60.0.0.41 and later), UG56 (v56.0.0.3 and later)
  • Any LoRaWAN Sensor


Configuration

Inbuilt Payload Codec

Milesight gateway has supported the inbuilt payload codec library to add Milesight decoders and encoders easily. It supports two obtaining types:

Online: when gateway is able to access the Internet, it will check the update and update the library automatically. You can also click Obtain to check the update.

Local Upload: upload the zip format decoder package locally. The decoder package can be found here.



Custom Payload Codec

If you use other brand devices or the default decoder does not work with your application, please add a custom payload codec:

  • Name: customize a unique name for this codec
  • Description: add the description as required
  • Template: select a template from inbuilt payload codec library
  • Payload Decoder Function: convert HEX format data to JSON format results
  • Payload Decoder Function: convert JSON format messages to HEX format commands
  • Payload Codec Test: input the HEX format data to test encoder or JSON format messages to test encoder
  • Object Mapping Function: only add if you require to use BACnet server or Modbus server feature of the gateway (UG65/67 with firmware V60.0.0.45 and later, UG56 with firmware V56.0.0.5 and later)

Note: 

1. The supported JavaScript version of payload decoder and encoder is ES5.

2. Usually the variable names used in decoders and encoders of one Payload Codec should be the same if they point to the same items.



Milesight gateways provide multiple solutions to customize the decoders to change the report content to MQTT/HTTP server. Take EM300-TH as example, here are below steps to customize the decoders to change the report content.


Solution 1: Only report decoded sensor data.

Click here to find the decoder and paste it to Payload decoder function box.

Note: If you configure the sensor from other company, please contact their support to get the Decoder Script (Java Script), then ensure the script header is function Decode(fPort, bytes) when paste.

You can use Payload Codec Test feature to test the uplink result.

Uplink Result:

{
  "battery": 92,
  "temperature": 30.8,
  "humidity": 50.5
}


Solution 2: Report data with device EUI and Name

Applicable: UG65/UG67(v60.0.0.44 and later), UG56 (v56.0.0.5 and later)


Enable the Metadata option under the Application page. After enabled, when a payload codec is selected to apply to a device, the gateway will add device EUI, device name and application ID to every report of this device when sending to MQTT/HTTP servers.

Note: this will take effects for all devices under this application. If you only require specific devices to report these information, please refer to solution 3 or 4.


Solution 3: Report raw data with specific attribute items.

Milesight gateways provide a LoRaObject function for expand usage. Here is an example decoder to report dev EUI, RSSI, SNR and raw data:

function Decode(fPort, bytes) {
var decoded = {};
decoded.devEUI = LoRaObject.devEUI;
decoded.rssi = LoRaObject.rxInfo[0].rssi;
decoded.snr = LoRaObject.rxInfo[0].loRaSNR;
decoded.data = LoRaObject.data;
return decoded;
}


Uplink Result:

{
"devEUI": "24e1611234567890",
"rssi": -5,
"snr": 11,
"data": "AXVkA2cgAQRoeg=="
}


After adding, you can add this payload codec to a device and go to Network Server > Packets to check the uplink result on Packet Details.



Solution 4: Report decoded data with specific attribute items.

Follow the solution 1 to paste the decoder to Payload decoder function box, then add attribute items before return decoded statement. Example:

function Decode(fPort, bytes) {
    var decoded = {};
  
//Data decoder 
    for (var i = 0; i < bytes.length;) {
        var channel_id = bytes[i++];
        var channel_type = bytes[i++];

        // BATTERY
        if (channel_id === 0x01 && channel_type === 0x75) {
            decoded.battery = bytes[i];
            i += 1;
        }
        // TEMPERATURE
        else if (channel_id === 0x03 && channel_type === 0x67) {
            decoded.temperature = readInt16LE(bytes.slice(i, i + 2)) / 10;
            i += 2;
        }
        // HUMIDITY
        else if (channel_id === 0x04 && channel_type === 0x68) {
            decoded.humidity = bytes[i] / 2;
            i += 1;
        } else {
            break;
        }
    }
  
decoded.devEUI = LoRaObject.devEUI;
    return decoded;
}

/* ******************************************
 * bytes to number
 ********************************************/
function readUInt16LE(bytes) {
    var value = (bytes[1] << 8) + bytes[0];
    return value & 0xffff;
}

function readInt16LE(bytes) {
    var ref = readUInt16LE(bytes);
    return ref > 0x7fff ? ref - 0x10000 : ref;
}


Uplink Result:

{
 "battery": 92,
 "temperature": 30.8,
 "humidity": 50.5,
 "devEUI": "24e1611234567890"
}

Note: If you need to add all contents of the LoRaObject , please add decoded.obj= LoRaObject; before return decoded statement.


After adding, you can add this payload codec to a device and go to Network Server > Packets to check the uplink result on Packet Details.



Note: the time variable in LoRaObject function means the time to receive packet after packet forwarder program starts. If you need to get the correct time for the gateway to receive the packet, please add this statement:

//replace the en-US as other time zone
decoded.time = new Date().toLocaleString('en-US', { timeZone: ' ' });  




Object Mapping Function

Applicable: UG65/UG67(v60.0.0.45 and later), UG56 (v56.0.0.5 and later)


Milesight gateways support to work as BACnet server or Modbus server to achieve the conversation between LoRaWAN and BACnet/Modbus. And it is necessary to add the object mapping functions to map the uplink data or downlink control items as the objects which can be recognized by BACnet server or Modbus server. 

Here is the object mapping example:

{
    "object": [
        {
            "id": "ipso_version",
            "name": "IPSO Version",
            "value": "",
            "unit": "",
            "access_mode": "R",
            "data_type": "TEXT",
            "value_type": "STRING",
            "max_length": 6,
            "bacnet_type": "character_string_value_object",
            "bacnet_unit_type_id": 95,
            "bacnet_unit_type": "UNITS_NO_UNITS"
        },
                {
            "id": "temperature_unit",
            "name": "Temperature Unit",
            "value": "",
            "unit": "",
            "access_mode": "RW",
            "data_type": "ENUM",
            "value_type": "UINT8",
            "values": [
                { "value": 0, "name": "celsius" },
                { "value": 1, "name": "fahrenheit" }
            ],
            "bacnet_type": "multistate_value_object",
            "bacnet_unit_type_id": 95,
            "bacnet_unit_type": "UNITS_NO_UNITS",
            "reference": ["temperature_control_mode", "temperature_target"]
        }
        ]
}

Parameter

Description

id

This value must be the same as the variable names of decoders and encoders. 

name

Leave blank or customize any content as required.

value

Unused. Leave blank.

unit

Leave blank or type the unit as required.

access_mode

Set the access mode of this variable. Supported options and corresponding Modbus register types:

Option

Description

Supported Modbus Register Type

R

Read-only

Discrete Input, Input Register

W

Write-only

Coil, Holding Register

RW

Read-write

Coil, Holding Register

 

data_type

Define the value type of this variable. Supported options:

Option

Description

Supported Modbus Register Type

TEXT

String type data, example: serial number

Input Register, Holding Register

NUMBER

Number type data including integer and float, example: temperature

Input Register, Holding Register

BOOL

The data which has only 0 and 1 status, example: button status

Discrete Input, Coil

ENUM

Multiple values

Discrete Input, Input Register, Coil, Holding Register

Note: if the data type is ENUM and the reference parameter is not blank, it is suggested to set Modbus register type as Input Register or Holding Register.

value_type

Supported options: UINT8, INT8, UINT16, INT16, UINT32, INT32, FLOAT, STRING.

values

Set the value range of this variable.

max_length

Whent the value type is STRING, set the maximum length of the strings or maximum length of Modbus registers.

bacnet_type

Supported options: 

analog_value_object, analog_input_object, analog_output_object, binary_value_object, binary_input_object, binary_output_object, multistate_value_object, multistate_input_object, multistate_output_object

bacnet_unit_type_id

Type the BACnet unit ID which can be found here.

bacnet_unit_type

Type the BACnet unit type which can be found here (see Description).

reference

If this variable should be written together with other variables, please add the variables array here.


After adding, you can try to add BACnet objects or Modbus objects to check if this object mapping function takes effect.


Add Payload Codec to Device

When you add any device on Network Server > Device page, you can select the decoder and the decoded data will be shown on the packet details of Network > Packets page.


---END---