Introduction

This article will guide you how to add decoders and custom uplink contents when using Milesight gateway built-in network server.


 

Requirement

  • Milesight LoRaWAN Gateway: UG8x(firmware version 80.0.0.64 and later), UG65/UG67 (firmware version 60.0.0.40 and before), UG56, UG63

For UG65/UG67 with firmware version 60.0.0.41 and later, please refer to How to Use Payload Codec on Milesight Gateway.


Configuration

Login Milesight Gateway’s web interface, create an application in Network Server->Applications page.

Select the Payload Codec type as Custom and type the Javascript code to custom the uplink contents.

Note: This payload codec is only applied for the devices under this application. If you need to use other decoders, please create a new application.

Take EM300-TH as example, here are 3 situations for uplink content customization.


1. Only upload decoded sensor data.


Copy EM300-TH 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.


Result:

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


2. Upload specific attribute items and raw data.

Click here to know all attribute items in one package. Milesight gateways provide function LoRaOject to call every item you require. If you need to send dev EUI, RSSI, SNR and raw data, edit this example coder:

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;
}


Result:

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


3. Upload specific attribute items and decoded data.

Copy EM300-TH decoder and paste it to Payload decoder function box, then add attribute items before return decoded statement:

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;
}


Result:

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

Note: If you need to add all attribute items, add decoded.obj= LoRaObject; before return decoded statement.