Эта страница является
переводом страницы
CloudMQTT. Перевод выполнен на 100%.
Connecting Modbus TCP devices to MQTT cloud services
MQTT has advantages of a telemetry protocol: it provides data exchange between devices (clients) in conditions on unreliable or unstable connection. Devices can disconnect from the net and when they are connected back, the MQTT-broker definetely sends a message.
Different cloud services with MQTT broker, such as, Azure IoT Suite by Microsoft or AWS IoT by Amazon have become quite popular.
You can connect devices that do not support MQTT to MQTT cloud services, using iRidium server as a gateway.
Description of example
iRidium server is used to connect Modbus TCP devices to MQTT cloud service. A panel for which a separate graphical interface is developed is also connected to the service. Thus, there appears a capability to control devices in Modbus TCP network remotely via MQTT telemetry protocol.
Setting CloudMQTT
CloudMQTT suggests different buying plans.
You can select a free plan - Cute Cat, that provides a virtual server (not dedicated), 5 users (max), 5 acl rules, 5 connections and 10 Kbit/s traffic. It is not recommended for commercial use. But it is suitable for testing and hobbies.
1 Register at CloudMQTT web-site and log in with your login and password.
2 Click "Create New Instance" button.
3 Set the name of instance, a plan and the server location (Data Center).
Click "Create New Instance".
4 Click on the name of the created instance to go to its settings.
5 All data required for connection to the cloud service are displayed in the DETAILS tab.
Setting iRidium server
Setting MQTT driver
1 Create a server project.
2 Add MQTT driver to a project.
3 Set the driver according to the DETAILS tab of the instance in the CloudMQTT service:
- Host - address of the service instance (Server)
- Port - instance port:
- 13816 (unprotected connection),
- 23816 (SSL protected connection),
- 33816 (WebSocket protected connection; not supported by iRidium pro)
- Login - login to access instance (User)
- Password - password to access instance (Password)
- Client ID - ID of MQTT-client, a unique name for each MQTT device
- SSL - encrypting method:
- Disable
- TLS 1.2 (recommended)
- SSL 2.3
- SSL 3.0 (not supported by CloudMQTT)
More: Setting connection parameters to MQTT.
4 Create a required number of channels and tags. Set each channel and tag.
CloudMQTT allows to set any line to Topic parameter. It is recommended to set Topic in the standard hierarchic form that looks like an address line of a file system, for example,
/place/device_name/group_name/parameter_name
More: Setting parameters of MQTT channels and feedback.
Setting Modbus TCP driver
1 Add Modbus TCP driver in the project.
2 Set connection parameters.
3 Create the required number of commands and tags in the driver.
4 Set each command and tag according to the task.
Gateway between MQTT and Modbus TCP drivers
1 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);
}
});
2 Remember to set correct names of drivers that are connected via a gateway and the gateway map.
The gateway map (gatemap variable) is a list of corresponding pairs of tag and channel names of two different drivers. Data are exchanged between these pairs of tags and channels. More pair can be added to the list.
Unmanaged gateway
Method of managed gateway between drivers was described above. It required a gateway map of tags and channels. In a managed gateway you can set which data can be sent from a tag of one driver to a channel of another driver and which data can't be sent.
A simpler script can be used that does not require a gateway map. In this case the gateway is unmanaged and all data is sent from tags to channels. It's obligatory that tag names of one driver are identical to channel names of another driver and vice versa.
1 Give identical names to tags of MQTT drivers and channels of Modbus TCP driver, and to tags of Modbus TCP driver and channels of MQTT driver accordingly.
2 Delete the previous script and add the following script instead of it:
// 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);
});
3 It is enough to give driver names in this script.
Setting i3 pro project
1 Create a panel project.
2 Add MQTT driver to a project.
3 Set connection to CloudMQTT service the way it was done in a server project, that is, enter the same connection parameters of MQTT driver(Host, Port, Login, Password). Set another Client ID for CloudMQTT service to distinguish connecting clients from one another.
4 Create a required number of commands and tags. Variables that are commands on the server must be tags on a panel and vice versa. Tags on the server must correspond to commands on a panel.
5 Set Topic and Value Type parameters of tags and commands the same was as the corresponding server channels.
6 Create a graphic interface. Assign commands and tags of MQTT driver to graphic items by events or relations.
Testing projects
1 Launch a server project and a panel project in any order. You can use i3 pro Emulator and iRidium server for Windows.
2 Go to CONNECTIONS tab of CloudMQTT instance. The state of MQTT connections is displayed in this tab.
3 Go to WEBSOCKET UI tab that is used for testing by developers. Here you can see all received MQTT-messages and send them.
4 You can use any emulator of Modbus TCP server, for example, this free one Modbus PLC Simulator to test a project.
In the given example all publishers and subscribers have full access rights ti MQTT topics. But you can create rules and limitations in USERS & ACL tab of CloudMQTT instance.