Connecting Modbus TCP devices to MQTT cloud services
MQTT offers advantages as a telemetry protocol: it enables devices (clients) to exchange data over unreliable or intermittent connections. Devices can disconnect from the network, but when they reconnect, the MQTT broker guarantees delivery of the message.
Various cloud services with MQTT brokers have recently become popular, such as Microsoft Azure IoT Hub and Amazon AWS IoT.
You can connect devices that do not support MQTT, such as Modbus devices, to MQTT cloud services by using iRidi server as a gateway.
Example overview
iRidi server is used to connect Modbus TCP devices to the CloudMQTT cloud service. A panel also connects to the service and uses a separate Panel Project with a graphical interface. This enables remote control of devices on the Modbus TCP network over the MQTT telemetry protocol.
-54ec4504077cbaa01a4b6e470151d3de.png)
Configuring CloudMQTT
CloudMQTT offers various service plans.
You can choose the free Cute Cat plan, which provides a virtual server (not a dedicated server), a maximum of 5 users, 5 access rules and 5 connections, and 10 kbit/s of traffic. This plan is not recommended for commercial use, but is well suited to testing and home hobby projects.
-
Sign up on the CloudMQTT website and sign in to your account.
-
Click "Create New Instance".

- Specify the instance name, service plan and geographical location of the server (Data center ). Click "Create New Instance".

- Click the name of the instance you created to open its settings.
- The DETAILS tab displays all the details needed to connect to the cloud service.

Configuring iRidi server
Configuring the MQTT driver
-
Create a server project.
-
Add the MQTT driver to the project.
-3d59d890e11dc987ec9167472d02ffbf.png)
- Configure the driver using the DETAILS tab of the CloudMQTT service instance:
-
Host - service instance address (Server )
-
Port - instance port:
- 13816 (unsecured connection),
- 23816 (secure SSL connection),
- 33816 (secure WebSocket connection; not supported by iRidi pro)
-
Login - login for access to the instance (User )
-
Password - password for access to the instance (Password )
-
Client ID - MQTT client identifier, a unique name for each MQTT device
-
SSL - encryption method:
- Disable
- TLS 1.2 (recommended)
- SSL 2.3
- SSL 3.0 (not supported by CloudMQTT)
For details: Configuring MQTT connection parameters.
- Create the required number of channels and tags. Configure each channel and tag.

CloudMQTT allows an arbitrary string for the Topic parameter. It is recommended to specify Topic in a standard hierarchical form resembling a file system path, for example:
/place/device_name/group_name/parameter_name
For details: Configuring MQTT channel and feedback parameters.
Configuring the Modbus TCP driver
-
Add the Modbus TCP driver.
-
Configure the connection parameters.
-
Create the required number of commands and tags in the driver.
-
Configure each command and tag for your task.
Gateway between the MQTT and Modbus TCP drivers
- Add a script to the project:
var driver1 = IR.GetDevice("MQTT"); // name of a driver 1
var driver2 = IR.GetDevice("Modbus TCP"); // name of a driver 2
var gatemap = {
// Tag name Channel name
"Door_switch" : "MQTT_Door_switch",
"Ring_button" : "MQTT_Ring_button",
"Outdoor_temperature" : "MQTT_Outdoor_temperature",
"Indoor_temperature" : "MQTT_Indoor_temperature",
"MQTT_Lock" : "Lock",
"MQTT_Siren" : "Siren",
"MQTT_Dimmer_R" : "Dimmer_R",
"MQTT_Dimmer_G" : "Dimmer_G",
"MQTT_Dimmer_B" : "Dimmer_B",
"MQTT_Dimmer_ON" : "Dimmer_ON"
};
IR.AddListener(IR.EVENT_TAG_CHANGE, driver1, function(name, value) {
if (gatemap[name]) {
driver2.Set(gatemap[name], value);
}
});
IR.AddListener(IR.EVENT_TAG_CHANGE, driver2, function(name, value) {
if (gatemap[name]) {
driver1.Set(gatemap[name], value);
}
});
- Remember to specify the correct names of the drivers connected by the gateway in the script, as well as the gateway map.
The gateway map (the gatemap variable) is a list of corresponding tag and channel name pairs in two different drivers. Data is exchanged between these tag and channel pairs. You can add entries to this list.
Unmanaged gateway
The method described above creates a managed gateway between drivers and requires a gateway map of tags and channels. With a managed gateway, you can specify which data should be forwarded from a tag in one driver to a channel in the other, and which data should not.
A simpler script can be used without specifying a gateway map. In this case, the gateway is called unmanaged, and all data is forwarded from tags to channels. The tag names in one driver must match the channel names in the other driver, and vice versa.
-
Assign the same names to MQTT driver tags and Modbus TCP driver channels, and correspondingly to Modbus TCP driver tags and MQTT driver channels.
-
Remove the previous script. Add the following script instead:
// Assign the same names to tags of first driver and to channels of second driver,
// and accordingly - to tags of second drivers and to channels of first driver.
var driver1 = IR.GetDevice("MQTT"); // name of a driver 1
var driver2 = IR.GetDevice("Modbus TCP"); // name of a driver 2
IR.AddListener(IR.EVENT_TAG_CHANGE, driver1, function(name,value) {
driver2.Set(name, value);
});
IR.AddListener(IR.EVENT_TAG_CHANGE, driver2, function(name,value) {
driver1.Set(name, value);
});
- In this script, you only need to specify the driver names.
Configuring the i3 pro project
-
Create a Panel Project.
-
Add the MQTT driver.
-
Configure the connection to CloudMQTT in the same way as in the server project: enter the same MQTT driver connection parameters (Host , Port , Login , Password ). Assign a different client identifier (Client ID ) so that CloudMQTT can distinguish the connecting clients from one another.
-
Create the required number of commands and tags. Variables that were commands on the server must be mapped to tags on the panel, and conversely, server tags must correspond to commands on the panel.
-
For commands and tags, set the Topic and Value Type parameters to the same values as in the corresponding server channels.
-
Create the graphical interface. Connect its graphical items to MQTT driver commands and tags using events or bindings.
-a539c67e3caf7874b2ee62cb60349b4c.png)
Testing the projects
-
Start the server project and Panel Project in any order. You can use the i3 pro emulator and iRidi server for Windows for this.
-
Open the CONNECTIONS tab of the CloudMQTT instance. This tab displays the status of MQTT connections.

- Open the WEBSOCKET UI tab, which developers use for testing. Here you can view all received MQTT messages and send messages.

- You can use any Modbus TCP server emulator to test the project, such as the free Modbus PLC Simulator.
In this example, all publishers and subscribers have full access to MQTT topics, but you can configure rules and restrictions on the USERS & ACL tab of the CloudMQTT instance.