Table of Contents:

1.  Introduction

Dragino LoRa/LoRaWAN gateway support MQTT forwarding. It can forward the sensor data from LoRa network to MQTT server , and vice verse.

1.1  Support Devices

This MQTT forward instruction is for below devices:

  • Firmware Version > LG02_LG08-5.3.1580178039 Firmware Download
  • LG01N, OLG01N (Warning: LG01-P LG01-S use another instruction: MQTT for LG01-P/LG01S)
  • LG02, OLG02
  • LG308, DLOS8
  • LPS8
  • LIG16
  • MS14 series if installed with the same firmware. (in this case, the MQTT forward will work , but no LoRa support)

2.  Firmware Change Log for MQTT feature

This instruction is wrote start from LG02_LG08-5.3.1580178039. Below is related change log since this version of firmware.

  • LG02_LG08-5.3.1580178039
    • Initiate version

3.  MQTT forward operating principle

3.1  Network Structure

Below shows the network structure for MQTT forwarding.

  • For Uplink:      The sensor sends data to LoRa Gateway via LoRa wireless, The gateway will process these data and forward to remote MQTT Broker via Internet.
  • For Downlink: The gateway subscribe a topic in the MQTT broker, when there is update on the topic, the gateway will know and broadcast the data to Local LoRa network.


General MQTT structure

3.2  How sensor data is forwarded

In this MQTT forward feature, the key point is how the gateway process the sensor data.

3.2.1  Upstream

Assume there are two sensor nodes, their ID are Node1 ID: 6734 , Node2 ID: 7456. In the remote MQTT broker there are two topics: Topic1: /channel/765800, Topic2: /channel/367860. We can set up in the gateway to map Node1 to Topic1 and Node2 to Topic2. So when there is a sensor data from Node1, the gateway will forward the data to Topic1, when there is sensor data from Node2, the gateway will forward to Topic2.

The data flow works as below diagram.



Upstream path


Note: The sensor data can base or LoRa or other method, as long as there are data on the file /var/iot/channels. /span>



3.2.2  Downstream

The gateway subscribes to a topic of the remote MQTT broker topic. When there is some one publish a value on this topic. The gateway will get it and broadcast to local LoRa Network.

Below are the data flow for downstream.



Downstream path

3.3  Macro Definition

The MQTT publish command use Macro settings to generate flexible upstream payload for MQTT publish.

Currently the -t (topic) and -m (message) support Macros.


3.3.1  -t topic macro

  • CHANNEL: Remote Channel ID
  • CLIENTID: Client ID , Same as -i
  • WRITE_API: Remote Channel Write API
  • USERNAME: User ID (-u)
  • HOSTNAME: Device Hostname

3.3.2  -m message macro

  • HOSTNAME: Device Hostname
  • CHANNEL: Remote Channel ID
  • DATA: Sensor Data without time stamp and rssi
  • META: Completely sensor data with time stamp and rssi
  • JSON: Convert META to json format.

3.3.3  Example for Macro


MQTT Publish configure

Above screen shots shows below format:

  • -m: DATA

When there is a LoRa sensor arrive. it will be store at the /var/iot/channels as below:


Sensor Data

According to above macro. Gateway will publish field1=22.0&field2=49.0 to topic: dragino-1b7060/78901/data, where 78901 is the remote channel for this node ID.

3.4  Modify the MQTT to support more options

The MQTT Client Utility used in Dragino is mosquitto_pub and mosquitto_sub. User can add more options to the mqtt commands. User can check the valid options by command mosquitto_pub --help. as below:

root@dragino-1ec39c:~# mosquitto_pub --help
mosquitto_pub is a simple mqtt client that will publish a message on a single topic and exit.
mosquitto_pub version 1.6.4 running on libmosquitto 1.6.4.

Usage: mosquitto_pub {[-h host] [-p port] [-u username] [-P password] -t topic | -L URL}
                     {-f file | -l | -n | -m message}
                     [-c] [-k keepalive] [-q qos] [-r] [--repeat N] [--repeat-delay time]
                     [-A bind_address]
                     [-i id] [-I id_prefix]
                     [-d] [--quiet]
                     [-M max_inflight]
                     [-u username [-P password]]
                     [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]
                     [{--cafile file | --capath dir} [--cert file] [--key file]
                     [--ciphers ciphers] [--insecure]
                     [--tls-alpn protocol]
                     [--tls-engine engine] [--keyform keyform] [--tls-engine-kpass-sha1]]
                     [--psk hex-key --psk-identity identity [--ciphers ciphers]]
                     [--proxy socks-url]
                     [--property command identifier value]
                     [-D command identifier value]
       mosquitto_pub --help

-A : bind the outgoing socket to this host/ip address. Use to control which interface
      the client communicates over.
-d : enable debug messages.
-D : Define MQTT v5 properties. See the documentation for more details.
-f : send the contents of a file as the message.
-h : mqtt host to connect to. Defaults to localhost.
-i : id to use for this client. Defaults to mosquitto_pub_ appended with the process id.
-I : define the client id as id_prefix appended with the process id. Useful for when the
      broker is using the clientid_prefixes option.
-k : keep alive in seconds for this client. Defaults to 60.
-L : specify user, password, hostname, port and topic as a URL in the form:
-l : read messages from stdin, sending a separate message for each line.
-m : message payload to send.
-M : the maximum inflight messages for QoS 1/2..
-n : send a null (zero length) message.
-p : network port to connect to. Defaults to 1883 for plain MQTT and 8883 for MQTT over TLS.
-P : provide a password
-q : quality of service level to use for all messages. Defaults to 0.
-r : message should be retained.
-s : read message from stdin, sending the entire input as a message.
-t : mqtt topic to publish to.
-u : provide a username
-V : specify the version of the MQTT protocol to use when connecting.
      Can be mqttv5, mqttv311 or mqttv31. Defaults to mqttv311.
--help : display this message.
--repeat : if publish mode is -f, -m, or -s, then repeat the publish N times.
--repeat-delay : if using --repeat, wait time seconds between publishes. Defaults to 0.
--quiet : don't print error messages.
--will-payload : payload for the client Will, which is sent by the broker in case of
                  unexpected disconnection. If not given and will-topic is set, a zero
                  length message will be sent.
--will-qos : QoS level for the client Will.
--will-retain : if given, make the client Will retained.
--will-topic : the topic on which to publish the client Will.
--cafile : path to a file containing trusted CA certificates to enable encrypted
--capath : path to a directory containing trusted CA certificates to enable encrypted
--cert : client certificate for authentication, if required by server.
--key : client private key for authentication, if required by server.
--keyform : keyfile type, can be either "pem" or "engine".
--ciphers : openssl compatible list of TLS ciphers to support.
--tls-version : TLS protocol version, can be one of tlsv1.3 tlsv1.2 or tlsv1.1.
                 Defaults to tlsv1.2 if available.
--insecure : do not check that the server certificate hostname matches the remote
              hostname. Using this option means that you cannot be sure that the
              remote host is the server you wish to connect to and so is insecure.
              Do not use this option in a production environment.
--tls-engine : If set, enables the use of a TLS engine device.
--tls-engine-kpass-sha1 : SHA1 of the key password to be used with the selected SSL engine.
--psk : pre-shared-key in hexadecimal (no leading 0x) to enable TLS-PSK mode.
--psk-identity : client identity string for TLS-PSK mode.
--proxy : SOCKS5 proxy URL of the form:
           Only "none" and "username" authentication is supported.

See for more information.

and modify the /usr/bin/ script, the location to change the command option is below lines:

# Call MQTT Publish command

# 1. Case with User, Password and Client ID present  (e.g. Azure)
if [ ! -z "$pass" ] && [ ! -z "$user" ] && [ ! -z "$clientID" ]; then
mosquitto_pub $D -h $server -p $port -q $pub_qos -i $clientID -t $pub_topic -u $user -P "$pass" $C $cafile $PUB_FLAG "$mqtt_data"

# 2. Case with Certificate, Key and ClientID present (e.g. AWS)
elif [ ! -z "$certfile" ] && [ ! -z "$key" ] && [ ! -z "$clientID" ]; then
mosquitto_pub $D -h $server -p $port -q $pub_qos -i $clientID -t $pub_topic --cert $cert --key $key $C $cafile $PUB_FLAG "$mqtt_data"

# 3. Case with no User, Certificate or ClientID present
elif [ -z "$user" ] && [ -z "$certfile" ] && [ -z "$clientID" ]; then
mosquitto_pub $D -h $server -p $port -q $pub_qos -t $pub_topic $PUB_FLAG "$mqtt_data"

# 4. Case with no User, Certificate, but with ClientID present
elif [ -z "$user" ] && [ -z "$certfile" ] && [ ! -z "$clientID" ]; then
mosquitto_pub $D -h $server -p $port -q $pub_qos -i $clientID -t $pub_topic $PUB_FLAG "$mqtt_data"

# 5. Case with User and ClientID present, but no Password and no Certificate present
elif [ -z "$pass" ] && [ -z "$certfile" ] && [ ! -z "$user" ] && [ ! -z "$clientID" ]; then
mosquitto_pub $D -h $server -p $port -q $pub_qos -i $clientID -t $pub_topic -u $user $PUB_FLAG "$mqtt_data"

# 6. Case with User and Password present, but no ClientID and no Certificate present
elif [ ! -z "$user" ] && [ ! -z "$pass" ] && [ -z "$clientID" ] && [ -z "$certfile" ]; then
mosquitto_pub $D -h $server -p $port -q $pub_qos  -t $pub_topic -u $user -P "$pass" $PUB_FLAG "$mqtt_data"

# 0. Else - invalid parameters, just log
case="Invalid parameters" 
logger "[IoT.MQTT]:Invalid Parameters - mosquitto_pub not called."

4.  Example to communicate to a simple MQTT server

4.1  Overview

This section is an example to show how to set up LG01-N to communicate with a MQTT server. The MQTT server is a simple utility set up in a local PC. Note: User can set up same server via this instruction.

4.2  Simulate via MQTT.fx utility

The MQTT.fx is a MQTT client tool. We can use this to simulate a MQTT connection to our MQTT broker first to make sure the MQTT broker works. This will also help us understand how it works.

In this test, the MQTT broker and MQTT.fx are installed in the same PC, so the MQTT server address in MQTT.fx should be localhost. Below shows how to connect to the server.


Connect to MQTT Broker

After connected, use publish to public some thing to MQTT server. This to simulate upsteam


Upstream: Publish message to MQTT Broker

To simulate a downstream, use MQTT.fx to subscribe a topic, and publish something to this topic. as Below:


Downstream: Subscribe a topic to get downstream

4.3  Simulate via Dragino Command Line

For first try of MQTT connection, simulate via command line is recommend, there are many servers / connection type for MQTT. They are using different connection parameters. Simulating the connection via command line will help us rapidly connect to server and debug.

In the Dragino Gateway, we use mosquitto client for MQTT connection.

For Upstream

command is mosquitto_pub

Example: mosquitto_pub -h -p 1883 -t /channel/6543 -m temp=36

Note: is MQTT broker address, the gateway and the MQTT broker PC are in the same network.



For Downstream

Use mosquitto_sub to subscribe the change on the topic.



4.4  Configure Dragino UI for MQTT connection

This chapter are step by step to show to configure the Dragino Menu for MQTT auto connection.

Go to Dragino Menu --> MQTT Client


go to mqtt configure menu

Select Forward to MQTT serverNotice: This option is removed from the latest firmware, in the latest firmware, if user submit "SAVE & APPLY" in MQTT page, the gateway will use MQTT service.


forward to MQTT

4.4.1  Configure the MQTT Client for Upstream

Below screenshot is same as the publish command:

mosquitto_pub -h -p 1883 -i dragino-1b7060 -t CLIENTID/CHANNEL/data -m DATA
//where the CLIENTID, CHANNEL & DATA are macro. represent for
//CLIENTID: dragino-1b7060
//CHANNEL: Remote ID in Channel settings; here is 78901 or 567456
//DATA: The data stores in /var/iot/channels/


MQTT Publish configure


MQTT Channel settings

For example, if we put a data(temp=46) on the file /var/iot/channels/4567, because 4567 match the remote channel 78901. the gateway will run this command:

mosquitto_pub -h -p 1883 -i dragino-1b7060 -t dragino-1b7060/78901/data -m temp=46

to MQTT broker.

Below is a simulation to put this data to active the MQTT publish.


MQTT Publish

4.4.2  Configure the MQTT Client for Downstream

Below screen shot equal to this subscribe command:

mosquitto_sub -h -p 1883 -i dragino-1b7060 -t command. 


MQTT Subscribe

When MQTT broker receive a update on this topic, the gateway will get the update and use LoRa radio to broadcast this message. The LoRa parameters used for update is:


LoRa Broadcast parameters.

And below is the subscribe simulation:


downstream simulation

4.5  Add LoRa support to communicate with remote sensor

In above section, we have configured the UI to support MQTT upstream and downstream. We can simulate via Linux command. In this section, we will guide how to communicate with remote LoRa End Node for upstream and downstream.

4.5.1  Use LoRa Raw protocol for communication -- For LG01/LG02

We can use LoRa Shield to send LoRa Raw data to Gateway and receive data from gateway.

The example Sketch for LoRa Shield +Arduino is here: LoRa_Shield_Sketch_For_MQTT

And this link is the required library: arduino-LoRa-master. Unzip this library and put in Arduino library location.


What does the Arduino Sketch do? The Arduino Sketch will:

  • Upstream: Keep sending a LoRa Message every minutes with this payload : <4567>tem=xx&hum=yy (Where xx and yy are temperature and humidity value generated randomly).
  • Downstream: Listening broadcast message from gateway, and print it in console.
  • The LoRa parameter settings in Arduino should match the LoRa settings in gateway, as below:


LoRa Parameter should match

Below is the test result after the Arduino Sketch is running.


Upstream Data Flow


Downstream Data Flow

4.5.2  Use LoRaWAN Protocol for communication -- For LG308/LPS8/DLOS8

Since firmware LG02_LG08--build-v5.3.1585192026-20200326-1109, Dragino LoRaWAN gateways support the communication to LoRaWAN ABP end node locally without the need of LoRaWAN server. This feature allow us to integrate MQTT in the gateway to support LoRaWAN to MQTT forwarding or visa verse.

When use test this feature, please use the version higher then : LG02_LG08--build-v5.4.1593400722-20200629-1120, in this version, the upload format is changed and readable, which is easier for integration.

Video Instruction

Step 1: Refer Communicate with ABP End Node to know how to set up LG308 to work with LoRaWAN End node.

Step 2: Make sure your Radio settings match the End Node settings.


Use Same Frequency Band as End Node

Step 3: Set up publish format and MQTT channel. The LG308 will store the Data from End node in HEX format in the file. 


Step 4: Map the Device Address to Remote ID in MQTT server.


Step 5: Upstream: Save the change, we can see the log info via "sytem log", End Node and MQTT Server



Choose ASCII Format


LHT65 Decoder

Step 6: Set up subscribe: Subscribe a topci for downstream.


Step 7: Downstream: Save the change, we can see the log info via "sytem log", End Node and MQTT Server.


4.5.3  MQTT Downstream format

mosquitto_pub -h $server_address -p $server_port -t $Client_ID -m "dev_addr,imme/time,txt/hex,payload"

mosquitto_pub -h -p 1883 -t dragino-1d25dc/ -m "260211D,time,txt,hello"

mosquitto_pub -h $server_address -p $server_port -t $Client_ID -m "dev_addr,imme/time,txt/hex,payload,txpw,txbw,SF,frequency,rxwindow"

mosquitto_pub -h -p 1883 -t dragino-1d25dc/ -m "260211D,time,txt,hello,20,1,SF12,923300000,2"

Or use MQTT.fx


Notice: The text use for Downstream must meet the requirement from LG308 Downstream Payload

Check out this link for more mosquito-related directives

5.  How to Debug

User can login the gateway's console and run logread -f. It will shows the output when there is packet arrive.User can see if it is correct.

6.  How to ask for Support

If a user still not have trouble making it works. please send a mail to with the below info:

  • Detail of your LoRaWAN end node.
  • Gateway Model and firmware version
  • A set of screenshots you configure in the gateway according to our instruction
  • A full log of "logread -f"
Created by Xiaoling on 2022/05/12 15:36
Copyright ©2010-2022 Dragino Technology Co., LTD. All rights reserved
Dragino Wiki v2.0