MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe based message transmission protocol, suitable for resource-constrained devices and network environments with low bandwidth, high latency, or instability. It is widely popular in IoT applications, enabling efficient communication between sensors, actuators, and other devices.
QoS
QoS (Quality of Service) refers to the quality of service for message transmission. It can be set on both the message sender and the message consumer sides.
QoS setting on the sender side: Affects the transmission quality from the sender to the MQTT Broker of the server message queue.
QoS setting on the consumer side: Affects the transmission quality from the MQTT Broker server to the consumer.
QoS includes the following levels:
QoS 0: Represents at most once delivery.
QoS 1: Represents at least once delivery.
QoS 2: Represents exactly once delivery.
This manual uses a WINDOWS computer as the MQTT Broker, a personal computer, and an iComputer as clients to implement mutual subscription and information publishing. As shown in the following figure:
The following table outlines the product components of this manual
Product Component | Description |
---|---|
iotmqtt_0.0.2_amd64.deb | iComputer MQTT component |
MQTT_Client.projectarchive | PLC project archive |
sf4202_iotmqtt_1.0.2.0.library | PLC project library file |
emqx-5.3.2-windows-amd64.zip | EMQX compressed file (MQTT Broker) |
MQTTX-Setup-1.11.0-x64.exe | MQTT client installation program |
Installation Requirements
The built-in system of Sinsegye's factory-produced iComputer;
All series of SX and SP iComputers
The iComputer can access the Internet;
Installation Process
1. Install EMQX (MQTT Broker)
Unzip the emqx-5.3.2-windows-amd64.zip downloaded from the official website to the corresponding folder.
Open the bin directory in the folder, type "cmd" in the folder location, and press Enter.
Type in the command line——emqx start——and press Enter
Startup completed
EMQX port numbers:
Port Number | Description |
---|---|
1833 | TCP port |
8883 | WebSocket port |
8884 | WebSocket Secure port |
8883 | SSL/TLS port |
18083 | Broker's Dashboard access port |
You can access the MQTT Broker visualization interface through port 18083.
Local access: 127.0.0.1:18083/
External access: 192.168.110.147:18083/
(192.168.110.147 is the IP address of the MQTT Broker in this example)
Username: admin
Password: public
The first login will prompt to change the password, or you can choose to skip.
Dashboard interface, where you can set the Broker and view client connection status, message subscriptions, etc.
Install MQTTX on WINDOWS Personal Computer
This manual uses MQTTX as the client for message publishing and subscription.
Open the MQTTX-Setup-x64.exe program downloaded from the official website, select the installation options—Next.
Select the installation directory—Install
After installation, click the MQTTX icon—New Connection.
Fill in the client information
Parameter | Description | Example Setting |
---|---|---|
Name | Client name | user1 |
Host | Host IP address | 192.168.110.147 |
Port | Port number | 1883 |
Client ID | Client ID, must be a unique identifier | mqtt_user1 |
Click "Connect" in the upper right corner to connect. After successful connection:
You can see that the number of connections becomes 1 on the MQTT Broker interface.
Click the client to display client ID, connection status, IP address, etc.:
Return to the MQTTX client, publish a message in Plaintext format, with the topic "a" and the payload content "a=0"
On the Broker interface, you can see that the number of incoming messages is 1. Since no client subscribes to topic "a", all messages are discarded.
3. Install iotmqtt_0.0.2_amd64.deb component on iComputer
Refer to Appendix I, use Windows cmd to enter commands to install the iotmqtt_0.0.2_amd64.deb file into the iComputer.
scp -P 2224 iotmqtt_0.0.2_amd64.deb sinsegye@192.168.110.137:~
ssh -p 2224 sinsegye@192.168.110.137
sudo dpkg -i iotmqtt_0.0.2_amd64.deb
sudo nano /usr/local/etc/SinsegyeRTE/SinsegyeRTE.cfg
Modify ComponentManager
sudo reboot
4. Install iotmqtt.library file on iComputer
Open the MetaFacture program, select "Tools"——"Library Storage"
Select "Install"
Select the iotmqtt.library file downloaded from Sinsegye official website
Installation completed
Update Installation
Refer to the previous chapter "Installation Process", overwrite and install with new deb files and library files to update.
Uninstallation Process
1. Stop EMQX (MQTT Broker)
Open the EMQX folder——open the bin directory, type "cmd" in the folder location, and press Enter.
Type in the command line——emqx stop——and press Enter
Wait for a moment, then the EMQX service will stop.
2. Uninstall MQTTX using Control Panel
3. Uninstall iotmqtt_0.0.2_amd64.deb component from iComputer
sudo dpkg -r iotmqtt
3.2 Modify the RTE configuration file, remove iotmqtt from the ComponentManger module
sudo nano /usr/local/etc/SinsegyeRTE/SinsegyeRTE.cfg
Delete Component.8=iotmqtt
3.3 Restart the iComputer
sudo reboot
4. Uninstall the library file iotmqtt.library from MetaFacture
4.1 Open the software, click "Tools"—"Library Storage"
4.2 Find the library file to be uninstalled in "Miscellaneous", select it and click "Uninstall"
Quick Start
(I) Software and hardware configuration in this example
Hardware: | Software: |
---|---|
1. iComputer SX20 | 1. Server software emqx-5.3.2 |
2. Server computer (WINDOWS) | 2. Client software MQTTX 1.11.0 |
3. Personal computer (WINDOWS) | 3. iComputer component iotmqtt_0.0.2_amd64.deb |
4. PLC library file iotmqtt.library | |
5. PLC archive project MQTT_Client.projectarchive | |
6. Sinsegye MetaFacture V1.0.6.3 |
(II) Experimental operation steps in this example
1. Experimental Requirements
a. MQTT Broker runs EMQX successfully;
b. Install MQTTX on the personal computer, connect to the Broker successfully, and subscribe to the topic "zksd";
c. The iComputer successfully installs the iotmqtt_0.0.2_amd64.deb component;
d. MetaFacture successfully installs the library file iotmqtt.library;
2. Experimental Schematic Diagram
3. Experimental Steps
3.1 Use MetaFacture to open the PLC archive project "MQTT_Client.projectarchive". Log in to the device, download the PLC program and start running.
3.2 Click the PLC program "Connect", configure the IP address and port of the Broker, set the variable bConnect to TRUE, and the iComputer connects to the Broker.
You can check the connected client status on the Broker Dashboard interface.
3.3 Click the PLC program "Publish", set the variable bPublish to TRUE. The MQTTX client can receive the subscribed message.
3.4 Click the PLC program "Subscribe", set the variable bSubscribe to TRUE. Subscribe to three topics "x", "y", "z". Send three topic messages in the MQTTX client. The PLC program variables topic and payload can receive the topic and content.
Send topic messages:
3.5 Click the PLC program "Unbscribe", set the variable bUnSubscribe to TRUE. Unsubscribe from "x", "y", "z". You can see the unsubscription in the Broker Dashboard interface—Subscription Management.
3.6 After the MQTTX client disconnects and reconnects, it can receive the retained message marked as Retained.
3.7 Manually unplug the network cable of the iComputer, after about 5 seconds, the MQTTX client receives a will message "offine".
Open Sinsegye MetaFacture V1.0.6.3, select "File"——"New Project"
Select Standard Project——name it MQTT_Client——click "OK"
Select Sinsegye iComputer and programming language (ST language in this example)
Select "Library Manager"——"Add Library"——select "iotmqtt" in Miscellaneous——click "OK".
The library file is added to the PLC project.
Right-click "Application"——"Add Object"——"Global Variable List"
Name the global variable list
Add the following variables:
Instance Name | Function Block Name | Description |
---|---|---|
Client | TMP.FB_IotMqttClient | Instantiate MQTT client |
MessageQueue | TMP.FB_IotMqttMessageQueue | Queue for stored subscribed messages |
Message | TMP.FB_IotMqttMessage | A single subscribed message taken out |
Right-click "Application"——"Add Object"——"POU"
Name the program "Connect", select ST language, then click "Add"
In the Connect PLC, right-click the blank area and select "Input Assistant"
Click "Instance Call"——find the Client in GVL of the project Application, click "OK"
Add parameters: Client ID, Host IP, Host Port, keep-alive time, message queue pointer.
For the parameter description of the function block, see the Function Introduction chapter.
Parameter | Description | Example Setting |
---|---|---|
sClientId | Client ID | 'Sinsegye' |
sHostName | Connection IP address | '192.168.110.147' |
nHostPort | Connection port number | 1883 |
nKeepAlive | Interval time for sending heartbeat messages, in seconds | 60 |
ipMessageQueue | Pointer to the message queue | ADR(MessageQueue) |
Declare a BOOL type connection variable bConnect
Use the Client.Execute() method to connect, pass the variable bConnect as an actual parameter to the method.
For method parameter description, see the Function Introduction chapter.
Click the PLC_PRG program and call the Connect program.
Log in to the iComputer and download the PLC to the iComputer.
Start the PLC
Set the bConnect variable to TRUE, press Ctrl+F7 to write the variable, and the client connects to the MQTT Broker.
Check the client interface of the Broker's Dashboard, you can see that mqttx_user1 is the client created by the personal computer using MQTTX, and Sinsegye is the client created by the iComputer.
Create a new POU program named Publish.
Declare variables
Add PLC program, where the bPublish_Trig function block is used to take the rising edge of the bPubish variable.
Use the Client.Publish() method to publish a message,
Publish a message with the topic zksd and content "This is Sinsegye".
For method parameter description, see the Function Introduction chapter.
On the personal computer's MQTTX client, subscribe to the zksd topic
Click the PLC_PRG program and call the Publish program.
Log in to the iComputer, download the PLC to the iComputer, and start the PLC.
First, set the bConnect variable to TRUE, press Ctrl+F7 to write the variable, and the client connects to the MQTT Broker.
Then, set the bPublish variable to TRUE, press Ctrl+F7 to write the variable, and publish the message.
The MQTTX client receives the message with topic zksd
Create a new POU program named Subscribe.
Declare variables
Add PLC program. Use the Client.Subscribe() method to subscribe to messages.
In this example, three topics are subscribed: 'x', 'y', and 'z'.
For method parameter description, see the Function Introduction chapter.
When a message is sent, it is stored in the message queue. To retrieve the message, add PLC code for message retrieval:
Call the Subscribe program in PLC_PRG
Log in to the iComputer, download the PLC to the iComputer, and start the PLC.
First, set the bConnect variable to TRUE, press Ctrl+F7 to write the variable, and the client connects to the MQTT Broker.
Then, set the bSubscribe variable to TRUE, press Ctrl+F7 to write the variable, and subscribe to messages of the three topics.
Subscription successful
You can see the successful subscription in the Broker's Subscription Management.
Test by publishing messages in the personal computer's MQTTX client
Publishing Message Sequence | Topic | Content |
---|---|---|
1 | x | x=1 |
2 | y | y=2 |
3 | z | z=3 |
4 | x | x=4 |
In the PLC program, the variable topic records the topics of historical messages
The variable payload records the content of historical messages
Create a new POU program named UnSubscribe, and declare the following variables.
Add PLC program. Use the Client.UnSubscribe() method to unsubscribe. Pass the String type topic characters to unsubscribe from 'x', 'y', and 'z' topic messages.
For method parameter description, see the Function Introduction chapter.
Call the UnSubscribe program in PLC_PRG
Log in to the iComputer, download the PLC to the iComputer, and start the PLC.
First, set the bConnect variable to TRUE, press Ctrl+F7 to write the variable, and the client connects to the MQTT Broker. Check the Broker's Subscription Management, Sinsegye subscribes to three topics.
Then, set the bUnsubscribe variable to TRUE, press Ctrl+F7 to write the variable, and unsubscribe from messages of the three topics.
Unsubscription successful
Check the Broker's Subscription Management, click "Refresh", Sinsegye has unsubscribed.
MQTT Retained Messages are a way to publish persistent messages in MQTT. When a client publishes a retained message, it is saved on the MQTT server. When a new subscriber connects to the server, the server sends this retained message to the new subscriber. Only one retained message is stored for a topic, and a new retained message replaces the old one.
Set the bRetain parameter of the Client.Publish() method to TRUE, and the published message is a retained message.
Log in to the iComputer, download the PLC to the iComputer, and start the PLC.
First, set the bConnect variable to TRUE, press Ctrl+F7 to write the variable, and the client connects to the MQTT Broker.
Then, set the bPublish variable to TRUE, press Ctrl+F7 to write the variable, and publish the retained message.
If the MQTTX client subscribing to zksd is online when the retained message is sent, it will receive the message normally.
If the MQTTX client subscribing to zksd is offline when the retained message is sent, the MQTTX client will receive the retained message marked as Retained after going online.
A will message is a special message used to notify other subscribers of the offline status when a client disconnects abnormally.
Declare the will message in the Connect program.
Set the members of the will message structure, and pass the structure to the stWill parameter of the Client. Meanwhile, to facilitate the demonstration of abnormal disconnection, set the heartbeat message sending time to 5s.
Will Structure Member | Type | Example Setting | Description |
---|---|---|---|
sTopic | String | 'zksd' | The topic of the will message is zksd |
pPayload | Pointer | ADR(WillingPayload) | Pointer to the will message content |
nPayloadSize | UDINT | INT_TO_UDINT(Len(WillingPayload)) | Length of the will message data |
eQoS | UINT | 2 | eQoS message quality level 2 |
bRetain | Bool | FALSE | Not a retained message |
Log in to the iComputer, download the PLC to the iComputer, and start the PLC.
First, set the bConnect variable to TRUE, press Ctrl+F7 to write the variable, and the client connects to the MQTT Broker.
Manually unplug the network cable of the iComputer.
On the MQTTX client, receive the will message.