Difference between revisions of "Domnev"

From Teltonika Networks Wiki
Line 2: Line 2:
 
__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 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.
+
In this guide, the MQTT Serial Gateway function will be configured using third-party MQTT Broker services (in this example, ''Flespi.io'').
  
 
==Configuration overview & prerequisites==
 
==Configuration overview & prerequisites==
*Two RUT955 routers - one acts as Modbus TCP Master, another as Modbus TCP Slave
+
*Two devices with serials ports - one acts as Modbus RTU Master, another as Modbus RTU 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)
 
  
 
==Configuration using flespi.io as MQTT Broker==
 
==Configuration using flespi.io as MQTT Broker==
[[File:Networking Topology MQTT MODBUS flespi configuration v1.png|center|class=tlt-border|800x800px]]
+
[[File:MQTT Serial gateway topology.png|border|center|class=tlt-border|847x279px]]
  
 
===Configuring RUT955 MQTT Gateway===
 
===Configuring RUT955 MQTT Gateway===
Line 22: Line 20:
 
# '''Enter Password'''.
 
# '''Enter Password'''.
  
[[File:MQTT Gateway config.png|border|center|class=tlt-border|862x412px]]
+
[[File:MQTT Gateway config.png|border|center|class=tlt-border|862 × 412px]]
  
 
'''Note''': ''Everything else can be left as default or changed according to your needs.''
 
'''Note''': ''Everything else can be left as default or changed according to your needs.''
Line 29: Line 27:
 
----
 
----
 
Under the MQTT Gateway configuration, create the Serial Gateway:
 
Under the MQTT Gateway configuration, create the Serial Gateway:
# Enter the desired device ID;
+
# Enter the '''desired device ID''';
# Select the desired serial interface.
+
# Select the '''desired serial interface'''.
  
[[File:Serial gateway config.png|border|center|class=tlt-border|868x308px]]
+
[[File:Serial gateway config.png|border|center|class=tlt-border|868×308px]]
  
 
===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''';
  
 
#Navigate to '''MQTT Board''' on the '''left side''' menu;                                         
 
#Navigate to '''MQTT Board''' on the '''left side''' menu;                                         
#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 a username;
+
#In the opened window, press '''"Get flespi token"''' to generate a username;
#Enter the Client name;
+
#Enter the '''Client name''';
 
#Copy the Host address;
 
#Copy the Host address;
#Copy Username;
+
#Copy '''Username''';
#Create a password.
+
#Create a '''password'''.
  
Once done, save all the changes
+
Once done, save all the changes.
 
[[File:Flespi board.png|border|center|class=tlt-border|1102x729px]]
 
[[File:Flespi board.png|border|center|class=tlt-border|1102x729px]]
 
  
 
===Message format for MQTT publisher===
 
===Message format for MQTT publisher===
 
----
 
----
  <b>1 <COOKIE> <SERIAL_DEVICE_ID> <TIMEOUT> <SLAVE_ID> <MODBUS_FUNCTION> <FIRST_REGISTER> <REGISTER_COUNT/VALUES></b>
+
Modbus request data sent in the MQTT payload should be generated in accordance with the following format:
 +
 
 +
<pre>1 <COOKIE> <SERIAL_DEVICE_ID> <TIMEOUT> <SLAVE_ID> <MODBUS_FUNCTION> <FIRST_REGISTER> <REGISTER_COUNT/VALUES> </pre>
 +
 
 +
The table below explains what each option means:
  
The format is in the text - heavier and slower, but less difficult to edit.
 
 
{| class="wikitable"
 
{| class="wikitable"
 
|1.  Format version
 
|1.  Format version
Line 97: Line 96:
 
             <li><b>15</b> - <u>coil count</u> (in range [1..1968]); must not exceed the boundary (first coil number + coil count <= 65537); and <u>coil values</u> separated with commas, without spaces (e.g., <i>1,2,3,654,21,789</i>); there must be exactly as many values as specified (with coil count); each value must be in the range of [0..1].
 
             <li><b>15</b> - <u>coil count</u> (in range [1..1968]); must not exceed the boundary (first coil number + coil count <= 65537); and <u>coil values</u> separated with commas, without spaces (e.g., <i>1,2,3,654,21,789</i>); there must be exactly as many values as specified (with coil count); each value must be in the range of [0..1].
 
|}
 
|}
 
 
 
 
 
 
 
 
 
====Examples====
 
====Examples====
 
----
 
----
 
{| class="wikitable"
 
{| class="wikitable"
 
|Setting relay (on) (Relay address is 202, which means 'Number of first register will be 203)
 
|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'''
+
|'''1 1 1 1 1 6 203 1'''
 
|-
 
|-
|Getting uptime
+
|Getting temperature
|'''0 65432 0 192.168.1.1 502 5 1 3 2 2'''
+
|'''1 1 1 1 1 3 6 2'''
 
|}
 
|}
 
 
===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
+
====Adding Flespi Subscriber====
 
+
----
Add a Subscriber:
+
To test the Modbus Serial Gateway functionality, '''log into''' your '''Flespi account''' → '''MQTT Board''' and '''add a Subscriber''':
  
 
#Press '''''<nowiki/>'+'''''' button on the top right corner
 
#Press '''''<nowiki/>'+'''''' button on the top right corner
Line 124: Line 116:
 
#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 subscriber.png|border|class=tlt-border|496x204px]]           [[File:Flespi subscriber setup.png|border|class=tlt-border|496x205px]]
 +
====Adding Flespi Subscriber====
 +
----
  
Add a Publisher:
+
Also, you will need to '''add a Publisher''':
  
 
#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 '''''<nowiki/>'Getting uptime'''''' is used
+
#In the message field enter message, for this example '''''<nowiki/>'Getting temperature'''''' is used
 
#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 publisher.png|border|class=tlt-border|495x238px]]   [[File:Flespi publisher setup.png|border|class=tlt-border|494x239px]]
 
+
====Flespi Subscriber output====
 
+
----
 
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 v2.png|border|class=tlt-border|931x931px]]
+
[[File:Flespi response.png|border|center|class=tlt-border|500x305px]]
 
 
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.''
 
 
 
==Configuration using a router as MQTT Broker==
 
 
 
[[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===
 
----
 
''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'''
 
 
 
[[File:Networking MQTT Modbus MQTT gateway config v1.png|border|class=tlt-border|1090x1090px]]
 
 
 
===Configuring RUT MQTT Broker===
 
----
 
 
 
Navigate to '''Services → MQTT → Broker'''
 
 
 
#Select '''Enable'''
 
#Check '''Enable Remote Access'''
 
#Press '''Save & Apply'''
 
 
 
[[File:Networking MQTT Modbus MQTT Broker RUTos config v1.png|border|class=tlt-border|1079x1079px]]
 
 
 
===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:
 
 
 
<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:
 
 
 
<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
 
  
[[File:Networking MQTT Modbus Subscriber response ubuntu v1.png|border|class=tlt-border]]
+
In the output, we can see that router's '''temperature''' is '''44 degrees Celsius'''.
  
 
==See Also==
 
==See Also==
 
*[[Modbus Master RutOS configuration example]]
 
*[[Modbus Master RutOS configuration example]]
*[[RUT955 Monitoring via Modbus#Get Parameters]]
+
*[[RUT955_Modbus#MQTT_Gateway|MQTT Gateway]]
 +
==External links==
 +
[https://flespi.io/#/ Flespi.io]

Revision as of 15:34, 20 April 2023

The information on this page is updated in accordance with the 00.07.4 firmware version .

Summary

In this guide, the MQTT Serial Gateway function will be configured using third-party MQTT Broker services (in this example, Flespi.io).

Configuration overview & prerequisites

  • Two devices with serials ports - one acts as Modbus RTU Master, another as Modbus RTU Slave;
  • Flespi.io account to act as an MQTT Broker/Publisher/Subscriber (for first configuration example);

Configuration using flespi.io as MQTT Broker

Configuring RUT955 MQTT Gateway


Go to Services → Modbus → MQTT Gateway and there:

  1. Enable the instance;
  2. Enter Host (copied from flespi connection settings without 'wss://' and port);
  3. Enter Username (Copied from flespi Connection settings generated token);
  4. Enter Password.
862 × 412px

Note: Everything else can be left as default or changed according to your needs.

Configuring RUT955 Serial Gateway


Under the MQTT Gateway configuration, create the Serial Gateway:

  1. Enter the desired device ID;
  2. Select the desired serial interface.
868×308px

Configuring Flespi.io MQTT Broker


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

  1. Navigate to MQTT Board on the left side menu;
  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 the Client name;
  5. Copy the Host address;
  6. Copy Username;
  7. Create a password.

Once done, save all the changes.

Flespi board.png

Message format for MQTT publisher


Modbus request data sent in the MQTT payload should be generated in accordance with the following format:

1 <COOKIE> <SERIAL_DEVICE_ID> <TIMEOUT> <SLAVE_ID> <MODBUS_FUNCTION> <FIRST_REGISTER> <REGISTER_COUNT/VALUES> 

The table below explains what each option means:

1. Format version 1
2. Cookie from 0 to 264 -1
3. Serial device ID a string used to identify a serial device. Must match with Device ID field in MQTT Gateway page Serial gateway configuration section
4. Timeout timeout for Modbus connection, in seconds. Range [1..999].
5. Slave ID Indicates to which slave request is sent
6. Modbus function Modbus task type that will be executed. Possible values are:
  • 1 - read coils;
  • 2 - read input coils;
  • 3 - read holding registers;
  • 4 - read input registers;
  • 5 - set single coil;
  • 6 - write to a single holding register;
  • 15 - set multiple coils;
  • 16 - write to multiple holding registers.
7. First register number (not address) of the first register/coil/input (in range [1..65536]) from which the registers/coils/inputs will be read/written to.
8. Registry count
  • 1 - coil count (in range [1..2000]); must not exceed the boundary (first coil number + coil count <= 65537);
  • 2 - input count (in range [1..2000]); must not exceed the boundary (first input number + input count <= 65537);
  • 3 - holding register count (in range [0..125]); must not exceed the boundary (first register number + holding register count <= 65537);
  • 4 - input register count (in range [0..125]); must not exceed the boundary (first register number + input register count <= 65537);
  • 5 - coil value (in range [0..1]);
  • 6 - holding register value (in range [0..65535]);
  • 15 - coil count (in range [1..1968]); must not exceed the boundary (first coil number + coil count <= 65537); and coil values separated with commas, without spaces (e.g., 1,2,3,654,21,789); there must be exactly as many values as specified (with coil count); each value must be in the range of [0..1].
  • Examples


    Setting relay (on) (Relay address is 202, which means 'Number of first register will be 203) 1 1 1 1 1 6 203 1
    Getting temperature 1 1 1 1 1 3 6 2

    Testing MQTT Publisher and Subscriber on flespi.io


    Adding Flespi Subscriber


    To test the Modbus Serial Gateway functionality, log into your Flespi accountMQTT Board and 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

    Flespi subscriber.png Flespi subscriber setup.png

    Adding Flespi Subscriber


    Also, you will need to 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 temperature' is used
    5. Press 'Publish' button

    Flespi publisher.png Flespi publisher setup.png

    Flespi Subscriber output


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


    Flespi response.png


    In the output, we can see that router's temperature is 44 degrees Celsius.

    See Also

    External links

    Flespi.io