How To Articles > How to Access Modbus Data Via MQTT

How to Access Modbus Data Via MQTT

How to Access Modbus Data Via MQTT

Open Automation Software Tags can be defined to connect directly to Modbus slave devices or host data to Modbus masters with the built in Modbus Driver Interface which supports communications over ethernet and serial interfaces for Modbus TCP, Modbus RTU, and Modbus ASCII protocols. OAS can act as both an MQTT Client and an MQTT Broker allowing you to access your Modbus data via MQTT. This tutorial walks you though downloading and installing OAS, configuring a Modbus driver, configuring tags and reading and writing to them via MQTT.

Step 1. Download and Install the Open Automation Software and Start the OAS Service

If you have not already done so, you will need to download and install the OAS platform.  Fully functional trial versions of the software are available for Windows, Windows IoT Core, Linux, Raspberry Pi and Docker on our downloads page.

On Windows run the downloaded Setup.exe file to install one or more of the Open Automation Software features. Select the default Typical installation if you are not sure what features to use or the Custom installation if you want to save disk space on the target system.  When prompted agree to the End User License Agreement to continue the installation.

For more detailed instructions and video tutorials, visit the installation guide for your system:
Windows Installation | Linux Installation | Raspberry Pi Installation | Dockers Installation

When the installation is finished the OAS Service Control application will appear.  Use this application to start the 4 Services. If this is the first time installing the software it will automatically enter Runtime with an example Tag Configuration.

Step 2. Configure Your Modbus Data Source

  1. First, you will need to open the Configure OAS application from the program group Open Automation Software.

  2. Select Configure >> License from the top menu and verify that Modbus is one of the available Drivers in the lower left of the form. The demo license will have this by default. If you do not see Modbus available, contact to update your license.

  3. Select Configure >> Drivers from the top menu.

  4. Select localhost or the remote service you wish to modify with the Select button to the right of the Network Node list.

  5. The Configure Drivers Screen will appear. Select Modbus from the Driver dropdown box.

  6. Enter a meaningful Driver Interface Name that you will refer to this physical connection when defining Tags with a Modbus Data Source.

  7. Specify the Connection as Ethernet or Serial.

  8. Specify the Modbus Type as Master or Slave. Master will be used when communicating to a Modbus device. Slave will be used when other Modbus masters will be communicating to OAS.

  9. When setting up a Slave interface over Ethernet set the IP Address to the computer IPv4 IP address or network node name if the master is on a remote PC. You can also use or localhost if the Modbus master will be on the same computer.

For more detailed instructions on configuring your Modbus data source, click here to see our Getting Started Modbus tutorial or watch the video tutorial below:

Step 3. Configure Your Tags

OAS provides multiple ways to add and define tags:

To add a Tag manually:

  1. In the OAS Configure Application, select Configure >> Tags from the top menu.

  2. Select localhost or the remote service you wish to modify with the Select button to the right of the Network Node list.

  3. Click on the Add Tag button located at the top of the Tag browser on the left portion of the screen.

  4. A dialog box will appear. Enter a name for your new tag and click ok.

  5. A configuration screen will appear for your new tag. Select your data source type in in the Data Source dropdown box.

  6. Specify the correct data type in the Data Type dropdown box.

  7. Click Apply Changes at the bottom right of the window.

For more detailed instructions on configuring your tags, click here to see our Getting Started Tags tutorial.

Step 4. Access Your Data with the OAS MQTT Broker

The OAS MQTT broker will work without configuration but if you want to use SSL or have security set on your OAS Service you will need to configure it. Please see our Getting Started – MQTT Broker page in the knowledge base if this applies to you.

Accessing Your Tag Data (Subscribing)

This example uses MQTT Explorer, it is a free MQTT client and you can download it here.

Enter the address of your MQTT Broker in the host name. This will be the IP address or domain name of where you have the OAS Server installed. If it is on your local machine, you can just enter localhost. The name field is just a friendly-name so enter something meaningful to you, perhaps your Broker ID. Enter the Port and Username and Password if you have set one up. Click on the Advanced button.

Here is where you can subscribe to your topics (OAS tags). In the Topic box, enter TagName.Value, for example, Ramp.Value and click Add. You can also subscribe to any other property of the tag, for example, Ramp.DataSource. Once you are done adding topics, click Back.

Now click on save and then connect. You should now see your data coming in. The payload is in standard JSON format and includes the Value, Timestamp, and Quality.

Writing to Your Data Source (Publishing)

Follow the same setup for the MQTT Explorer client that you did for subscribing to connect to the OAS MQTT Broker but you can skip the Advanced button part. Click Connect.

In the lower right portion of the client window, enter in your Topic, this will be the tag you are trying to write to, for example, MyValueTag.Value. Switch the selector to raw and enter a value in the text box below the Publish button. Click Publish. If you would like to write your value in json format, you can. Using the JSON Calculations in OAS you can parse the JSON message for extended functionality.

The video below, goes into detail about how to do that as well as an overview of the OAS Broker.