Coreflux Modbus2MQTT Asset (v1.2)

Alexandr Baranovschi
Coreflux Blog
Published in
4 min readMar 9, 2023

In this guide, we will provide a comprehensive overview of Modbus2MQTT, including its features, benefits, and how to install and configure the software. With this guide, users can easily integrate their Modbus devices with their data hub, enabling seamless communication for their IIoT projects.

If it’s your first time using Coreflux v1.2, please refer to: Presenting Coreflux Central v1.2 🚀.

Modbus is still a standard in the Energy sector

Installation

There are two methods to install the asset, GUI and Command-line. In this guide, we will focus only on the new command-line feature.

Requirements:

1. Basic understanding of MQTT protocol
2. Coreflux account
3. Coreflux v1.2
4. Coreflux Mqtt Broker asset
5. MQTT Client (such as MQTT Explorer)

Setup

First, make sure you are logged in by sending the following payload in the topic: $SYS/Coreflux/Cloud/Command

-L <User> <Password>

Make sure you have available assets using:

-l

Install the asset:

-I coreflux_modbus2mqtt

At this point, the asset is installed but it’s not ready to be used. It is necessary to configure it first.

Configuration

In order to configure the asset it is necessary to run only 1 command:

-assetConfigSave <guid> <configuration>

The <guid> is a unique asset id that is generated during the installation. To get the asset guid, consult the $SYS/Coreflux/Assets topic.

The <configuration> parameter is where you are able to set the asset settings. Now, imagine this scenario:

Your client wants to get sensor data from a Modbus device. The hardware setup is ready and a broker was initialized with IP: 192.168.0.33 and port: 1883.

The Modbus device has 2 temperature sensors connected to it and the following connection configuration:

Modbus IP: 192.168.0.22
Modbus port: 502
Interface: TCP/IP


Sensor 1 temperature (data type — float) mapped to Modbus register 1;
Sensor 2 temperature (data type — float) mapped to Modbus register 3;

Considering the scenario described above, a possible <configuration> might look like this:

{
"MQTTParameters": {
"Port": 1883,
"Address": "192.168.100.210",
"IsAnonymous": false,
"Username": "",
"Password": "",
"EnableTLS": false,
"EnableDebugTopic": true,
"DebugTopic": "$SYS/Coreflux/modbus2mqtt/DebugTopic"
},
"ModbusParameters": {
"Interface": "TCPIP",
"Port": "502",
"IP": "192.168.100.200",
"PollingMs": 500
},
"Tags": [
{
"Name": "sensor1Example",
"WriteDirection": "ToMQTT",
"MQTTTopic": "data/temperature/SensorTemp1",
"MQTTQoS": "AtMostOnce",
"MQTTRetain": false,
"ModbusMemoryArea": "DiscreteInputs",
"ModbusMemoryAddress": 1,
"ModbusDataType": "Boolean"
},
{
"Name": "sensor2Example",
"WriteDirection": "ToMQTT",
"MQTTTopic": "data/temperature/SensorTemp2",
"MQTTQoS": "AtMostOnce",
"MQTTRetain": false,
"ModbusMemoryArea": "DiscreteInputs",
"ModbusMemoryAddress": 3,
"ModbusDataType": "Boolean"
}
]
}

Now that configuration is ready, let’s break it down in parts and analyze the parameters.

The configuration is composed by 3 parts:

{
"MQTTParameters": {...},
"ModbusParameters": {...},
"Tags": [...],
}

MQTTParameters are used to specify the MQTT broker that will be used to publish or get data from. It also contains an option to activate the debug topic. The debug topic gives feedback on the connection state.

The ModbusParameters contain the device configuration. Interface option: “TCPIP” or SerialRTU; IP and Port of the device; “PollingMs”, this value (in milliseconds) will vary according to the device, in case of poor connection, increase this value and test again.

“Tags” represent an array of “Tag”. Each tag consists of various parameters, the following table describes all possible configurations and some examples:

Tag parameters with examples.

And the full command to configure the asset will look like this:

-assetConfigSave 9185f209-b7af-45df-81b2-6787451577f5 {
"MQTTParameters": {
"Port": 1883,
"Address": "192.168.100.210",
"IsAnonymous": false,
"Username": "",
"Password": "",
"EnableTLS": false,
"EnableDebugTopic": true,
"DebugTopic": "$SYS/Coreflux/modbus2mqtt/DebugTopic"
},
"ModbusParameters": {
"Interface": "TCPIP",
"Port": "502",
"IP": "192.168.100.200",
"PollingMs": 500
},
"Tags": [
{
"Name": "sensor1Example",
"WriteDirection": "ToMQTT",
"MQTTTopic": "data/temperature/SensorTemp1",
"MQTTQoS": "AtMostOnce",
"MQTTRetain": false,
"ModbusMemoryArea": "DiscreteInputs",
"ModbusMemoryAddress": 1,
"ModbusDataType": "Boolean"
},
{
"Name": "sensor2Example",
"WriteDirection": "ToMQTT",
"MQTTTopic": "data/temperature/SensorTemp2",
"MQTTQoS": "AtMostOnce",
"MQTTRetain": false,
"ModbusMemoryArea": "DiscreteInputs",
"ModbusMemoryAddress": 3,
"ModbusDataType": "Boolean"
}
]
}

And finally we can run the asset by publishing the “-R” (run) command with the asset guid to $SYS/Coreflux/Cloud/Command topic:

-R 9185f209-b7af-45df-81b2-6787451577f5

And the asset is running! 🎉

Using the MQTT client it is now possible to visualize the temperature data.

Next steps

Now that the asset is configured, and temperature data is flowing, why not store it in the ☁ Cloud using the “Firebase-2-MQTT” asset?

Or maybe you want to create a trigger when the temperature reaches a certain value, post that value, add units to it and compares to the last entry. Seems like a lot of work? Not really, Coreflux “Digital worker asset will take care of it.

For more information and help join our Discord community “Coreflux — Flux the System”.

--

--