Modbus TCP Master MQTT Gateway

From Teltonika Networks Wiki
Revision as of 10:19, 12 July 2022 by Karolis.t (talk | contribs) (testing added)

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.

Networking Topology MQTT MODBUS flespi configuration v1.png

Configuring RUT955 Modbus TCP Slave

Go to Services > Modbus > Modbus TCP Slave:

  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

*In this config LAN port is used so “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

  1. Press Add
  2. Check Enable
  3. Enter name for slave device
  4. Slave ID must match with previously configured Slave device ID
  5. Enter IP address of Modbus TCP Slave device
  6. Chose same Port as in Slave device - 502
  7. Enter Period in seconds, how often requests will be sent to Slave device
  8. Enter new request name and press Add in Requests configurations
  9. 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 WAN IP address of the slave device is used.
  10. Press Test button in Request Configuration Testing to see if Slave device respond to requests, response similar to image below should be shown.
  11. Press Save & Apply

Configuring Flespi.io MQTT Broker

Login or create an account on https://flespi.io

Networking MQTT Modbus flespiio login.png

Once logged in:

  1. Navigate to MQTT Board on the left side of the screen and press it. Networking MQTT Modbus flespi board.png
  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 an username
  4. Enter Client name
  5. Copy Host address
  6. Copy Username
  7. Create a password
  8. Press Save

Networking MQTT modbus fespi Connection settings v1.png

Configuring MQTT Gateway on RUT955 Modbus TCP Master

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

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

Networking MQTT Modbus Rut configuration Mqtt gateway v1.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

Format is in 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 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 v1.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

Login 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

Networking MQTT Modbus flespi subscriber config1 v1.png Networking MQTT Modbus flespi subscriber config2 v1.png


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

Networking MQTT Modbus flespi publisher config1 v1.png Networking MQTT Modbus flespi publisher config2 v1.png