Monitoring via MQTT

From Teltonika Networks Wiki
Main Page > General Information > Configuration Examples > Router control and monitoring > Monitoring via MQTT

Router monitoring via MQTT Linux guide applies to TRB14x devices and to RUT9xx, RUTXxx routers.

Introduction

MQTT (MQ Telemetry Transport or Message Queue Telemetry Transport) is an ISO standard (ISO/IEC PRF 20922) publish-subscribe-based messaging protocol. It works on top of the TCP/IP protocol. It is designed for connections with remote locations where a "small code footprint" is required or the network bandwidth is limited. The publish-subscribe messaging pattern requires a message broker.

This article provides a guide on how to configure and use a basic MQTT setup on TRB14x devices and on RUT9xx, RUTXxx routers.

How MQTT works

In general an MQTT connection takes place between two Clients and a Broker. A TRB14x devices and RUT routers can be Broker, Client or both. The MQTT Publisher(Client) present in TRB14x devices and RUT routers subscribes to two topics by default: <TYPE>/get and get/<SERIAL>/command. Where <TYPE> is device type, for RUT routers type is router and for TRB14x devices type is device. Parameter <SERIAL> is the router's serial number. When a third party client connects to the Broker, it sends the message id to the the topic <TYPE>/get. The publisher then sends a response containing its serial number to the topic <TYPE>/id. Now that the Client knows the router's or device's serial number it can ask for values of various parameters by sending requests to the topic <TYPE>/<SERIAL>/parameter_name. The MQTT Publisher can send responses containing values of these system parameters, if device supports that parameter:

Parameter name Parameter description Supported devices
temperature Temperature of the module in 0.1 degrees Celsius RUT9xx and RUTX11
operator Current operator’s name RUT9xx, RUTX11 and TRB14x
signal Signal strength in dBm RUT9xx, RUTX11 and TRB14x
network Network state RUT9xx, RUTX11 and TRB14x
connection Current connection type (2G, 3G, 4G) RUT9xx, RUTX11 and TRB14x
wan WAN IP address RUT9xx, RUTX11 and TRB14x
uptime System uptime in seconds RUT9xx, RUTXxx and TRB14x
name Device's device code RUT9xx, RUTXxx and TRB14x
digital1 Value of digital input no. 1 RUT9xx
digital2 Value of digital input no. 2 RUT9xx
analog Value of analog RUT9xx

After the client sends a message containing of these parameters, the Publisher will send a response message containing the value of the requested parameter to the topic <TYPE>/<SERIAL>/parameter_name, where parameter_name is the name of the requested parameter. The scheme below shows example of how to get parameters on RUT routers.


Configuration examples mqtt scheme v2.jpg

It should also be noted that, according to the MQTT protocol, topic names are case-sensitive. For instance, topic router is not the same as topic RoUtEr.

Configuring the device

This section will provide an explanation on how to configure an MQTT Broker and MQTT Publisher on a TRB14x devices and on RUT routers.

Broker


Basic (not counting Security, Bridge and Miscellaneous Broker settings) Broker configuration only contains three fields. Make sure to Enable the Broker and specify a Port for connection to the Broker. Enable Remote Access if you wish to connect to your router remotely (via WAN). Although in this case your router would need to have a Public IP address. In image below is presented how to configure RUT9xx routers:

Configuration examples mqtt broker.png


Configuration for a TRB14x devices and a RUTXxx routers looks like this:

Configuration examples publisher new design.png

Publisher


Publisher configuration is similar in its simplicity to the Broker configuration. Make sure to Enable the Publisher and specify a Port for connection to a Broker. If you plan on using the router's Broker, specify the same port as the one in the Broker Settings section. Hostname is the Broker's host name or IP address. If you're using the router's Broker, specify your router's LAN IP address. Username and Password are optional and depend on the type of security (if any) that the Broker uses. In image below is presented how configure RUT9xx routers:

Configuration examples mqtt publisher.png


Configuration for a TRB14x devices and a RUTXxx routers looks like this:

Configuration examples mqtt publisher new design.png.png

NOTE: If you configuring a TRB14x devices, it's default hostname is 192.168.2.1.


Configuring the PC

Once the Broker is up, you'll need to install Mosquitto and Mosquitto Clients on your PC. To do so, open the Linux Terminal app and enter this command:

$ sudo apt-get install mosquitto mosquitto-clients

NOTE: this software was chosen because it suits the needs of this example and is easily accessible to most users. It is not the only application compatible with RUT MQTT, so if you're using your own software for your solution, it is not mandatory to install this.

Subscribing and Publishing

Now you can use set up Brokers and Clients on your PC. In order to Publish commands and Subscribe to topics on a TRB14x devices and RUT routers, you'll need to know the device's Serial Number and TYPE. For TRB14x device's you'll need to use type : device and for RUT router's you'll need to use type: router. Router's serial number you can find in the Status → Device section of the router's WebUI. For TRB14x devices serial number you can find in the Status → System section of the device WebUI. Or you can subscribe to the topic <TYPE>/id and publish the message id to the topic <TYPE>/get. To do so, enter these commands in separate Terminal windows:

Subscribe:

$ mosquitto_sub -h 192.168.1.1 -p 1833 -u user -P pass -t <TYPE>/id

Publish:

$ mosquitto_pub -h 192.168.1.1 -p 1833 -u user -P pass -t <TYPE>/get -m id

In image below is presented example of how to get RUT router serial number:

File:Configuration examples mqtt router id v7.png


mosquitto_sub command is used to subscribe to a certain topic(s) and mosquitto_pub command is used to publish messages to specified topics. -m defines the message that you're publishing

-h indicates the host (the Broker's IP address) which, continuing from the example above, is 192.168.1.1. Replace this value with your router's LAN IP address or, if you're using MQTT remotely, your WAN IP address.

-p indicates the port used for connection to the Broker. Replace it with the port number that you specified in the Broker configuration.

-u and -P specify the username and password. If the Broker doesn't require authentication, these values aren't mandatory. Else replace them with the log in information of your Broker.

-t specifies the topics that you subscribe or publish to.

Now that we have the using device's serial number we can start publishing messages and receiving responses containing the device's system parameter values. Lets say we want to monitor the device's signal strength and up time values. In order to do that we'll need to subscribe to the topics router/<SERIAL>/signal and router/<SERIAL>/uptime using the mosquitto_sub command. Again, replace the values given in the commands below with the ones in your configuration:

Subscribe:

$ mosquitto_sub -h 192.168.1.1 -p 1833 -u user -P pass -t router/<SERIAL>/signal -t router/<SERIAL>/uptime

Publish:

$ mosquitto_pub -h 192.168.1.1 -p 1833 -u user -P pass -t router/get -m signal
$ mosquitto_pub -h 192.168.1.1 -p 1833 -u user -P pass -t router/get -m uptime

In example below you can see how to subscribe and publish to the topics signal strength and uptime on RUT routers:

Configuration examples mqtt subscribe publish v5.png