Jump to content

Template:RutOS configuration example MQTT Gateway: Difference between revisions

no edit summary
No edit summary
 
Line 1: Line 1:
__TOC__
__TOC__
==Summary==
==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 {{{name}}} routers will be used as Modbus TCP Master and Slave, and the PC acts as MQTT Publisher and Subscriber.
This guide will demonstrate how to configure the MQTT Gateway on a Modbus TCP Client using two different types of MQTT Brokers. The first configuration involves using a third-party MQTT Broker service "'''''Flespi.io'''''". The second setup uses the RUT241 router as the MQTT Broker. In this scenario, two routers will serve as the Modbus TCP Client and Server, while a PC will act as both the MQTT Publisher and Subscriber.


==Configuration overview & prerequisites==
==Configuration overview & prerequisites==
*Two {{{name}}} routers - one acts as Modbus TCP Master, another as Modbus TCP Slave
*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)
*RUT240 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)
*An end device (PC, Laptop) to act as MQTT Subscriber/Publisher (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==
==Configuration using flespi.io as MQTT Broker==
[[File:mqtt_broker_configuration_scheme_1.png|center|class=tlt-border|800x800px]]


===Configuring {{{name}}} Modbus TCP Slave===
[[File:Networking Topology MQTT MODBUS flespi configuration v2.png|border|class=tlt-border|1100x1100px]]
 
===Configuring Modbus TCP Server===
----
----
Go to '''Services → Modbus → Modbus TCP Slave''':
Open RUT1 router's WebUI and navigate to '''Services → Modbus → Modbus TCP Server'''


#Check '''Enable'''
#Check '''Enable'''
#Enter port that will be used, in this example 502 will be used
#Enter port that will be used, in this example 502 will be used
#Enter device ID
#Enter device ID
#Press '''Save & Apply'''
#Press [[File:Save & Apply.png|90px]]
[[File:Networking_MQTT_MODBUS_flespi_configuration_slave1_v1.png|border|class=tlt-border|1044x1044px]]
[[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''


''Modbus TCP Slave {{{name}}} is now configured''.
''Modbus TCP Server is now configured''.


===Configuring {{{name}}} Modbus TCP Master===
===Configuring Modbus TCP Client===
----
----
Go to '''Services → Modbus → Modbus TCP Master'''
Open RUT2 router's WebUI and navigate to '''Services → Modbus → Modbus TCP Client'''


#Press '''Add'''
#Press [[File:Add Button.png|50px]]
# Check '''Enable'''
[[File:MQTT gateway modbus client configuration v1.png|border|class=tlt-border|1100px]]
# Enter the name for the slave device
#<li value="2"> Check '''Enable'''
# Slave ID must match with the previously configured Slave device ID
#Enter the name for the Server device
#Enter the IP address of the Modbus TCP Slave device
#Server ID must match with the previously configured Modbus TCP Server device ID
#Chose same Port as in Slave device - 502
#Enter the IP address of the Modbus TCP Server device
#Enter Period in seconds, how often requests will be sent to the Slave device
#Chose same Port as in Server device - 502
#Enter new request name and press '''Add''' in Requests configurations
#Enter Period in seconds, how often requests will be sent to the Server device
#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.
[[File:MQTT gateway modbus client configuration.png|border|class=tlt-border|1100px]]
#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.
#<li value="8">Enter new request name and press [[File:Add Button.png|50px]] in Requests configurations
#Press Save & Apply
[[File:MQTT gateway modbus client configuration v2.png|border|class=tlt-border|1100px]]
''List of available Modbus parameters can be found [https://wiki.teltonika-networks.com/view/{{{name}}}_Monitoring_via_Modbus#Get_Parameters here]''
#<li value="9">Choose "Data type" - '''ASCII''', "Function" - '''Read holding resgiter (3)''', enter "First register number" - '''72''' and "Register count/Values" - '''3''' 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.
#Press the "'''Test'''" button in Request Configuration Testing to see if the Server device responds to requests, a response similar to the image below should be shown.
[[File:MQTT gateway modbus client configuration v3.png|border|class=tlt-border|1100px]]
[[File:MQTT gateway modbus client configuration v4.png|border|class=tlt-border|1100px]]
#<li value="11">Press [[File:Save & Apply.png|90px]] to save the configuration
''List of available Modbus parameters can be found [[https://wiki.teltonika-networks.com/view/RUTX14_Modbus#Modbus_Registers here]]''


[[File:Networking_MQTT_MODBUS_flespi_configuration_master_add_v1.png|alt=|border|class=tlt-border|1007x1007px]]
===Configuring Flespi.io MQTT Broker===


[[File:Networking MQTT MODBUS flespi configuration master afteradd step 234567 v2.png|border|class=tlt-border|1006x1006px]]
[[File:Networking MQTT MODBUS flespi configuration master req testing v3.png|border|class=tlt-border|1007x1007px]]
===Configuring Flespi.io MQTT Broker===
----
----
#Log in or create an account on https://flespi.io
Log in or create an account on https://flespi.io
 
[[File:Flespi login.png|border|class=tlt-border|1100px]]
[[File:Networking MQTT Modbus flespiio login.png|border|class=tlt-border|842x842px]]


Once logged in:
Once logged in:


#Navigate to MQTT Board on the left side of the screen and press it.                                          
#Navigate to MQTT Board on the left side of the screen and press it.
#On the right-hand panel, top right corner, next to the name of the MQTT board, press the cogwheel-looking icon to open ''Connection Settings''
#On the right-hand panel, top right corner, next to the name of the MQTT board, press the cogwheel-looking [[File:Flespi cogwheel.png|20px]] icon to open ''Connection Settings''
#In the opened window, press "Get flespi token" to generate a username
#In the opened window, press "Get flespi token" to generate a username
#Enter Client name
#Enter Client name
Line 65: Line 65:
#Create a password
#Create a password
#Press Save
#Press Save
[[File:Networking MQTT modbus fespi Connection settings v1.png|border|class=tlt-border|544x544px]] [[File:Networking MQTT Modbus flespi board.png|border|class=tlt-border|544x544px]] 


=== Configuring MQTT Gateway on {{{name}}} Modbus TCP Master===
[[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===
----
----
Open routers WebUI and navigate to '''Services → Modbus → MQTT Gateway'''
Open RUT2 router's WebUI and navigate to '''Services → Modbus → MQTT Modbus Gateway'''


#Select Enable
#Select Enable
#Enter Host (copied from flespi connection settings without 'wss://' and port)
#Enter Host (copied from flespi connection settings without 'wss://' and port)
#Select QoS (Quality of Service) level
#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:Networking MQTT Modbus Rut configuration Mqtt gateway v1.png|border|class=tlt-border|1058x1058px]]
''You can change Request and Response topics that you will have to publish and subscribe to get information from Modbus TCP Client through MQTT Gateway, but for this example, they are left on default topics''


''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===
 
===Message format for MQTT publisher===
----
----
The format is in the text - heavier and slower, but less difficult to edit.
The format is in the text - heavier and slower, but less difficult to edit.
Line 102: Line 104:
|from '''1''' to '''999'''
|from '''1''' to '''999'''
|-
|-
|7. Slave ID - Indicates to which slave request is sent
|7. Server ID - Indicates to which Server request is sent
|from '''1''' to '''255'''
|from '''1''' to '''255'''
|-
|-
Line 117: 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|1100px]]


====Examples====
====Examples====
Line 133: 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/>'+'''''' button on the top right corner
#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''''''
#Press '''''<nowiki/>'Subscribe'''''' button
#Press '''''<nowiki/>'Subscribe'''''' button


[[File:Networking MQTT Modbus flespi subscriber config1 v1.png|border|class=tlt-border]]            [[File:Networking MQTT Modbus flespi subscriber config2 v1.png|border|class=tlt-border]]
[[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 150: Line 152:
#Press '''''<nowiki/>'Publish'''''' button
#Press '''''<nowiki/>'Publish'''''' button


[[File:Networking MQTT Modbus flespi publisher config1 v1.png|border|class=tlt-border|434x434px]]           [[File:Networking MQTT Modbus flespi publisher config2 v1.png|border|class=tlt-border|365x365px]]
[[File:flespi-mqtt_publisher_config.png|border|class=tlt-border|450x450px]] [[File:Flespi-mqtt publisher config v1.png|border|class=tlt-border|520x520px]]




Line 156: Line 158:




[[File:Networking MQTT Modbus flespi Response v2.png|border|class=tlt-border|931x931px]]
[[File:flespi-mqtt_response.png|border|class=tlt-border|931x931px]]


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: '''9 * 65536 + 3502 = 593326s''' or '''6 days 20 hours 48 minutes and 46 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==


[[File:{{{name}}}_mqtt_broker_configuration_scheme_2.png|987x987px]]
[[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===
''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''
 
===Configuring MQTT Modbus Gateway settings===
----
----
''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''
Open RUT2 router's WebUI and navigate to '''Services → MQTT Modbus Gateway'''
 
Navigate to '''Services → Modbus MQTT Gateway'''


#'''Enable'''
#'''Enable'''
# Host: '''Enter Public IP address of MQTT Broker (RUT240)'''
# Host: '''Enter Public IP address of the new MQTT Broker (RUT241)'''
#Username: '''N/A'''
#Username: '''N/A'''
#Password: '''N/A'''
#Password: '''N/A'''
#Press '''Save & Apply'''
#Press [[File:Save & Apply.png|90px]]


[[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|1100x1100px]]


===Configuring RUT240 MQTT Broker===
===Configuring MQTT Broker settings on RUT241 device===
----
----


Navigate to '''Services → MQTT → Broker'''
Open RUT241 router's WebUI and navigate to '''Services → MQTT → Broker'''


#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"> Scroll down to "'''Broker settings'''" and select "'''Miscellaneous'''" tab
#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:Networking MQTT Modbus MQTT Broker RUTos config v1.png|border|class=tlt-border|1079x1079px]]
===Configuring MQTT Publisher/Subscriber on PC & testing MQTT Gateway===


===Testing MQTT Gateway===
----
----


Line 212: Line 217:
==See Also==
==See Also==
*[[Modbus Master RutOS configuration example]]
*[[Modbus Master RutOS configuration example]]
*[[{{{name}}} Monitoring via Modbus#Get Parameters]]
*[[Monitoring via Modbus]]