Modbus TCP Master MQTT Gateway: Difference between revisions
m (Added Configuration overview and prerequisities) |
mNo edit summary |
||
(14 intermediate revisions by 2 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 | 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 another router as MQTT Broker. Two RUT955 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 RUT955 routers - one acts as Modbus TCP Master, another as Modbus TCP Slave | ||
*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) | ||
* | *RUT 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| | [[File:Networking Topology MQTT MODBUS flespi configuration v1.png|center|class=tlt-border|800x800px]] | ||
===Configuring RUT955 Modbus TCP Slave=== | ===Configuring RUT955 Modbus TCP Slave=== | ||
---- | ---- | ||
Go to '''Services | Go to '''Services → Modbus → Modbus TCP Slave''': | ||
#Check '''Enable''' | #Check '''Enable''' | ||
Line 28: | Line 28: | ||
===Configuring RUT955 Modbus TCP Master=== | ===Configuring RUT955 Modbus TCP Master=== | ||
---- | ---- | ||
Go to '''Services | Go to '''Services → Modbus → Modbus TCP Master''' | ||
#Press '''Add''' | #Press '''Add''' | ||
# Check '''Enable''' | # Check '''Enable''' | ||
# Enter name for slave device | # Enter the name for the slave device | ||
# Slave ID must match with previously configured Slave device ID | # Slave ID must match with the previously configured Slave device ID | ||
#Enter IP address of Modbus TCP Slave device | #Enter the IP address of the Modbus TCP Slave device | ||
#Chose same Port as in Slave device - 502 | #Chose same Port as in Slave device - 502 | ||
#Enter Period in seconds, how often requests will be sent to Slave device | #Enter Period in seconds, how often requests will be sent to the Slave device | ||
#Enter new request name and press '''Add''' in Requests configurations | #Enter new request name and press '''Add''' in Requests configurations | ||
#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 | #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. | ||
#Press Test button in Request Configuration Testing to see if Slave device | #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. | ||
#Press Save & Apply | #Press Save & Apply | ||
[ | ''List of available Modbus parameters can be found [https://wiki.teltonika-networks.com/view/RUT955_Monitoring_via_Modbus#Get_Parameters here]'' | ||
=== Configuring Flespi.io MQTT Broker=== | [[File:Networking_MQTT_MODBUS_flespi_configuration_master_add_v1.png|alt=|border|class=tlt-border|1007x1007px]] | ||
[[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 | |||
[[File:Networking MQTT Modbus flespiio login.png|842x842px]] | [[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 icon to open ''Connection Settings'' | ||
#In the opened window, press "Get flespi token" to generate | #In the opened window, press "Get flespi token" to generate a username | ||
#Enter Client name | #Enter Client name | ||
#Copy Host address | #Copy Host address | ||
# Copy Username | #Copy Username | ||
#Create a password | #Create a password | ||
#Press Save | #Press Save | ||
[[File:Networking MQTT modbus fespi Connection settings v1.png|544x544px]] | [[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 RUT955 Modbus TCP Master=== | === Configuring MQTT Gateway on RUT955 Modbus TCP Master=== | ||
---- | ---- | ||
Open routers WebUI and navigate to Services | Open routers WebUI and navigate to '''Services → Modbus → MQTT 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) | #Enter Username (Copied from flespi Connection settings generated token) | ||
#Enter Password | #Enter Password | ||
Line 72: | Line 78: | ||
[[File:Networking MQTT Modbus Rut configuration Mqtt gateway v1.png|border|class=tlt-border|1058x1058px]] | [[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 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'' | ||
===Message format for MQTT publisher === | ===Message format for MQTT publisher=== | ||
---- | ---- | ||
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 102: | Line 108: | ||
|'''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''' | ||
|- | |- | ||
Line 111: | Line 117: | ||
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 v1.png|border|class=tlt-border]] | ||
====Examples==== | ====Examples==== | ||
Line 125: | Line 131: | ||
===Testing MQTT Publisher and Subscriber on flespi.io=== | ===Testing MQTT Publisher and Subscriber on flespi.io=== | ||
---- | ---- | ||
Log in and navigate to MQTT Board on https://flespi.io | |||
Add a | Add a Subscriber: | ||
#Press '''''<nowiki/>'+'''''' button on the top right corner | #Press '''''<nowiki/>'+'''''' 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: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]] | ||
Add a Publisher: | Add a Publisher: | ||
Line 140: | Line 146: | ||
#Press '''''<nowiki/>'+'''''' button on the top right corner | #Press '''''<nowiki/>'+'''''' 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 '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: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]] | ||
Check the response in the '''''<nowiki/>'Subscriber'''''' tab, you should receive a message similar to the one below. | Check the response in the '''''<nowiki/>'Subscriber'''''' tab, you should receive a message similar to the one below. | ||
[[File:Networking MQTT Modbus flespi Response | |||
[[File:Networking MQTT Modbus flespi Response v2.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.''' | |||
''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 Master router is working correctly and Modbus TCP Slave receives requests.'' | ||
==Configuration using | ==Configuration using a router as MQTT Broker== | ||
[[File:Networking Topology MQTT MODBUS RUTbroker configuration v1.png|987x987px]] | [[File:Networking Topology MQTT MODBUS RUTbroker configuration v1.png|987x987px]] | ||
===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 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'' | ||
Navigate to '''Services | Navigate to '''Services → Modbus MQTT Gateway''' | ||
#'''Enable''' | #'''Enable''' | ||
#Host: '''Enter Public IP address of MQTT Broker (RUT240)''' | # Host: '''Enter Public IP address of MQTT Broker (RUT240)''' | ||
#Username: '''N/A''' | #Username: '''N/A''' | ||
#Password: '''N/A''' | #Password: '''N/A''' | ||
Line 171: | Line 180: | ||
[[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 | ===Configuring RUT MQTT Broker=== | ||
---- | ---- | ||
Navigate to '''Services | Navigate to '''Services → MQTT → Broker''' | ||
#Select '''Enable''' | #Select '''Enable''' | ||
Line 191: | Line 200: | ||
*Open first terminal, which will act as '''Subscriber'''. Type in: | *Open first terminal, which will act as '''Subscriber'''. Type in: | ||
<blockquote>'' | <blockquote>''mosquitto_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>'' | <blockquote>''mosquitto_pub -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]] | ||
==See Also== | |||
*[[Modbus Master RutOS configuration example]] | |||
*[[RUT955 Monitoring via Modbus#Get Parameters]] |
Latest revision as of 13:33, 3 October 2022
Pages with broken file links > Modbus TCP Master MQTT GatewaySummary
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 another router as MQTT Broker. Two RUT955 routers will be used as Modbus TCP Master and Slave, and the PC acts as MQTT Publisher and Subscriber.
Configuration overview & prerequisites
- Two RUT955 routers - one acts as Modbus TCP Master, another as Modbus TCP Slave
- Flespi.io account to act as an MQTT Broker/Publisher/Subscriber (for first configuration example)
- RUT 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 RUT955 Modbus TCP Slave
Go to Services → Modbus → Modbus TCP Slave:
- Check Enable
- Enter port that will be used, in this example 502 will be used
- Enter device ID
- Press Save & Apply
*In this configuration LAN port is used hence “Allow Remote Access” is not needed
Modbus TCP Slave RUT955 is now configured.
Configuring RUT955 Modbus TCP Master
Go to Services → Modbus → Modbus TCP Master
- Press Add
- Check Enable
- Enter the name for the slave device
- Slave ID must match with the previously configured Slave device ID
- Enter the IP address of the Modbus TCP Slave device
- Chose same Port as in Slave device - 502
- Enter Period in seconds, how often requests will be sent to the Slave device
- Enter new request name and press Add in Requests configurations
- 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.
- 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.
- Press Save & Apply
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:
- 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
- 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 MQTT Gateway on RUT955 Modbus TCP Master
Open routers WebUI and navigate to Services → Modbus → MQTT Gateway
- Select Enable
- Enter Host (copied from flespi connection settings without 'wss://' and port)
- Enter Username (Copied from flespi Connection settings generated token)
- Enter Password
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:
- Press '+' button on the top right corner
- Select 'Subscriber'
- In the topic field enter 'response'
- Press 'Subscribe' button
Add a Publisher:
- Press '+' button on the top right corner
- Select 'Publisher'
- In the topic field enter 'request'
- In the message field enter message, for this example 'Getting uptime' is used
- 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: 9 * 65536 + 3502 = 593326s or 6 days 20 hours 48 minutes and 46 seconds.
This Means that MQTT Gateway on Modbus TCP Master router is working correctly and Modbus TCP Slave receives requests.
Configuration using a router as MQTT Broker
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 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
Navigate to Services → Modbus MQTT Gateway
- Enable
- Host: Enter Public IP address of MQTT Broker (RUT240)
- Username: N/A
- Password: N/A
- Press Save & Apply
Configuring RUT MQTT Broker
Navigate to Services → MQTT → Broker
- Select Enable
- Check Enable Remote Access
- Press Save & Apply
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:
mosquitto_sub -h [Host_address] -p [Port] -t [Topic]
- Open second terminal, which will act as Publisher. Type in:
mosquitto_pub -h [Host_address] -p [Port] -m [‘Message’] -t [Topic]
- On the first window - Subscriber, a response should appear