Monitoring via MQTT: Difference between revisions

From Teltonika Networks Wiki
No edit summary
No edit summary
 
(87 intermediate revisions by 11 users not shown)
Line 1: Line 1:
==Summary==
Router monitoring via MQTT Linux guide applies to TRB gateways and to RUT 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 chapter is a guide on how to configure a basic MQTT setup on RUT routers.
'''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 TRB gateways and on RUT routers.


==How MQTT works==
==How MQTT works==
 
In general an MQTT connection takes place between two Clients and a Broker. A TRB device and RUT/TCR router can be Broker, Client or both. The MQTT Publisher(Client) present in TRB devices and RUT/TCR routers subscribes to two topics by default: '''<TYPE>/get''' and '''get/<SERIAL>/command'''. Where '''<TYPE>''' is the device type, for RUT/TCR routers type is '''router''' and for TRB 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 the system parameters if the device supports that parameter:
First lets look over how MQTT works on RUT routers. An MQTT connection takes place between two Clients and a Broker. A RUT router can be Broker, a Client or both. The MQTT Publisher (Client) present in RUT routers subscribes to two topics by default: '''router/get''' and '''get/<SERIAL>/command''', where '''<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 '''router/get'''. The publisher then sends a response containing its serial number to the topic '''router/id'''. Now that the Client knows the router's serial number it can ask for values of various parameters by sending requests to the topic '''router/<SERIAL>/parameter_name'''. The MQTT Publisher can send responses containing values of these system parameters:


{| class="wikitable"
{| class="wikitable"
|+
|+
! style="width: 250px; background: black; color: white;"  | Parameter name
! style="width: 250px; background: black; color: white;"  | Parameter name
! style="width: 500px; background: black; color: white;"  | Parameter description
! style="width: 400px; background: black; color: white;"  | Parameter description
! style="width: 300; background: black; color: white;"  | Supported devices
 
|-
! style="text-align: left; vertical-align: top;" | id
| style="text-align: left; vertical-align: top;" | Modem IMEI
| style="text-align: left; vertical-align: top;" | RUT2, RUT9, RUTX, RUT3, RUT1, TRB2, TRB5, OTD, RUT1, RUTM, RUTC
|-
|-
! style="text-align: left; vertical-align: top;" | temperature
! style="text-align: left; vertical-align: top;" | temperature
| style="text-align: left; vertical-align: top;" | Temperature of the module in 0.1 degrees Celsius
| style="text-align: left; vertical-align: top;" | Temperature of the module in 0.1 degrees Celsius
| style="text-align: left; vertical-align: top;" | RUT2, RUT9, RUTX, RUT3, RUT1, TRB2, TRB5, OTD, RUT1, RUTM, RUTC
|-
|-
! style="text-align: left; vertical-align: top;" | operator
! style="text-align: left; vertical-align: top;" | operator
| style="text-align: left; vertical-align: top;" | Current operator’s name
| style="text-align: left; vertical-align: top;" | Current operator’s name
| style="text-align: left; vertical-align: top;" | RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUTM, RUTC
|-
|-
! style="text-align: left; vertical-align: top;" | signal
! style="text-align: left; vertical-align: top;" | signal
| style="text-align: left; vertical-align: top;" | Signal strength in dBm
| style="text-align: left; vertical-align: top;" | Signal strength in dBm
| style="text-align: left; vertical-align: top;" | RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUTM, RUTC
|-
|-
! style="text-align: left; vertical-align: top;" | network
! style="text-align: left; vertical-align: top;" | network
| style="text-align: left; vertical-align: top;" | Current network type (2G, 3G, 4G)
| style="text-align: left; vertical-align: top;" | Network state
| style="text-align: left; vertical-align: top;" | RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUTM, RUTC
|-
|-
! style="text-align: left; vertical-align: top;" | connection
! style="text-align: left; vertical-align: top;" | connection
| style="text-align: left; vertical-align: top;" | Data connection status
| style="text-align: left; vertical-align: top;" | Current connection type (2G, 3G, 4G)
| style="text-align: left; vertical-align: top;" | RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUTM, RUTC
|-
|-
! style="text-align: left; vertical-align: top;" | wan
! style="text-align: left; vertical-align: top;" | wan
| style="text-align: left; vertical-align: top;" | WAN IP address
| style="text-align: left; vertical-align: top;" | WAN IP address
| style="text-align: left; vertical-align: top;" | RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUT1, RUTM, RUTC
|-
|-
! style="text-align: left; vertical-align: top;" | uptime
! style="text-align: left; vertical-align: top;" | uptime
| style="text-align: left; vertical-align: top;" | System uptime in seconds
| style="text-align: left; vertical-align: top;" | System uptime in seconds
| style="text-align: left; vertical-align: top;" | RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUT1, RUTM, RUTC
|-
|-
! style="text-align: left; vertical-align: top;" | name
! style="text-align: left; vertical-align: top;" | name
| style="text-align: left; vertical-align: top;" | Router’s name
| style="text-align: left; vertical-align: top;" | Device's device code
| style="text-align: left; vertical-align: top;" | RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUT1, RUTM, RUTC
|-
|-
! style="text-align: left; vertical-align: top;" | digital1
! style="text-align: left; vertical-align: top;" | digital1
| style="text-align: left; vertical-align: top;" | Value of digital input no. 1
| style="text-align: left; vertical-align: top;" | Value of digital input no. 1
| style="text-align: left; vertical-align: top;" | RUT9
|-
|-
! style="text-align: left; vertical-align: top;" | digital2
! style="text-align: left; vertical-align: top;" | digital2
| style="text-align: left; vertical-align: top;" | Value of digital input no. 2
| style="text-align: left; vertical-align: top;" | Value of digital input no. 2
| style="text-align: left; vertical-align: top;" | RUT9
|-
|-
! style="text-align: left; vertical-align: top;" | analog
! style="text-align: left; vertical-align: top;" | analog
| style="text-align: left; vertical-align: top;" | Value of analog
| style="text-align: left; vertical-align: top;" | Value of analog
| style="text-align: left; vertical-align: top;" | RUT9, TRB2, TRB141
|-
! style="text-align: left; vertical-align: top;" | pin2
| style="text-align: left; vertical-align: top;" | Value of 2's pin state
| style="text-align: left; vertical-align: top;" | TRB2
|-
! style="text-align: left; vertical-align: top;" | pin3
| style="text-align: left; vertical-align: top;" | Value of 3's pin state
| style="text-align: left; vertical-align: top;" | RUT1, RUT2, RUT9, RUTX, RUT3, TRB1, TRB2, TRB5, RUTM
|-
! style="text-align: left; vertical-align: top;" | pin4
| style="text-align: left; vertical-align: top;" | Value of 4's pin state
| style="text-align: left; vertical-align: top;" | RUT1, RUT2, RUT9, RUTX, RUT3, TRB1, TRB2, TRB5, RUTM
|-
|-
|}
|}


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 '''router/<SERIAL>/parameter_name''', where '''parameter_name''' is the name of the requested parameter.  
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.


[[File:Configuration examples mqtt scheme v1.png]]


It should also be noted that, according to the MQTT protocol, topic names are case-sensitive, for example topic '''router''' is not the same as topic '''RoUtEr'''.
[[File:Configuration examples mqtt scheme v2.jpg|border|class=tlt-border]]


==Configuring the router==
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'''''.


This section will provide an explanation on how to configure an MQTT Broker and MQTT Publisher on a RUT router.
==Configuring the device==
 
This section will provide an explanation on how to configure an MQTT Broker and MQTT Publisher on a TRB gateways and on RUT routers.


===Broker===
===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 to have a Public IP address.  
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 routers:
   
   
[[Image:Configuration examples mqtt broker.png]]
[[File:Monitoring_via_mqtt_enable.png|border|class=tlt-border|500x167]]
 
 
If you want to access the broker without password or ACL file, then you need to enable "Allow anonymous" option in Broker settings -> Miscellaneous section:
 
[[File:Monitoring_via_mqtt_broker_misc_anon.png|border|class=tlt-border|500x167]]


===Publisher===
===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 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.
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 TRB gateways and RUT routers:
 
[[File:Monitoring_via_mqtt_publisher_general.png|border|class=tlt-border]]
 
 
<b>NOTE</b>: If you are configuring a TRB device, 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
 
<b>NOTE</b>: 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 TRB series devices and RUT/TCR routers, you'll need to know the device's '''Serial Number''' and '''TYPE'''. For TRB series device's you'll need to use type : '''device''' and for RUT/TCR 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 TRB series 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 new 1.png|border|class=tlt-border]]
 
 
'''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/TCR routers:
 
[[File:Configuration examples mqtt subscribe publish new 2.png|1100px|border|class=tlt-border]]
 
==External links==
https://mosquitto.org/ - Eclipse Mosquitto official website


[[Image:Configuration examples mqtt publisher.png]]
[[Category:Router control and monitoring]]

Latest revision as of 13:15, 25 July 2024

Main Page > General Information > Configuration Examples > Router control and monitoring > Monitoring via MQTT

Router monitoring via MQTT Linux guide applies to TRB gateways and to RUT 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 TRB gateways and on RUT routers.

How MQTT works

In general an MQTT connection takes place between two Clients and a Broker. A TRB device and RUT/TCR router can be Broker, Client or both. The MQTT Publisher(Client) present in TRB devices and RUT/TCR routers subscribes to two topics by default: <TYPE>/get and get/<SERIAL>/command. Where <TYPE> is the device type, for RUT/TCR routers type is router and for TRB 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 the system parameters if the device supports that parameter:

Parameter name Parameter description Supported devices
id Modem IMEI RUT2, RUT9, RUTX, RUT3, RUT1, TRB2, TRB5, OTD, RUT1, RUTM, RUTC
temperature Temperature of the module in 0.1 degrees Celsius RUT2, RUT9, RUTX, RUT3, RUT1, TRB2, TRB5, OTD, RUT1, RUTM, RUTC
operator Current operator’s name RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUTM, RUTC
signal Signal strength in dBm RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUTM, RUTC
network Network state RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUTM, RUTC
connection Current connection type (2G, 3G, 4G) RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUTM, RUTC
wan WAN IP address RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUT1, RUTM, RUTC
uptime System uptime in seconds RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUT1, RUTM, RUTC
name Device's device code RUT2, RUT9, RUTX, RUT3, RUT1, TRB1, TRB2, TRB5, OTD, RUT1, RUTM, RUTC
digital1 Value of digital input no. 1 RUT9
digital2 Value of digital input no. 2 RUT9
analog Value of analog RUT9, TRB2, TRB141
pin2 Value of 2's pin state TRB2
pin3 Value of 3's pin state RUT1, RUT2, RUT9, RUTX, RUT3, TRB1, TRB2, TRB5, RUTM
pin4 Value of 4's pin state RUT1, RUT2, RUT9, RUTX, RUT3, TRB1, TRB2, TRB5, RUTM

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.


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 TRB gateways 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 routers:

500x167


If you want to access the broker without password or ACL file, then you need to enable "Allow anonymous" option in Broker settings -> Miscellaneous section:

500x167

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 TRB gateways and RUT routers:


NOTE: If you are configuring a TRB device, 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 TRB series devices and RUT/TCR routers, you'll need to know the device's Serial Number and TYPE. For TRB series device's you'll need to use type : device and for RUT/TCR 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 TRB series 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:


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/TCR routers:

External links

https://mosquitto.org/ - Eclipse Mosquitto official website