Difference between revisions of "Template:RutOS configuration example MQTT Gateway Demo LV"

From Teltonika Networks Wiki
 
(23 intermediate revisions by the same user not shown)
Line 4: Line 4:
  
 
==Configuration overview & prerequisites==
 
==Configuration overview & prerequisites==
*Two routers - '''RUT1''' as Modbus TCP Slave (Server) and '''RUT2''' as Modbus TCP Master (Client)
+
*Two routers - '''RUT1''' as Modbus TCP Server (Slave) and '''RUT2''' as Modbus TCP Client (Master)
 
*Flespi.io account to act as an MQTT Broker/Publisher/Subscriber (for first configuration example)
 
*Flespi.io account to act as an MQTT Broker/Publisher/Subscriber (for first configuration example)
 
*RUT241 with a Public IP address to act as MQTT Broker (for second configuration example)
 
*RUT241 with a Public IP address to act as MQTT Broker (for second configuration example)
Line 11: Line 11:
 
==Configuration using flespi.io as MQTT Broker==
 
==Configuration using flespi.io as MQTT Broker==
  
<Scheme>
+
[[File:Networking Topology MQTT MODBUS flespi configuration v2.png|border|class=tlt-border|1100x1100px]]
  
===Configuring Modbus TCP Server===
+
===Configuring Modbus TCP Server device===
 
----
 
----
 
Go to '''Services → Modbus → Modbus TCP Server''':
 
Go to '''Services → Modbus → Modbus TCP Server''':
Line 21: Line 21:
 
#Enter device ID
 
#Enter device ID
 
#Press [[File:Save & Apply.png|90px]]
 
#Press [[File:Save & Apply.png|90px]]
[[File:Networking MQTT MODBUS flespi configuration.png|border|class=tlt-border|1007x1007px]]
+
[[File:Networking MQTT MODBUS flespi configuration.png|border|class=tlt-border|1100x1100px]]
  
 
''*In this configuration LAN port is used hence “Allow Remote Access” is not needed''
 
''*In this configuration LAN port is used hence “Allow Remote Access” is not needed''
Line 27: Line 27:
 
''Modbus TCP Server is now configured''.
 
''Modbus TCP Server is now configured''.
  
===Configuring Modbus TCP Client===
+
===Configuring Modbus TCP Client device===
 
----
 
----
 
Go to '''Services → Modbus → Modbus TCP Client'''
 
Go to '''Services → Modbus → Modbus TCP Client'''
Line 66: Line 66:
 
#Press Save
 
#Press Save
  
[[File:Flespi mqtt left pane.png|border|class=tlt-border|400x400px]] [[File:Flespio mqtt board cogwheel.png|border|class=tlt-border|400x400px]] [[File:Flespi_generate_username_token.png|border|class=tlt-border|400x400px]] [[File:Flespi mqtt broker config.png|border|class=tlt-border|400x400px]]
+
[[File:Flespi mqtt left pane.png|border|class=tlt-border|410x410px]] [[File:Flespio mqtt board cogwheel.png|border|class=tlt-border|410x410px]] [[File:Flespi_generate_username_token.png|border|class=tlt-border|410x410px]] [[File:Flespi mqtt broker config.png|border|class=tlt-border|410x410px]]
 
 
  
 
=== Configuring MQTT Modbus Gateway on Modbus TCP Server device===
 
=== Configuring MQTT Modbus Gateway on Modbus TCP Server device===
Line 78: Line 77:
 
#Enter Username (Copied from flespi Connection settings generated token)
 
#Enter Username (Copied from flespi Connection settings generated token)
 
#Enter Password
 
#Enter Password
 
+
#Press [[File:Save & Apply.png|90px]] to save the configuration
 
[[File:MQTT modbus gateway config.png|border|class=tlt-border|1100px]]
 
[[File:MQTT modbus gateway config.png|border|class=tlt-border|1100px]]
  
Line 120: Line 119:
 
If function is 16 - from '''1''' to '''123''' (first register + registry value can not go out of range), Registry values separated by commas without spaces. Example.: '''1,2,3,654,21,789'''. There has to be as many values, as specified number of registers, and each value must be between '''0''' and '''65535'''. If number of registries is 0, there should be '''no registry values'''
 
If function is 16 - from '''1''' to '''123''' (first register + registry value can not go out of range), Registry values separated by commas without spaces. Example.: '''1,2,3,654,21,789'''. There has to be as many values, as specified number of registers, and each value must be between '''0''' and '''65535'''. If number of registries is 0, there should be '''no registry values'''
 
|}
 
|}
[[File:Networking MQTT modbus message format publisher v1.png|border|class=tlt-border]]
+
[[File:Networking MQTT modbus message format publisher v2.png|border|class=tlt-border]]
  
 
====Examples====
 
====Examples====
Line 136: Line 135:
 
Log in and navigate to MQTT Board on https://flespi.io
 
Log in and navigate to MQTT Board on https://flespi.io
  
Add a Subscriber:
+
'''Add a Subscriber:'''
  
#Press '''''<nowiki/>[[File:Flespi-plus-button.png]]''''' button on the top right corner, next to cogwheel [[File:Flespi cogwheel.png|20px]] icon
+
#Press '''''<nowiki/>[[File:Flespi-plus-button.png]]''''' button on the top right corner
 
#Select '''''<nowiki/>'Subscriber''''''
 
#Select '''''<nowiki/>'Subscriber''''''
 
# In the topic field enter '''''<nowiki/>'response''''''
 
# In the topic field enter '''''<nowiki/>'response''''''
Line 145: Line 144:
 
[[File:Flespi mqtt add subscriber.png|border|class=tlt-border|450px]]            [[File:Flespi-mqtt subscriber config.png|border|class=tlt-border|450px]]
 
[[File:Flespi mqtt add subscriber.png|border|class=tlt-border|450px]]            [[File:Flespi-mqtt subscriber config.png|border|class=tlt-border|450px]]
  
Add a Publisher:
+
'''Add a Publisher:'''
  
#Press '''''<nowiki/>'+'''''' button on the top right corner
+
#Press '''''<nowiki/>[[File:Flespi-plus-button.png]]''''' button on the top right corner
 
#Select '''''<nowiki/>'Publisher''''''
 
#Select '''''<nowiki/>'Publisher''''''
 
# In the topic field enter '''''<nowiki/>'request''''''
 
# In the topic field enter '''''<nowiki/>'request''''''
Line 153: Line 152:
 
#Press '''''<nowiki/>'Publish'''''' button
 
#Press '''''<nowiki/>'Publish'''''' button
  
[[File:flespi-mqtt_publisher_config.png|border|class=tlt-border|450x450px]] [[File:Flespi-mqtt publisher config v1.png|border|class=tlt-border|500x500px]]
+
[[File:flespi-mqtt_publisher_config.png|border|class=tlt-border|450x450px]] [[File:Flespi-mqtt publisher config v1.png|border|class=tlt-border|520x520px]]
  
  
Line 163: Line 162:
 
When the value climbs over 65535 the counter resets and the value held by the first register increases by '''1'''. So one way to interpret the results would be to multiply the value in the first register by '''2<sup>16</sup>''' and add it to the value of the second register. In this example: '''0 * 65536 + 31539 = 31539s''' or '''0 days 8 hours 45 minutes and 39 seconds.'''
 
When the value climbs over 65535 the counter resets and the value held by the first register increases by '''1'''. So one way to interpret the results would be to multiply the value in the first register by '''2<sup>16</sup>''' and add it to the value of the second register. In this example: '''0 * 65536 + 31539 = 31539s''' or '''0 days 8 hours 45 minutes and 39 seconds.'''
  
''This Means that MQTT Gateway on Modbus TCP Master router is working correctly and Modbus TCP Slave receives requests.''
+
''This Means that MQTT Gateway on Modbus TCP Client router is working correctly and Modbus TCP Server receives requests.''
  
==Configuration using RUT240 router as MQTT Broker==
+
==Configuration using RUT241 router as MQTT Broker==
  
<scheme>
+
[[File:Networking Topology MQTT broker configuration v1.png|border|class=tlt-border|1100x1100px]]
  
 
===Configuring Modbus TCP Master, MQTT Gateway MQTT Broker on RUT240, and MQTT Publisher/Subscriber on PC===
 
===Configuring Modbus TCP Master, MQTT Gateway MQTT Broker on RUT240, and MQTT Publisher/Subscriber on PC===
 
----
 
----
''The same configuration will be used for Modbus TCP Master and Slave {{{name}}} routers as in the previous example, only settings in Modbus TCP Master router {{{name}}} will be changed to match MQTT Broker on RUT240 router''
+
''The same configuration will be used for Modbus TCP Client and Server routers as in the previous example, only settings in Modbus TCP Client router will be changed to match MQTT Broker on RUT241 router''
  
 
Navigate to '''Services → Modbus  MQTT Gateway'''
 
Navigate to '''Services → Modbus  MQTT Gateway'''
Line 183: Line 182:
 
[[File:Networking MQTT Modbus MQTT gateway config v1.png|border|class=tlt-border|1090x1090px]]
 
[[File:Networking MQTT Modbus MQTT gateway config v1.png|border|class=tlt-border|1090x1090px]]
  
===Configuring RUT240 MQTT Broker===
+
===Configuring RUT241 MQTT Broker===
 
----
 
----
  
Line 190: Line 189:
 
#Select '''Enable'''
 
#Select '''Enable'''
 
#Check '''Enable Remote Access'''
 
#Check '''Enable Remote Access'''
#Press '''Save & Apply'''
+
[[File:MQTT gateway broker settings p01.png|border|class=tlt-border|1100x1100px]]
 
+
#<li value="3"> Select "'''Miscellaneous'''" tab
[[File:Networking MQTT Modbus MQTT Broker RUTos config v1.png|border|class=tlt-border|1079x1079px]]
+
#Enable "'''Allow anonymous'''"
 +
[[File:MQTT gateway broker settings p02.png|border|class=tlt-border|1100x1100px]]
 +
#<li value="5">Press [[File:Save & Apply.png|90px]] to save the configuration
 +
[[File:MQTT gateway broker settings p03.png|border|class=tlt-border|1100x1100px]]
  
 
===Testing MQTT Gateway===
 
===Testing MQTT Gateway===

Latest revision as of 11:47, 28 August 2024

Summary

In this guide, Modbus TCP master MQTT Gateway function will be configured with two different types of MQTT Brokers. First using third-party MQTT Broker services (in this example Flespi.io). Second, using the RUT240 router as MQTT Broker. Two routers will be used as Modbus TCP Master and Slave, and the PC acts as MQTT Publisher and Subscriber.

Configuration overview & prerequisites

  • Two routers - RUT1 as Modbus TCP Server (Slave) and RUT2 as Modbus TCP Client (Master)
  • Flespi.io account to act as an MQTT Broker/Publisher/Subscriber (for first configuration example)
  • RUT241 with a Public IP address to act as MQTT Broker (for second configuration example)
  • An end device (PC, Laptop) to act as MQTT Subscriber/Publisher (for second configuration example)

Configuration using flespi.io as MQTT Broker

Networking Topology MQTT MODBUS flespi configuration v2.png

Configuring Modbus TCP Server device


Go to Services → Modbus → Modbus TCP Server:

  1. Check Enable
  2. Enter port that will be used, in this example 502 will be used
  3. Enter device ID
  4. Press Save & Apply.png

Networking MQTT MODBUS flespi configuration.png

*In this configuration LAN port is used hence “Allow Remote Access” is not needed

Modbus TCP Server is now configured.

Configuring Modbus TCP Client device


Go to Services → Modbus → Modbus TCP Client

  1. Press Add Button.png

MQTT gateway modbus client configuration v1.png

  1. Check Enable
  2. Enter the name for the Server device
  3. Slave ID must match with the previously configured Server device ID
  4. Enter the IP address of the Modbus TCP Server device
  5. Chose same Port as in Server device - 502
  6. Enter Period in seconds, how often requests will be sent to the Server device

MQTT gateway modbus client configuration.png

  1. Enter new request name and press Add Button.png in Requests configurations

MQTT gateway modbus client configuration v2.png

  1. Choose Data type and Function, enter First register/coil/input to be read or written and Register count. You can name each individual configuration, and then select enable on configurations that you want to use. In this example, register to get Routers name is used.
  2. Press the Test button in Request Configuration Testing to see if the Slave device responds to requests, a response similar to the image below should be shown.

MQTT gateway modbus client configuration v3.png MQTT gateway modbus client configuration v4.png

  1. Press Save & Apply.png to save the configuration

List of available Modbus parameters can be found here

Configuring Flespi.io MQTT Broker


Log in or create an account on https://flespi.io Flespi login.png

Once logged in:

  1. Navigate to MQTT Board on the left side of the screen and press it.
  2. On the right-hand panel, top right corner, next to the name of the MQTT board, press the cogwheel-looking Flespi cogwheel.png icon to open Connection Settings
  3. In the opened window, press "Get flespi token" to generate a username
  4. Enter Client name
  5. Copy Host address
  6. Copy Username
  7. Create a password
  8. Press Save

Flespi mqtt left pane.png Flespio mqtt board cogwheel.png Flespi generate username token.png Flespi mqtt broker config.png

Configuring MQTT Modbus Gateway on Modbus TCP Server device


Open routers WebUI and navigate to Services → Modbus → MQTT Modbus Gateway

  1. Select Enable
  2. Enter Host (copied from flespi connection settings without 'wss://' and port)
  3. Select QoS (Quality of Service) level
  4. Enter Username (Copied from flespi Connection settings generated token)
  5. Enter Password
  6. Press Save & Apply.png to save the configuration

MQTT modbus gateway config.png

You can change Request and Response topics that you will have to publish and subscribe to get information from Modbus TCP Master through MQTT Gateway, but for this example, they are left on default topics

Message format for MQTT publisher


The format is in the text - heavier and slower, but less difficult to edit.

1. Format version 0
2. Cookie from 0 to 264 -1
3. IP Type 0 - IPv4; 1 - IPv6; 2 - hostname
4. IP IPv6 must be in full format (for example: 2001:0db8:0000:0000:0000:8a2e:0370:7334)
5. Port Port number (for example: 502)
6. Timeout in seconds (time to wait for response) from 1 to 999
7. Slave ID - Indicates to which slave request is sent from 1 to 255
8. Function 3 - read registers; 6 - write single register; 16 - write multiple registers
9. Number of the first register from which information will be read or written from 1 to 65535
10. Registry value If function is 3 - from 1 to 123 (first register + registry value can not go out of range);

If function is 6 - from 0 to 65535

If function is 16 - from 1 to 123 (first register + registry value can not go out of range), Registry values separated by commas without spaces. Example.: 1,2,3,654,21,789. There has to be as many values, as specified number of registers, and each value must be between 0 and 65535. If number of registries is 0, there should be no registry values

Networking MQTT modbus message format publisher v2.png

Examples


Setting relay (on) (Relay address is 202, which means 'Number of first register will be 203) 0 65432 0 192.168.1.1 502 5 1 6 203 1
Getting uptime 0 65432 0 192.168.1.1 502 5 1 3 2 2

Testing MQTT Publisher and Subscriber on flespi.io


Log in and navigate to MQTT Board on https://flespi.io

Add a Subscriber:

  1. Press Flespi-plus-button.png button on the top right corner
  2. Select 'Subscriber'
  3. In the topic field enter 'response'
  4. Press 'Subscribe' button

Flespi mqtt add subscriber.png Flespi-mqtt subscriber config.png

Add a Publisher:

  1. Press Flespi-plus-button.png button on the top right corner
  2. Select 'Publisher'
  3. In the topic field enter 'request'
  4. In the message field enter message, for this example 'Getting uptime' is used
  5. Press 'Publish' button

Flespi-mqtt publisher config.png Flespi-mqtt publisher config v1.png


Check the response in the 'Subscriber' tab, you should receive a message similar to the one below.


Flespi-mqtt response.png

When the value climbs over 65535 the counter resets and the value held by the first register increases by 1. So one way to interpret the results would be to multiply the value in the first register by 216 and add it to the value of the second register. In this example: 0 * 65536 + 31539 = 31539s or 0 days 8 hours 45 minutes and 39 seconds.

This Means that MQTT Gateway on Modbus TCP Client router is working correctly and Modbus TCP Server receives requests.

Configuration using RUT241 router as MQTT Broker

Networking Topology MQTT broker configuration v1.png

Configuring Modbus TCP Master, MQTT Gateway MQTT Broker on RUT240, and MQTT Publisher/Subscriber on PC


The same configuration will be used for Modbus TCP Client and Server routers as in the previous example, only settings in Modbus TCP Client router will be changed to match MQTT Broker on RUT241 router

Navigate to Services → Modbus MQTT Gateway

  1. Enable
  2. Host: Enter Public IP address of MQTT Broker (RUT240)
  3. Username: N/A
  4. Password: N/A
  5. Press Save & Apply

Networking MQTT Modbus MQTT gateway config v1.png

Configuring RUT241 MQTT Broker


Navigate to Services → MQTT → Broker

  1. Select Enable
  2. Check Enable Remote Access

MQTT gateway broker settings p01.png

  1. Select "Miscellaneous" tab
  2. Enable "Allow anonymous"

MQTT gateway broker settings p02.png

  1. Press Save & Apply.png to save the configuration

MQTT gateway broker settings p03.png

Testing MQTT Gateway


For testing purposes, two terminal windows will be used on the same PC.

To get Ubuntu terminal on Windows 10/11, refer to the following link for instructions: https://tutorials.ubuntu.com/tutorial/tutorial-ubuntu-on-windows#0

  • Open first terminal, which will act as Subscriber. Type in:

mosquito_sub -h [Host_address] -p [Port] -t [Topic]

Networking MQTT Modbus Subscriber in terminal v1.png

  • Open second terminal, which will act as Publisher. Type in:

mosquito_pub -h [Host] -h [Host_address] -p [Port] -m [‘Message’] -t [Topic]

Networking MQTT Modbus Publisher testing v1.png

  • On the first window - Subscriber, a response should appear

Networking MQTT Modbus Subscriber response ubuntu v1.png

See Also