Coreflux Modbus 2 MQTT, bring your industrial devices into the IoT world.

Parvez Rasool
Coreflux Blog
Published in
5 min readJan 24, 2022

The Purpose of this Flux Asset

The main purpose of this asset is to provide from a Modbus Slave device a data flow of topics or a JSON topic into an MQTT Broker.

Glossary of Parameters (Configuration)

The parameters can be configured in the following ways:

MQTT Broker Server Address

In this setting we provide the MQTT broker address to connect to.

MQTT Broker Server Port

The MQTT port usually 1883, but you can change to the one you are using.

Where To publish Values

This config provides the Base Topic values. So every variable will have Base Topic + Var Topic. Example: a user sets this property with “Coreflux/Modbus2MQTT/Data” and the variable name is “VoltagePhase1" then the full topic of the variable is “Coreflux/Modbus2MQTT/Data/VoltagePhase1”.

Publish as JSON on Topic

This setting provides all information into a JSON topic. This enables the system to define objects on the mqtt and bring your solution into an IoT Object oriented solution.

Where to Publish Errors

If a connection to the device fails, or a certain memory register can’t be obtained or even a sudden disconnect you can get the log in this topic.

Retain MQTT messages

This setting defines that all publish actions are in retain.

QOS Level MQTT

The quality of service of publishing/subscription of the asset.

Modbus Device Address

The TCP/IP address of the device . Ex: 192.168.1.100

Modbus Server TCP Port

The TCP/IP port usually 502.

Modbus Polling Interval (ms)

This is the polling interval, use it wisely since some devices might be using e same Task/Thread to process and communicate. Usually 50ms interval should be enough. But based on your devices , network you are able to get 4 to 6ms update.

Coreflux below V1.2.0

Go to your Coreflux installation asset you should find the config file of the asset.

Go to the section below where you can find the SlaveId and the array of data.

Slave ID

Check the slave ID you want to gather data. If you have a modbus gatherer you might have more than a device in that same Slave. Any case check with your vendor, if it is only a device connection this setting should be 1 in most of the cases.

Array of the Data

For the variables you should find an XML with this in the configuration:

<ArrayOfString xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=”http://www.w3.org/2001/XMLSchema">
<string>varName=first|memPosition=540|type=Signed|lenght=0|functionToUse=ReadHoldingRegisters|direction=modbus2mqtt</string>
</ArrayOfString>

Add the following between the <ArrayOfString> </ArrayofString> separator :

<string>varName=first|memPosition=540|type=Signed|lenght=0|functionToUse=ReadHoldingRegisters|direction=modbus2mqtt</string>

And change according to your necessities . You can add multiple like this example:

<ArrayOfString xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=”http://www.w3.org/2001/XMLSchema">
<string>varName=first|memPosition=540|type=Signed|lenght=0|functionToUse=ReadHoldingRegisters|direction=modbus2mqtt</string><string>varName=second|memPosition=640|type=Signed|lenght=0|functionToUse=ReadHoldingRegisters|direction=modbus2mqtt</string>
</ArrayOfString>

VarName

This provides the topic of the variable . Example: a user sets the property Where To publish Values with “Coreflux/Modbus2MQTT/Data” and the variable name is “VoltagePhase1” then the full topic of the variable is “Coreflux/Modbus2MQTT/Data/VoltagePhase1”.

memPosition

This is the memory address on the Modbus device. You should check with your vendor the registers and the values they hold .

An example of a PAC3200 from Siemens and it’s modbus registers

Type

Float — size 2 in memory register usually 32 bits. (range 3.4E +/- 38)

Double — size 4in memory register usually 64 bits (range 1.7E +/- 308)

Ulong — size 2 in memory register usually 32 bits. (range from 0 to 4,294,967,295)

Bool — size 1 in memory register usually writes/reads 16 bits to change 1 bit

Uint16 — size 1 in memory register usually 16 bits

Signed — size 2 in memory register usually 32 bits. (+/-2,147,483,648)

Byte — size 1 in memory register usually writes/reads 16 bits to change 8bits

Aggregated — size 1 in memory but uses length to fetch other values.

Length

When a variable is Aggregated (a string or an array) you need to use this value to fetch the full variable size.

FunctionToUse

In modbus we have different functions to load or save data.

ReadCoilStatus — Reads a single coil corresponds to true/false (usually function 1)

ReadInputStatus -Reads discrete inputs from the field device (usually function 2)

ReadHoldingRegisters — Retrieves the contents of the holding registers from the field device (usually function 3)

ReadInputRegisters- Retrieves the contents of input registers from the field device (usually function 4)

ForceSingleCoil -Change the status of a single logic coil to true/false (usually function 5)

PresetSingleRegister — Change the content of a single holding register (usually function 6)

ForceMultipleCoils — Changes the on/off status of multiple coils (usually function 15)

PresetMultipleRegistersExample — Changes the content of multiple holding register . (don’t forget the length value)

Direction

Setting the value to modbus2mqtt it copies the register from modbus to mqtt. It can be used with functions ReadCoilStatus, ReadInputStatus, ReadHoldingRegisters and ReadInputRegisters.

Setting the value to mqtt2modbus it copies the register from mqtt to modbus. It can be used with functions ForceSingleCoil, PresetSingleRegister, ForceMultipleCoils and PresetMultipleRegistersExample.

Conclusion

So that is it to configure an Mosbus 2 Mqtt Coreflux assset .

Try to check the following links:

Check out this example application using the Coreflux MQTT Asset and Modbus 2 MQTT asset -> Energy Monitoring

For C# users please check :

https://github.com/CorefluxCommunity/CorefluxMQTTcSharpAPI

--

--