Introduction

When Milesight gateway work as embedded network server, it supports to send 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 with firmware version 60.0.0.41 and later

For other models or versions, please refer to How to Use Payload Codec on Milesight Gateway(old).


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 decoder package locally.

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.


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:



Uplink Content Customization

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.

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

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


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.


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


Uplink 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.


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.



---END---