Jump to content

Modbus TCP Master MQTT Gateway: Difference between revisions

From Teltonika Networks Wiki
No edit summary
 
(18 intermediate revisions by 4 users not shown)
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 RUT240 router as MQTT Broker. Two RUT955 routers will be used as Modbus TCP Master and Slave, and PC acts as MQTT Publisher and Subscriber.
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==
==Configuration overview & prerequisites==
*Two RUT955 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:Networking Topology MQTT MODBUS flespi configuration v1.png|center|frameless|800x800px]]


===Configuring RUT955 Modbus TCP Slave===
[[File:Networking Topology MQTT MODBUS flespi configuration v2.png|border|class=tlt-border|1000px]]
 
===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]]


''*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 RUT955 is now configured''.
''Modbus TCP Server is now configured''.


===Configuring RUT955 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]]
# Enter 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 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]]
#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]]
''List of available Modbus parameters can be found [https://wiki.teltonika-networks.com/view/RUT955_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 Slave 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]]
[[File:MQTT gateway modbus client configuration v4.png|border|class=tlt-border]]
#<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]]
----
Log in or create an account on https://flespi.io
[[File:Flespi login.png|border|class=tlt-border]]


[[File:Networking MQTT MODBUS flespi configuration master req testing v3.png|border|class=tlt-border|1007x1007px]]
Once logged in:
 
#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 [[File:Flespi cogwheel.png|20px]] icon to open ''Connection Settings''
#In the opened window, press "Get flespi token" to generate a username
#Enter Client name
#Copy Host address
#Copy Username
#Create a password
#Press Save


===Configuring Flespi.io MQTT Broker===
===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]]
[[File:Networking MQTT Modbus flespiio login.png|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 82:
#Create a password
#Create a password
#Press Save
#Press Save
[[File:Networking MQTT modbus fespi Connection settings v1.png|544x544px]] [[File:Networking MQTT Modbus flespi board.png|301x301px]] 


===Configuring MQTT Gateway on RUT955 Modbus TCP Master===
<gallery mode="packed" heights="410>
File:Flespi mqtt left pane.png
File:Flespio mqtt board cogwheel.png
File:Flespi_generate_username_token.png
File:Flespi mqtt broker config.png
</gallery>
 
=== 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)
# Enter Username (Copied from flespi Connection settings generated token)
#Select QoS (Quality of Service) level
#Enter Username (Copied from flespi Connection settings generated token)
#Enter Password
#Enter Password
 
#Press [[File:Save & Apply.png|90px]] to save the configuration
[[File:Networking MQTT Modbus Rut configuration Mqtt gateway v1.png|border|class=tlt-border|1058x1058px]]
[[File:MQTT modbus gateway config.png|border|class=tlt-border|]]


''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''
''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''
Line 84: Line 108:
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.
{| class="wikitable"
{| class="wikitable"
| 1.  Format version
|1.  Format version
|'''0'''
|'''0'''
|-
|-
Line 94: Line 118:
|-
|-
|4. IP
|4. IP
| IPv6 must be in full format (for example: '''2001:0db8:0000:0000:0000:8a2e:0370:7334''')
|IPv6 must be in full format (for example: '''2001:0db8:0000:0000:0000:8a2e:0370:7334''')
|-
|-
|5. Port
|5. Port
Line 100: Line 124:
|-
|-
|6. Timeout in seconds (time to wait for response)
|6. Timeout in seconds (time to wait for response)
| from '''1''' to '''999'''
|from '''1''' to '''999'''
|-
|-
|7. Slave ID - Indicates to which slave request is sent
|7. Slave ID - Indicates to which slave request is sent
Line 108: Line 132:
|'''3''' - read registers; '''6''' - write single register; '''16''' - write multiple registers
|'''3''' - read registers; '''6''' - write single register; '''16''' - write multiple registers
|-
|-
|9. Number of first register from which information will be read or written
|9. Number of the first register from which information will be read or written
| from '''1''' to '''65535'''
|from '''1''' to '''65535'''
|-
|-
|10. Registry value
|10. Registry value
Line 117: Line 141:
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]]
[[File:Networking MQTT modbus message format publisher v2.png|border|class=tlt-border]]


====Examples====
====Examples====
Line 129: Line 153:
|}
|}


=== Testing MQTT Publisher and Subscriber on flespi.io===
===Testing MQTT Publisher and Subscriber on flespi.io===
----
----
Login 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]]            [[File:Networking MQTT Modbus flespi subscriber config2 v1.png]]
[[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''''''
#In the message field enter message, for this example '''''<nowiki/>'Getting uptime'''''' is used
#In the message field enter message, for this example '''''<nowiki/>'Getting uptime'''''' is used
#Press '''''<nowiki/>'Publish'''''' button
#Press '''''<nowiki/>'Publish'''''' button


[[File:Networking MQTT Modbus flespi publisher config1 v1.png|434x434px]]           [[File:Networking MQTT Modbus flespi publisher config2 v1.png|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 180:




[[File:Networking MQTT Modbus flespi Response v2.png|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:Networking Topology MQTT MODBUS RUTbroker configuration v1.png|987x987px]]
[[File:Networking Topology MQTT broker configuration v1.png|border|class=tlt-border|1000px]]


===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 RUT955 routers as in the previous example, only settings in Modbus TCP Master router RUT955 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]]


===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]]
#<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]]
#<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 200: Line 227:
*Open first terminal, which will act as '''Subscriber'''. Type in:
*Open first terminal, which will act as '''Subscriber'''. Type in:


<blockquote>''mosquito_sub -h [Host_address] -p [Port] -t [Topic]''</blockquote>[[File:Networking MQTT Modbus Subscriber in terminal v1.png]]
<blockquote>''mosquito_sub -h [Host_address] -p [Port] -t [Topic]''</blockquote>[[File:Networking MQTT Modbus Subscriber in terminal v1.png|border|class=tlt-border]]


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


<blockquote>''mosquito_pub -h [Host] -h [Host_address] -p [Port] -m [‘Message’] -t [Topic]''</blockquote>[[File:Networking MQTT Modbus Publisher testing v1.png]]
<blockquote>''mosquito_pub -h [Host] -h [Host_address] -p [Port] -m [‘Message’] -t [Topic]''</blockquote>[[File:Networking MQTT Modbus Publisher testing v1.png|border|class=tlt-border]]


*On the first window - '''Subscriber''', a response should appear
*On the first window - '''Subscriber''', a response should appear


[[File:Networking MQTT Modbus Subscriber response ubuntu v1.png]]
[[File:Networking MQTT Modbus Subscriber response ubuntu v1.png|border|class=tlt-border]]
 
=== MQTT Modbus Gateway JSON format===
----
 
{| class="wikitable"
|+ JSON Request Structure
|1. Cookie
|64-bit integer. Used to match request and response. Range: '''0''' to '''2<sup>64</sup> -1'''
|-
|2. Type
|Request type: '''0''' - TCP; '''1''' - Serial; '''2''' - Connection Control
|-
|3. Connection
|(Type = 0 only) Connection index. Optional if '''host''' is given
|-
|4. Host
|(Type = 0 or 2) IPv4, IPv6, or FQDN address of server. Required if connection is not used or if type = 2 and action = '''open'''
|-
|5. Port
|(Type = 0 or 2) Server port. Optional for type = 0, required for type = 2 with action = '''open''' (for example: '''502''')
|-
|6. Timeout in seconds
|(Type = 0, 1, or 2) How long to wait for response. From '''1''' to '''999'''
|-
|7. Device ID
|(Type = 1 only) Serial device ID
|-
|8. Server ID
|(Type = 0 or 1) Modbus slave/server ID. From '''1''' to '''255'''
|-
|9. Function
|(Type = 0 or 1) Modbus function: '''3''' - read registers; '''6''' - write single register; '''16''' - write multiple registers
|-
|10. Register number
|First register address. From '''1''' to '''65535'''
|-
|11. Register count
|Number of registers to read. If function is 3 or 16: from '''1''' to '''123'''
|-
|12. Coil number
|First coil address (for coil operations only)
|-
|13. Coil count
|Number of coils to read (for coil operations only)
|-
|14. Value
|Single value to write (used with function '''6'''). Must be from '''0''' to '''65535'''
|-
|15. Values
|Array of values (used with function '''16'''). Comma-separated without spaces (e.g. '''1,2,3,654,21,789'''). Number of values must match '''register count'''. Each value: '''0'''–'''65535'''
|-
|16. Action
|(Type = 2 only) '''"open"''', '''"close"''', or '''"status"'''
|}
 
{| class="wikitable"
|+ JSON Response Structure
|-
|1. Cookie
|Copied from request to identify it
|-
|2. Success
|'''true''' if the request succeeded, '''false''' otherwise
|-
|3. Data
|If success: can be a '''boolean''' or '''array of integers'''
|-
|4. Error
|If success is '''false''': a string containing an error message
|}
 
===JSON Message Examples===
----
'''Read uptime from Modbus TCP Server (using TCP)'''
<pre>
Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 3,
  "register_number": 2,
  "register_count": 2
}
Response:
{
  "cookie": 65432,
  "success": true,
  "data": [0, 5590]
}
</pre>
 
'''Read uptime from Modbus TCP Server (using serial)'''
<pre>
Request:
{
  "cookie": 65432,
  "type": 1,
  "device_id": "my_serial_device",
  "timeout": 1,
  "server_id": 1,
  "function": 3,
  "register_number": 2,
  "register_count": 2
}
Response:
{
  "cookie": 65432,
  "success": true,
  "data": [0, 5590]
}
</pre>
 
'''Read uptime from Modbus TCP Server (using TCP connection)'''
<pre>
Request:
{
  "cookie": 65432,
  "type": 0,
  "connection": 1,
  "timeout": 1,
  "server_id": 1,
  "function": 3,
  "register_number": 2,
  "register_count": 2
}
Response:
{
  "cookie": 65432,
  "success": true,
  "data": [0, 5590]
}
</pre>
 
'''Read coils from server'''
<pre>
Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 1,
  "coil_number": 2,
  "coil_count": 2
}
Response:
{
  "cookie": 65432,
  "success": true,
  "data": [0, 1]
}
</pre>
 
'''Write multiple registers to server'''
<pre>
Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 16,
  "register_number": 2,
  "values": [1, 2, 3]
}
Response:
{
  "cookie": 65432,
  "success": true
}
</pre>
 
'''Write multiple coils to server'''
<pre>
Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 15,
  "coil_number": 2,
  "values": [1, 2, 3]
}
Response:
{
  "cookie": 65432,
  "success": true
}
</pre>
 
'''Write single register to server'''
<pre>
Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 16,
  "register_number": 2,
  "value": 1
}
Response:
{
  "cookie": 65432,
  "success": true
}
</pre>
 
'''Write single coil to server'''
<pre>
Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 16,
  "coil_number": 2,
  "value": 1
}
Response:
{
  "cookie": 65432,
  "success": true
}
</pre>
 
'''Open TCP connection'''
<pre>
Request:
{
  "cookie": 65432,
  "type": 2,
  "action": "open",
  "connection": 1,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1
}
Response:
{
  "cookie": 65432,
  "success": true
}
</pre>
 
'''Close TCP connection'''
<pre>
Request:
{
  "cookie": 65432,
  "type": 2,
  "action": "close",
  "connection": 1
}
Response:
{
  "cookie": 65432,
  "success": true
}
</pre>
 
'''Check TCP connection status'''
<pre>
Request:
{
  "cookie": 65432,
  "type": 2,
  "action": "status",
  "connection": 1
}
Response:
{
  "cookie": 65432,
  "success": true,
  "data": false
}
</pre>


==See Also ==
==See Also==
*[[Modbus Master RutOS configuration example]]
*[[Modbus Master RutOS configuration example]]
*[[RUT955 Monitoring via Modbus#Get Parameters]]
*[[Monitoring via Modbus]]

Latest revision as of 14:16, 22 May 2025

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

Configuring Modbus TCP Server


Open RUT1 router's WebUI and navigate 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

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

Modbus TCP Server is now configured.

Configuring Modbus TCP Client


Open RUT2 router's WebUI and navigate to Services → Modbus → Modbus TCP Client

  1. Press

  1. Check Enable
  2. Enter the name for the Server device
  3. Server ID must match with the previously configured Modbus TCP 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

  1. Enter new request name and press in Requests configurations

  1. 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.
  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.

  1. Press 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

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 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

Configuring Flespi.io MQTT Broker


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

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 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

Configuring MQTT Modbus Gateway


Open RUT2 router's 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 to save the configuration

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

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 button on the top right corner
  2. Select 'Subscriber'
  3. In the topic field enter 'response'
  4. Press 'Subscribe' button

Add a Publisher:

  1. Press 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


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


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

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


Open RUT2 router's WebUI and navigate to Services → MQTT Modbus Gateway

  1. Enable
  2. Host: Enter Public IP address of the new MQTT Broker (RUT241)
  3. Username: N/A
  4. Password: N/A
  5. Press

Configuring MQTT Broker settings on RUT241 device


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

  1. Select Enable
  2. Check Enable Remote Access

  1. Scroll down to "Broker settings" and select "Miscellaneous" tab
  2. Enable "Allow anonymous"

  1. Press to save the configuration

Configuring MQTT Publisher/Subscriber on PC & 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]

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

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

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

MQTT Modbus Gateway JSON format


JSON Request Structure
1. Cookie 64-bit integer. Used to match request and response. Range: 0 to 264 -1
2. Type Request type: 0 - TCP; 1 - Serial; 2 - Connection Control
3. Connection (Type = 0 only) Connection index. Optional if host is given
4. Host (Type = 0 or 2) IPv4, IPv6, or FQDN address of server. Required if connection is not used or if type = 2 and action = open
5. Port (Type = 0 or 2) Server port. Optional for type = 0, required for type = 2 with action = open (for example: 502)
6. Timeout in seconds (Type = 0, 1, or 2) How long to wait for response. From 1 to 999
7. Device ID (Type = 1 only) Serial device ID
8. Server ID (Type = 0 or 1) Modbus slave/server ID. From 1 to 255
9. Function (Type = 0 or 1) Modbus function: 3 - read registers; 6 - write single register; 16 - write multiple registers
10. Register number First register address. From 1 to 65535
11. Register count Number of registers to read. If function is 3 or 16: from 1 to 123
12. Coil number First coil address (for coil operations only)
13. Coil count Number of coils to read (for coil operations only)
14. Value Single value to write (used with function 6). Must be from 0 to 65535
15. Values Array of values (used with function 16). Comma-separated without spaces (e.g. 1,2,3,654,21,789). Number of values must match register count. Each value: 065535
16. Action (Type = 2 only) "open", "close", or "status"
JSON Response Structure
1. Cookie Copied from request to identify it
2. Success true if the request succeeded, false otherwise
3. Data If success: can be a boolean or array of integers
4. Error If success is false: a string containing an error message

JSON Message Examples


Read uptime from Modbus TCP Server (using TCP)

Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 3,
  "register_number": 2,
  "register_count": 2
}
Response:
{
  "cookie": 65432,
  "success": true,
  "data": [0, 5590]
}

Read uptime from Modbus TCP Server (using serial)

Request:
{
  "cookie": 65432,
  "type": 1,
  "device_id": "my_serial_device",
  "timeout": 1,
  "server_id": 1,
  "function": 3,
  "register_number": 2,
  "register_count": 2
}
Response:
{
  "cookie": 65432,
  "success": true,
  "data": [0, 5590]
}

Read uptime from Modbus TCP Server (using TCP connection)

Request:
{
  "cookie": 65432,
  "type": 0,
  "connection": 1,
  "timeout": 1,
  "server_id": 1,
  "function": 3,
  "register_number": 2,
  "register_count": 2
}
Response:
{
  "cookie": 65432,
  "success": true,
  "data": [0, 5590]
}

Read coils from server

Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 1,
  "coil_number": 2,
  "coil_count": 2
}
Response:
{
  "cookie": 65432,
  "success": true,
  "data": [0, 1]
}

Write multiple registers to server

Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 16,
  "register_number": 2,
  "values": [1, 2, 3]
}
Response:
{
  "cookie": 65432,
  "success": true
}

Write multiple coils to server

Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 15,
  "coil_number": 2,
  "values": [1, 2, 3]
}
Response:
{
  "cookie": 65432,
  "success": true
}

Write single register to server

Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 16,
  "register_number": 2,
  "value": 1
}
Response:
{
  "cookie": 65432,
  "success": true
}

Write single coil to server

Request:
{
  "cookie": 65432,
  "type": 0,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1,
  "server_id": 1,
  "function": 16,
  "coil_number": 2,
  "value": 1
}
Response:
{
  "cookie": 65432,
  "success": true
}

Open TCP connection

Request:
{
  "cookie": 65432,
  "type": 2,
  "action": "open",
  "connection": 1,
  "host": "192.168.1.1",
  "port": 502,
  "timeout": 1
}
Response:
{
  "cookie": 65432,
  "success": true
}

Close TCP connection

Request:
{
  "cookie": 65432,
  "type": 2,
  "action": "close",
  "connection": 1
}
Response:
{
  "cookie": 65432,
  "success": true
}

Check TCP connection status

Request:
{
  "cookie": 65432,
  "type": 2,
  "action": "status",
  "connection": 1
}
Response:
{
  "cookie": 65432,
  "success": true,
  "data": false
}

See Also