MQTT Client DOKU V3-0 PT - Documentos Google
MQTT Client DOKU V3-0 PT - Documentos Google
MQTT Client DOKU V3-0 PT - Documentos Google
com/cs/w
w/en/view/109748872
SIMATIC
S7-1500 and
S7-1200
Siemens Industry Online Support
Blocks for S7-1500 and S7-1200
Legal information
Legal information
Use of applica on examples
Application examples illustrate the solution of automation tasks through an interaction of several
components in the form of text, graphics and/or software modules. The application examples are
a free service by Siemens AG and/or a subsidiary of Siemens AG ("Siemens"). They are
non-binding and make no claim to completeness or functionality regarding configuration and
equipment. The application examples merely offer help with typical tasks; they do not constitute
customer-specific solutions. You yourself are responsible for the proper and safe operation of the
products in accordance with applicable regulations and must also check the function of the
respective application example and customize it for your system.
Siemens grants you the non-exclusive, non-sublicensable and non-transferable right to have the
application examples used by technically trained personnel. Any change to the application
examples is your responsibility. Sharing the application examples with third parties or copying the
application examples or excerpts thereof is permitted only in combination with your own products.
The application examples are not required to undergo the customary tests and quality inspections
of a chargeable product; they may have functional and performance defects as well as errors. It is
your responsibility to use them in such a manner that any malfunctions that may occur do not
result in property damage or injury to persons.
Disclaimer of liability
Siemens shall not assume any liability, for any legal reason whatsoever, including, without
limitation, liability for the usability, availability, completeness and freedom from defects of the
application examples as well as for related information, configuration and performance data and
any damage caused thereby. This shall not apply in cases of mandatory liability, for example
under the German Product Liability Act, or in cases of intent, gross negligence, or culpable loss of
life, bodily injury or damage to health, non-compliance with a guarantee, fraudulent
non-disclosure of a defect, or culpable breach of material contractual obligations. Claims for
damages arising from a breach of material contractual obligations shall however be limited to the
d
e
foreseeable damage typical of the type of agreement, unless liability arises from intent or gross
v
r
e
negligence or is based on loss of life, bodily injury or damage to health. The foregoing provisions
s
do not imply any change in the burden of proof to your detriment. You shall indemnify Siemens
s
against existing or future claims of third parties in this connection except where Siemens is
g
i
r
mandatorily liable.
l
By using the application examples you acknowledge that Siemens cannot be held liable for any
2
e
Other informa on
m
e
i
S
Siemens reserves the right to make changes to the application examples at any time without
♥
notice. In case of discrepancies between the suggestions in the application examples and other
Siemens publications such as catalogs, the content of the other documentation shall have
precedence.
The Siemens terms of use (https://support.industry.siemens.com) shall also apply.
Security informa on
Siemens provides products and solutions with Industrial Security functions that support the secure
operation of plants, systems, machines and networks.
In order to protect plants, systems, machines and networks against cyber threats, it is necessary
to implement – and continuously maintain – a holistic, state-of-the-art industrial security concept.
Siemens’ products and solutions constitute one element of such a concept.
Customers are responsible for preventing unauthorized access to their plants, systems, machines
and networks. Such systems, machines and components should only be connected to an
enterprise network or the Internet if and to the extent such a connection is necessary and only
when appropriate security measures (e.g. firewalls and/or network segmentation) are in place.
For additional information on industrial security measures that may be implemented, please visit
https://www.siemens.com/industrialsecurity.
Siemens’ products and solutions undergo continuous development to make them more secure.
Siemens strongly recommends that product updates are applied as soon as they are available
and that the latest product versions are used. Use of product versions that are no longer
supported, and failure to apply the latest updates may increase customer’s exposure to cyber
threats.
To stay informed about product updates, subscribe to the Siemens Industrial Security RSS Feed
at: https://www.siemens.com/industrialsecurity.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 2
Table of Contents
Table of Contents
Legal information.........................................................................................................
2 1 Introduction........................................................................................................ 4
1.1 Overview............................................................................................... 4
1.2 Principle of Operation........................................................................... 6
1.3 Components Used................................................................................ 7
2 Engineering ........................................................................................................ 9
2.1 Block Description.................................................................................. 9
2.2 Configuration ...................................................................................... 10
2.2.1 Create TIA Portal Project ................................................................... 10
2.3 Integration of the Function Block in the User Program ...................... 11
2.3.1 Opening the "LMQTT" Global Library ................................................ 11
2.3.2 Copying Function Blocks and Data Types to the User Program ....... 13
2.3.3 Creating Global Data Block ................................................................ 14
2.3.4 Calling Function Blocks in the User Program .................................... 19
2.4 Configuration of the Security Feature ................................................ 21
2.4.1 Using the TIA Portal Global Certificate Manager ............................... 22
2.4.2 Using the Local CPU Certificate Manager ......................................... 26
2.5 Parameterization and Operation ........................................................ 29
2.6 Error Handling .................................................................................... 35
3 Useful Information ........................................................................................... 36
d
e
3.1 Fundamentals of MQTT ..................................................................... 36
v
r
s
3.1.1 Terminology........................................................................................ 36
e
t
3.1.2 Standard and Architecture.................................................................. 37
h
g
i
r
2
3.1.4 Structure of the MQTT Control Packets ............................................. 40
0
m
3.1.7 MQTT Sub-Mechanism ...................................................................... 47
e
i
S
3.1.8 MQTT Ping Mechanism...................................................................... 50
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 3
1 Introduction
1 Introduction
1.1 Overview
Motivation
Digitization has a major impact on the economy and society and is progressing
inexorably. The "Internet of Things" (short: IoT) is one of the main drivers of
digitization. The term "Internet of Things" is synonymous with one of the biggest
current dynamics of change: the increasing networking and automation of devices,
machines and products.
The protocol "Message Queue Telemetry Transport" (short: MQTT) is used in the
"Internet of Things" as a communication protocol. Its lightweight approach opens
up new possibilities for automation.
Criteria of MQTT
The MQTT protocol is distinguished by the following criteria:
• Lightweight protocol with low transport overhead
d
v
r
l
l
e
of a client
m
e
i
S
• Simple use and implementation thanks to a small set of commands
♥
• Quality of Serice (QoS level) with different reliability levels for the message
delivery
• Optional encryption of messages with SSL/TLS
• Authentication of publishers and subscribers with username and password
Applicative implementation
To implement the MQTT protocol in a SIMATIC S7 Controller, the "LMQTT" library
offers an adequate solution.
The "LMQTT" library provides a function block for the SIMATIC S7-1500 and
SIMATIC S7-1200. The function block "LMQTT_Client" integrates the MQTT Client
function and allows you to submit MQTT messages to a broker (Publisher role) and
to create subscriptions (Subscriber role). The communication can be secured via a
TLS connection.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 4
1 Introduction
Figure 1-1
v
r
g
i
r
l
l
e
i
♥
Note The MQTT Client supports MQTT protocol version 3.1.1.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 5
1 Introduction
Certificate
Store idx 1
Cert_Name
1 Cert_xy
Topic x Topic z
3 Message Message
S7 CPU Topic y
12
Table 1-1
d
A
e
v
s
r
e n
s e
e m
r
e
i
s
h
S
Connection
♥
g
i (MQTT Client) parameters LMQTT_ Client
r
l
l
Message
A
MQTT over TLS 4
(Un)Subscribe Topic y Message
Publish
Step Description
Note A more detailed functional description of the function block "LMQTT_Client" and
information on the MQTT protocol can be found in section 3.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 6
1 Introduction
v
r
g
i
r
l
l
e
i
♥
This application example consists of the following components:
Table 1-3
Components File name
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 7
1 Introduction
Note With S7-1500 CPUs (firmware V2.0 or higher) or S7-1200 CPUs (firmware V4.4 or
higher), you can reach the MQTT Broker via a static IP address or a domain
name ("Qualified Domain Name", short: QDN) if you use the "LMQTT" library.
v
r
g
i
r
l
l
0
2
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 8
2 Engineering
2 Engineering
Note The engineering in this section focuses on the MQTT Client function, which
realizes this application example.
It is assumed that you have already installed and configured the MQTT Broker.
v
r
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 9
2 Engineering
2.2 Configuration
The application example in entry 109748872 shows the configuration.
1. Create a TIA Portal project with the CPU that you want to use for the
application example.
2. Parameterize the Ethernet interface of the CPU with an IP address that lies in
the same subnet as the MQTT Broker.
3. If you are using a cloud service like AWS, parameterize a router and a DNS
server.
4. Connect the CPU and the MQTT Broker via Ethernet.
Note For secured MQTT communication via TLS, you need an S7-1500 CPU with
firmware version 2.0 or higher, or an S7-1200 CPU with firmware V4.4 or higher.
v
r
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 10
2 Engineering
1. In the TIA Portal project, click the "Libraries" task card and open the "Global
Libraries" palette.
2. Click on the "Open global library" button.
The “Open global library” dialog is opened.
v
r
g
i
r
l
l
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 11
2 Engineering
v
r
e
4. The "Libraries_Comm_Controller" library opens and appears under the Global
r
h
libraries palette.
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 12
2 Engineering
2.3.2 Copying Function Blocks and Data Types to the User Program
v
r
g
i
r
l
l
n
2. Insert the function block for your CPU via drag & drop into the folder "Program
e
e
blocks" of your device, e.g. S7-1500 CPU.
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 13
2 Engineering
3. The data types used by the FB "LMQTT_Client" are automatically inserted into
the folder "PLC data types" on your device (e.g., an S7-1500 CPU).
v
r
g
i
r
l
l
A
1
e
i
♥
2.3.3 Creating Global Data Block
This section shows you how to create a global data block (DB). This DB is used to
store the following data:
• TCP connection parameters
• MQTT connection parameters
• Topic and message to be sent to the MQTT Broker (publish)
• Received data, i.e. message and name of the subscribed topic (subscribe)
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 14
2 Engineering
3. Make the following settings and then confirm your entries with the "OK" button. –
Select the symbol "Data block".
– Select "Global DB" as the type.
– Enter the name of the DB.
– Enable the "Automatic" radio button for automatic number assignment. The
number of the global DB is assigned by the TIA Portal.
d
v
r
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 15
2 Engineering
v
r
g
i
r
l
l
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 16
2 Engineering
Result
The following figure shows the tags in the DB "MqttDb" for switching the inputs and
outputs of the FB "MQTT_Client".
Figure 2-1
v
r
g
i
r
l
l
A
1
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 17
2 Engineering
Figure 2-2
d
v
r
g
i
r
l
l
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 18
2 Engineering
1. In the "Project tree" open the folder "Program blocks" of your CPU
2. Double-click the block "Main [OB1]" to open the corresponding program editor.
3. Drag & drop the FB "LMQTT_Client" from the project navigation to any OB1
network.
d
– Enable the "Automatic" radio button for automatic number assignment. The
e
number of the instance DB is assigned by the TIA Portal.
v
r
e
– Click "OK" to confirm the settings.
r
g
i
r
l
l
e
i
6. Assign the tags that you have created in the global data block to the inputs and
outputs of the FB (see section 2.3.3).
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 19
2 Engineering
Result
The following figure shows the linking of the DB "MqttDb" tags on the FB
"MQTT_Client".
Figure 2-3
d
v
r
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 20
2 Engineering
Note You only need to configure the security feature if you are using a secure MQTT
connection via TLS.
Note In this application example, the MQTT Broker does not authenticate the MQTT Client.
Only the CA certificate of the MQTT Broker is required to authenticate the
MQTT Broker.
If you have configured the MQTT Broker to require MQTT Client authentication,
you must also import the client certificate.
The client certificate must be signed by the same CA as the server certificate.
Encryption via SSL/TLS works via certificates. A certificate is a public key signed
by its owner that guarantees its authenticity and integrity. To authenticate the
broker, the MQTT Client requires the CA certificate of the broker.
This section shows you how to import the certificate of the MQTT Broker into the
CPU (MQTT Client). Encrypted MQTT communication is only possible with this
certificate.
d
Requirement for TLS/SSL encryption
e
v
To set up a secure MQTT communication between the SIMATIC S7 CPU (MQTT
r
e
Client) and an MQTT Broker in your network, the following points must be fulfilled:
r
• The MQTT Broker is installed and preconfigured for the TLS procedure.
g
l
l
n
A certificate always contains a period of time in which it is valid. To be able to
e
e
encrypt with the certificate, the time of the S7 CPU must also be within this
i
♥
period. With a brand new S7-CPU or after an overall reset of the S7-CPU, the
internal clock is set to a default value that lies outside the certificate runtime.
The certificate is then marked as invalid.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 21
2 Engineering
You must import the CA certificate of the MQTT Broker into STEP 7 (TIA Portal).
In the TIA Portal, the certificates are managed in the global certificate manager.
The certificate manager contains an overview of all certificates used in the project.
In the certificate manager, for example, you can import new certificates and export,
renew, or replace existing certificates. Each certificate is assigned an ID that can
be used to reference the certificate in the program blocks.
2. In the area navigation of the "Properties" tab, select "Protection & Security >
Certificate Manager". Enable the option "Use global security settings for
certificate manager".
d
v
r
g
i
r
l
l
2
G
e
i
Result
The new entry "Security Settings" appears in the project navigation.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 22
2 Engineering
Logging on users
After you have enabled the global security settings for the certificate manager, you
must log in to the security settings. You cannot access the global certificate
manager without logging in.
Log on as a security user for the security settings as described below:
1. Double-click the entry "Settings" in the project navigation under "Security
settings".
2. The user administration editor opens and the project protection area is
displayed.
Click the "Protect this project" button.
d
3. This opens the dialog "Protect Project".
e
Enter a username and password.
v
r
t
The password must comply with the following guidelines:
h
g
i
r
l
– Password length: A minimum of eight characters, a maximum of 128
A
2
characters
0
A
– At least one upper-case letter
s
m
– At least one special character (special characters § and ß are not allowed)
e
i
♥
– At least one number
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 23
2 Engineering
Figure 2-4
d
v
r
g
i
r
l
l
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 24
2 Engineering
2. Select the appropriate registry for the certificate you want to import, for
example, "Trusted certificates and core certification authorities".
v
r
g
4. Select the export format of the certificate:
i
r
l
l
1
– CER, DER, CRT or PEM for certificates without a private key
2
G
– P12 (PKCS12 archive) for certificates with a private key.
A
e
Click on "Open" to import the certificate.
m
e
i
Result
The CA certificate of the MQTT Broker is now located in the global certificate
manager.
Figure 2-5
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 25
2 Engineering
Note If the MQTT Broker also requires authentication of the MQTT Client, you must
import the client certificate.
Observe the following information:
The CA certificate is currently only located in the global certificate manager of the
TIA Portal. Certificates imported via the certificate manager into the global security
settings are not automatically assigned to the corresponding modules.
To authenticate the MQTT Broker, you have to load the CA certificate into the
CPU. Only those device certificates that you have assigned to the module as
device certificates via the local certificate manager are loaded onto the module.
This assignment is made in the local security settings of the module in the entry
"Certificate manager" via the table editor "Device certificates". The certificates of
the global certificate manager are available for the certificate assignment.
d
The following steps show you how to assign the CA certificate from the global
e
certificate manager to the CPU.
v
r
e
1. In the Device or Network view select your CPU. The properties of the CPU are
r
h
displayed in the Inspector window.
g
i
r
l
l
A
2. To add the CA certificates, select the entry "Certificate manager" in the area
1
0
navigation of the "Properties" tab under "Protection & Security".
2
m
certificates. This inserts a new row into the table.
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 26
2 Engineering
d
v
r
g
i
r
l
l
n
4. Click in the new row. The selection for new certificates opens. Select the
e
e
previously imported CA certificate from the global certificate manager and click
i
♥
the green check mark.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 27
2 Engineering
Result
The selected certificate was assigned to the CPU and provided with an ID. The ID
is the number of the certificate. Enter this value in the connection parameters for
the "brokerCert" parameter (see Figure 2-2).
Figure 2-6
v
r
e
Note If the MQTT Broker additionally requires authentication of the MQTT Client, you
s
must also assign the imported client certificate to the CPU (section "Device
s
certificates"). Enter the value of the ID in the connection parameters for the
g
i
r
l
l
A
"clientCert" parameter (see Figure 2-2).
1
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 28
2 Engineering
Above all, you must enter your own value for the following parameters:
• Connection number
• IPv4 address or domain name of the MQTT Broker. The domain name must
end with a ".".
• Port on which the MQTT Broker receives the messages
– unsecured connection: remote port 1883
– secured connection: remote port 8883
• Parameters for secure communication
d
– Status of the security feature (On/Off) for this connection
e
v
r
e
– ID of the CA certificate (only relevant for a secure connection)
s
h
– ID of your own certificate, if the MQTT Broker also authenticates the client
g
i
r
l
(only relevant for a secure connection)
l
A
– Login information for the MQTT Broker
s
m
– Topic
e
i
♥
– Message text
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 29
2 Engineering
The following table shows the tags in the DB "MqttDb" to interconnect the inputs
and outputs of the FB "MQTT_Client".
Table 2-1
Tag Data type Note
v
r
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 30
2 Engineering
Tag Data type Note
v
r
e
s
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 31
2 Engineering
Tag Data type Note
v
r
g
i
r
l
l
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 32
2 Engineering
Table 2-2
Parameters Data type Note
v
r
g
i
r
l
l
e
i
♥
Note If the TCP connection will be established via the fully qualified domain name, you
must configure a DNS server in the CPU.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 33
2 Engineering
d
• MQTT message received for a subscribed topic: Trigger the input tag
e
v
r
e
"subscribe".
s
r
If the connection to the MQTT Broker is interrupted (status = 16#9000), the
s
t
h
g
connection is automatically re-established. After a disconnection, it is
i
r
l
l
A
necessary to perform a "subscribe" job for the subscribed topics.
1
2
• Unsubscribe yourself from subscribed topics: Trigger the tag "unsubscribe".
G
s
If the connection to the MQTT Broker is not established, check the output tag
n
m
"status" and "diagnostics" to diagnose the error.
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 34
2 Engineering
v
r
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 35
3 Useful Information
3 Useful Information
3.1 Fundamentals of MQTT
3.1.1 Terminology
The most important terms in the MQTT telemetry protocol are explained below.
MQTT message
A message with MQTT consists of several parts:
• A defined subject ("Topic")
• An assigned criterion for "Quality of Service"
• The message text
MQTT Client
An MQTT Client is a program or device that uses MQTT. A client always actively
establishes the connection to the broker. A client can perform the following
d
functions:
e
v
r
e
• Send messages with a defined subject ("Topic"), in which other clients might
s
s
be interested, to the MQTT Broker (Publish mechanism)
t
g
i
r
l
• Subscribe messages which follow a certain topic (Subscriber mechanism) at
l
2
the MQTT Broker
0
e
i
Note The function block "LMQTT_Client" in this application example supports the
following functions:
• Logging into the MQTT Broker
• Publish mechanism
• Subscribe and unsubscribe mechanisms
• Ping mechanism
• Unsubscribe from the MQTT Broker.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 36
3 Useful Information
MQTT Broker
An MQTT Broker is the central component of MQTT and can be a program or a
device. The MQTT Broker acts as an intermediary between the sending MQTT
Client and the subscribing MQTT Client. The MQTT Broker manages the topics
including the messages contained therein and regulates the access to the topics.
The MQTT Broker has the following functions:
• Accept network connections from the MQTT Clients
• Receive messages from an MQTT Client
• Edit subscription requests from MQTT Clients
• Forward messages to the MQTT Clients that match your subscription
Note The MQTT Broker is not part of this application example and is assumed to be
given.
Topics
MQTT messages are organized in topics. A topic "describes" a subject area. The
topics can be subscribed to by the MQTT Clients (subscriber mechanism). The
sender of a message (Publisher mechanism) is responsible for defining content
d
and topic when sending the message. The broker then takes care that the
e
Subscribers get the news from the subscribed topics. The topics follow a defined
v
r
s
scheme. They are similar to a directory path and represent a hierarchy.
e
g
i
r
l
l
2
3.1.2 Standard and Architecture
0
ISO standard
n
e
i
S
MQTT defines an OASIS or ISO standard (ISO/IEC PRF 20922).
♥
Depending on the security protocols used, MQTT runs on different access ports.
Ports offered are:
• 1883: MQTT, unencrypted
• 8883: MQTT, encrypted
• 8884: MQTT, encrypted, Client Certificate required
• 8080: MQTT via WebSockets, unencrypted
• 8081: MQTT via WebSockets, encrypted
Architecture
The MQTT is a publish and subscribe protocol. This mechanism decouples a client
sending messages (Publishers) from one or more clients receiving the messages
(Subscribers). This also means that the "Publishers" know nothing about the
existence of the "Subscribers" (and vice versa).
There is a third component in the MQTT architecture, the MQTT Broker. The
MQTT Broker is located between "Publisher" and "Subscriber". The MQTT Broker
controls the communication.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 37
3 Useful Information
3.1.3 Features
Quality of Service
The MQTT specification provides three service qualities for message transmission
quality assurance:
• QoS "0": The lowest level 0 is a "fire'n'forget" method. This means that there is
no guarantee that the message will arrive at all.
• QoS "1": The QoS level 1 ensures that the message ends up in the topic queue at
least once. The MQTT Broker acknowledges receipt of the message.
• QoS "2": In the highest level 2, the MQTT Broker guarantees by multiple
handshake with the MQTT Client that the message is exactly filed once.
Last will
MQTT supports the "Last Will and Testament" feature. This feature is used to notify
other MQTT Clients if the connection to a MQTT Client has been disconnected
accidentally.
Each MQTT Client can specify its last will while connecting to the MQTT Broker
and notify the MQTT Broker. This last will is built like a normal MQTT message,
including topic, QoS and payload. The MQTT Broker saves the last will. As soon as
the MQTT Broker notices that the connection with the MQTT Client in question has d
e
been abruptly terminated, the MQTT Broker sends the last will as an MQTT
v
r
e
s
message to all subscribers who have registered for the topic. In this way, the
e
s
subscribers also learn that the MQTT Client has been disconnected.
t
g
i
r
l
l
1
Keep-Alive
2
G
MQTT supports the "Keep-Alive" feature. This ensures that the connection is still
A
n
open and the MQTT Client and MQTT Broker are connected.
e
e
i
For the Keep-Alive, the MQTT Clients define a time interval and communicate it to
S
♥
the MQTT Broker during their connection setup. This interval is the largest possible
tolerated time period in which the MQTT Client and the MQTT Broker may remain
without contact. If the time is exceeded, the MQTT Broker must disconnect.
That means that, as long as the MQTT Client periodically sends messages to the
broker within the Keep-Alive interval, the MQTT Client does not need to take any
special action to maintain the connection. However, if the MQTT Client does not
send any messages within the Keep-Alive interval, they must ping the MQTT
Broker before the deadline expires. With this ping, the MQTT Client signals to the
MQTT Broker that it is still available.
When a message or a ping packet has been sent to the MQTT Broker, timing for
the Keep-Alive interval begins again.
Note • The client determines the Keep-Alive interval. It can therefore adjust the
interval of his environment, e.g. because of a slow bandwidth.
• The maximum value for the Keep-Alive interval is 18 h 12 m 15 s.
• When the client sets the Keep-Alive interval to "0", the Keep-Alive
mechanism is disabled.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 38
3 Useful Information
Message persistence
If the connection to an MQTT Client is interrupted, the broker can cache new
messages for this client for later delivery.
Retained messages
The first time an MQTT Client subscribes to a topic, it usually gets a message only
when another MQTT Client sends a message with the subscribed topic the next
time. With "Retained messages", the subscriber receives the last value sent to the
topic prior to its subscription request, delivered immediately.
d
v
r
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 39
3 Useful Information
Fixed header
Mandatory for all control packages
Variable header
Mandatory for some control packages
Payload
Mandatory for all control packages
v
r
g
i
r
l
l
1
The "Variable header" is required only for some control packets. The content of the
2
0
variable header depends on the control packet type.
2
The payload is mandatory for most control packets. Again, the content depends on
s
m
the control packet type. For each type of control packet, there are clear rules with
e
i
S
what and in what order the payload can be filled.
♥
Note A detailed description of MQTT control packets can be found in the MQTT
specification description (see \3\ in section 4.3).
The MQTT control packets from this application example are briefly explained
below.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 40
3 Useful Information
An MQTT connection is always made between an MQTT Client and the MQTT
Broker. A direct client-client connection is not possible.
The connection is initiated by an MQTT Client as soon as the MQTT Client sends a
"CONNECT" packet to the MQTT Broker. If positive, the MQTT Broker replies with
a "CONNACK" packet and a status code.
The MQTT Broker immediately closes the connection in the following cases:
• If the "CONNECT" packet is faulty
• If the structure of the "CONNECT" packet does not meet the specification
• If the connection takes too long
Bit 7 6 5 4 3 2 1 0
Byte 2 Remaining length: The number of following bytes after the "fixed
header" = "variable header" + "payload"
v
r
g
i
r
l
l
A
A "CONNECT" packet contains the following areas in the "variable header":
1
0
2
1. Report name: The report name "MQTT" is transmitted as UTF-8 string.
G
s
2. Report level: 4 (dec)
n
e
3. Connect flags: The "Connect Flags" byte contains a number of parameters that
i
♥
specify the behavior of the MQTT connection. In addition, the "Connect Flags"
byte also shows which optional fields are present in the "payload" or not. The
connection type can be regulated with the "Clean Session" flag.
4. Keep alive: The Keep-Alive time determines the time interval in which the
MQTT Client is obligated to report to the MQTT Broker. This can be done
either by sending a message or a PING command. If the client does not report
in the time interval, the MQTT Broker disconnects from the client.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 41
3 Useful Information
Table 3-2 shows the structure of the "variable header" of the "CONNECT" packet.
Table 3-2
Variable header
Bit 7 6 5 4 3 2 1 0
Report name
Byte 3 'M'
0 1 0 0 1 1 0 1
Byte 4 'Q'
0 1 0 1 0 0 0 1
Byte 5 'T'
0 1 0 1 0 1 0 0
Byte 6 'T'
0 1 0 1 0 1 0 0
Report level
Connect flags
Byte 8 User Passw Will Will QoS flag Will Clean Reserve
name ord retain flag sessi
flag flag flag on
flag
Keep alive
v
r
g
i
r
l
l
i
In "Payload" the existing fields appear in the following order:
S
• Client ID: The client ID is used to identify the client at the MQTT Broker. The
client ID must appear as the first field in the "Payload".
• Will topic: The field appears optionally if the "Will" flag is set to "TRUE".
• Will message: The field appears optionally if the "Will" flag is set to "TRUE".
• Username: The field appears optionally if the "Username" flag is set to "TRUE". •
Password: The field appears optionally if the "Password" flag is set to "TRUE".
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 42
3 Useful Information
Bit 7 6 5 4 3 2 1 0
Byte 2 Remaining length: The number of following bytes after the "fixed
header" = "variable header" = 2 bytes
Table 3-4 shows the structure of the "variable header" of the "CONNACK" packet.
Table 3-4
Variable header
Bit 7 6 5 4 3 2 1 0
Byte 2 • 0x00 = The MQTT Broker accepts the connection. The MQTT Broker
does not support the level of the MQTT protocol requested by the
client.
• 0x01 = The MQTT Broker does not support the level of the MQTT
protocol requested by the MQTT Client.
• 0x02: The MQTT Broker does not allow the client ID.
• 0x03: The MQTT service is not available.
• 0x04: The data in the username and password are incorrect.
• 0x05: The MQTT Client is not authorized to connect.
d
v
r
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 43
3 Useful Information
Once an MQTT Client connects to the MQTT Broker, it can send messages to the
MQTT Broker. To do this, the client uses the "PUBLISH" packet. Because MQTT
messages are filtered and managed based on topics, each MQTT message must
contain a topic. The topic is part of the "Variable Header". The actual message text
is contained in the "payload".
"PUBLISH" packet
Table 3-5 shows the structure of the "fixed header" of the "PUBLISH" packet.
Table 3-5
Fixed header
Bit 7 6 5 4 3 2 1 0
0 0 1 1 X X X X
Byte 2 Remaining length: The number of following bytes after the "fixed
header" = "variable header" + payload
d
Depending on the quality assurance setting ("QoS"), the push mechanism ends at
e
v
this point or other control packets are exchanged:
r
• QoS = 0 (dec): The message will be sent only once. The send job ends here.
r
g
i
• QoS = 1 (dec): The message will be sent at least once. The MQTT Broker
r
l
l
1
acknowledges the "PUBLISH" packet with a "PUBACK" packet.
2
G
• QoS = 2 (dec): The message will be sent exactly once. The MQTT Broker
A
n
acknowledges the "PUBLISH" packet with a "PUBREC" packet. This is
e
e
followed by another handshake between MQTT Client and MQTT Broker. The
i
♥
client answers the "PUBREC" packet with a "PUBREL" packet. The MQTT
Note You can find further information on Quality Assurance QoS in section 3.1.3.
The "variable header" of the "Publish" packet contains the following fields:
• Name of the topic
• Packet ID
Bit 7 6 5 4 3 2 1 0
0 1 0 0 0 0 0 0
Byte 2 Remaining length: The number of following bytes after the "fixed
header" = "variable header" = 2 bytes
The "variable header" of the "PUBACK" packet contains the packet ID.
The "PUBACK" packet has no "payload".
v
r
e
Table 3-7 shows the structure of the "fixed header" of the "PUBREC" packet.
s
s
Table 3-7
t
g
i
r
Fixed header
Bit 7 6 5 4 3 2 1 0
0 1 0 1 0 0 0 0
Byte 2 Remaining length: The number of following bytes after the "fixed
header" = "variable header" = 2 bytes
l
l
e
i
The "variable header" of the "PUBREC" packet contains the packet ID.
The "PUBREC" packet has no "payload".
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 45
3 Useful Information
Bit 7 6 5 4 3 2 1 0
0 1 1 0 0 0 1 0
Byte 2 Remaining length: The number of following bytes after the "fixed
header" = "variable header" = 2 bytes
Note The reserve bits in the "fixed header" must be set as follows:
• Bit 3 = 0
• Bit 2 = 0
• Bit 1 = 1
• Bit 0 = 0
e
The "variable header" of the "PUBREL" packet contains the packet ID.
v
r
e
The "PUBREL" packet has no "payload".
r
g
i
r
l
l
0
"PUBCOMP" packet (Publish Complete)
2
The MQTT Broker responds to the "PUBREL" packet with the "PUBCOMP" packet.
s
e
Table 3-9 shows the structure of the "fixed header" of the "PUBCOMP" packet.
i
Table 3-9
Fixed header
Bit 7 6 5 4 3 2 1 0
0 1 1 1 0 0 0 0
Byte 2 Remaining length: The number of following bytes after the "fixed
header" = "variable header" = 2 bytes
The "variable header" of the "PUBCOMP" packet contains the packet ID.
The "PUBCOMP" packet has no "payload".
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 46
3 Useful Information
Once an MQTT Client has connected to the MQTT Broker, it can create or
unsubscribe from subscriptions.
"SUBSCRIBE" packet
To create a subscription, the MQTT Client uses the "SUBSCRIBE" packet. A list of
the topics that the MQTT Client would like to subscribe to is stored in the
"Payload".
Table 3-10 shows the structure of the "fixed header" of the "SUBSCRIBE" packet.
Table 3-10
Fixed header
Bit 7 6 5 4 3 2 1 0
1 0 0 0 0 0 1 0
Byte 2 Remaining length: The number of following bytes after the "fixed
header" = "variable header" + "payload"
e
Note The reserve bits in the "fixed header" must be set as follows:
v
r
s
• Bit 3 = 0
t
g
i
• Bit 2 = 0
r
l
l
• Bit 1 = 1
1
• Bit 0 = 0
G
e
i
♥
The "variable header" of the "SUBSCRIBE" packet contains the packet ID.
Table 3-11 shows the structure of the "payload" of the "SUBSCRIBE" packet.
Table 3-11
Payload
Bit 7 6 5 4 3 2 1 0
Topic name
Byte 1 MSB length
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 47
3 Useful Information
Bit 7 6 5 4 3 2 1 0
1 0 0 1 0 0 0 0
Byte 2 Remaining length: The number of following bytes after the "fixed
header" = "variable header" + "payload"
The "variable header" of the "SUBACK" packet contains the packet ID.
Table 3-13 shows the structure of the "payload" of the "SUBACK" packet.
Table 3-13
Payload
Bit 7 6 5 4 3 2 1 0
Return code
v
r
s
e
g
i
r
l
l
e
i
♥
"UNSUBSCRIBE" packet
Bit 7 6 5 4 3 2 1 0
1 0 1 0 0 0 1 0
Byte 2 Remaining length: The number of following bytes after the "fixed
header" = "variable header" + "payload"
Note The reserve bits in the "fixed header" must be set as follows:
• Bit 3 = 0
• Bit 2 = 0
• Bit 1 = 1
• Bit 0 = 0
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 48
3 Useful Information
The "variable header" of the "UNSUBSCRIBE" packet contains the packet ID.
Table 3-15 shows the structure of the "payload" of the "UNSUBSCRIBE" packet.
Table 3-15
Payload
Bit 7 6 5 4 3 2 1 0
Topic name
"UNSUBACK" packet
The MQTT Broker responds to the "UNSUBSCRIBE" packet with the
"UNSUBACK" packet.
Table 3-16 shows the structure of the "fixed header" of the "UNSUBACK" packet.
Table 3-16
Fixed header
Bit 7 6 5 4 3 2 1 0
1 0 1 1 0 0 0 0
Byte 2 Remaining length: The number of following bytes after the "fixed
header" = "variable header" = 2 bytes.
v
r
g
i
r
l
l
e
i
The "variable header" of the "UNSUBACK" packet contains the packet ID.
The "UNSUBACK" packet has no "payload".
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 49
3 Useful Information
If the Keep-Alive interval is greater than "0", the Keep-Alive function is active. If the
Keep-Alive function is active, the MQTT Client must send at least one message to
the MQTT Broker within the Keep-Alive interval. If this is not the case, the MQTT
Broker must terminate the connection to the MQTT Client. To prevent this type of
forced abort, the MQTT Client must ping the MQTT Broker before the Keep-Alive
time expires. The control packet "PINGREQ" is used for this.
"PINGREQ" packet
Table 3-17 shows the structure of the "fixed header" of the "PINGREQ" packet
Table 3-17
Fixed header
Bit 7 6 5 4 3 2 1 0
1 1 0 0 0 0 0 0
Byte 2 Remaining length: The number of following bytes after the "fixed header"
= 0 bytes.
d
The "PINGREQ" packet has no "variable header" and no "payload".
e
v
r
h
"PINGRESP" packet
g
i
r
l
l
1
The MQTT Broker responds to the "PINGREQ" packet with the "PINGRESP"
2
2
packet and thus signals its availability to the MQTT Client.
G
e
i
S
Note This application example assumes an active Keep-Alive function. The Keep-Alive
♥
Table 3-18 shows the structure of the "fixed header" of the "PINGRESP" packet.
Table 3-18
Fixed header
Bit 7 6 5 4 3 2 1 0
1 1 0 1 0 0 0 0
Byte 2 Remaining length: The number of following bytes after the "fixed header"
= 0 bytes.
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 50
3 Useful Information
"DISCONNECT" packet
Table 3-19 shows the structure of the "fixed header" of the "DISCONNECT"
packet.
Table 3-19
Fixed header
Bit 7 6 5 4 3 2 1 0
1 1 1 0 0 0 0 0
Byte 2 Remaining length: The number of following bytes after the "fixed header"
= 0 bytes.
v
r
s
The "DISCONNECT" packet has no "variable header" and no "payload".
e
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 51
3 Useful Information
Note An MQTT connection setup is only possible if the TCP connection to the MQTT
Broker is successfully established and then maintained.
An MQTT message or Keep-Alive can only be sent if there is a TCP and MQTT
connection to the MQTT Broker.
d
Overview
e
v
r
s
To fulfill the mentioned requirements, several state machines were realized in the
e
s
program:
t
g
i
r
2
of the TCP connection
0
e
connection, the sending and receiving process
m
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 52
3 Useful Information
d
• FB_STATE_DISABLING
e
v
r
g
i
r
l
l
A
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 53
3 Useful Information
v
r
g
i
r
l
l
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 54
3 Useful Information
v
r
g
i
r
l
l
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 55
3 Useful Information
d
• MQTT_COMMAND_STATE_SEND_SUBSCRIBE
e
• MQTT_COMMAND_STATE_BUILD_UNSUBSCRIBE
v
r
• MQTT_COMMAND_STATE_SEND_UNSUBSCRIBE
e
h
g
• MQTT_COMMAND_STATE_SEND_PING
i
l
l
• MQTT_COMMAND_STATE_PING_RESP
1
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 56
3 Useful Information
Table 3-22
State Description
v
r
g
i
r
l
l
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 57
3 Useful Information
State Description
v
r
g
i
r
l
l
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 58
3 Useful Information
State Description
v
r
g
i
r
l
l
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 59
3 Useful Information
The following figure shows the diagram of the operation with the three state
machines.
Figure 3-2
enable
enable MQTT_CONNECT_STATE_
0 -> 1 BUILD_CONNECT
FB_STATE_NO_
PROCESSING
FB_STATE_ MQTT_CONNECT_STATE_SE
DISABLING TSEND_C. ND_
enable PACKET_WAIT_FOR_CONNA
MQTT_STATE_MACHINE
CK
1 -> 0enable=0
CTING CONNACK. SUCCESS
ERROR
FB_STATE_TCP_CONNE CONNACK. ERROR
l
l
12
TSEND_C.DONE
0
d
G FB_STATE_OPERATING_
e
v
A
MONITOR_TCP
r
s
e
n
s
e
e
r m
MQTT_STATE_MACHINE
e
s i
t
S
h
♥
g
i
r Connection interrupted
been established
MQTT_CONNECT_STATE_
FB_STATE_RECONNECTIN
G CONNECTED
TSEND_C.DONE
Connection has
been established
Connection has
MQTT_CONNECT_STATE_CONNECTED
subscribe or
unsubsribe
KeepAlive-Timer
publish=1
Keep alive or subscribe=1 unsubscribe=1
publish or
STATE_PING_RESP MQTT_COMMAND_STATE _ BUILD_UNSUBSCRIBE
MQTT_COMMAND_ _ BUILD_SUBSCRIBE
MQTT_COMMAND_
STATE_BUILD_PUBLISH
STATE_SEND_PING MQTT_COMMAND_STATE
MQTT_COMMAND_STATE _ SEND_UNSUBSCRIBE
MQTT_COMMAND_ _ SEND_SUBSCRIBE
MQTT_COMMAND_
STATE_SEND_PUBLISH MQTT_COMMAND_STATE
MQTT_COMMAND_NO_PRCESSING
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 60
4 Appendix
4 Appendix
4.1 Service and support
Industry Online Support
Do you have any questions or need assistance?
Siemens Industry Online Support offers round the clock access to our entire
service and support know-how and portfolio.
The Industry Online Support is the central address for information about our
products, solutions and services.
Product information, manuals, downloads, FAQs, application examples and videos
– all information is accessible with just a few mouse clicks:
support.industry.siemens.com
Technical Support
The Technical Support of Siemens Industry provides you fast and competent
support regarding all technical queries with numerous tailor-made offers
– ranging from basic support to individual support contracts.
Please send queries to Technical Support via Web form:
support.industry.siemens.com/cs/my/src
d
e
SITRAIN – Digital Industry Academy
v
r
r
We support you with our globally available training courses for industry with
s
g
practical experience, innovative learning methods and a concept that’s tailored to
i
r
l
l
A
the customer’s specific needs.
1
2
For more information on our offered trainings and courses, as well as their
G
A
locations and dates, refer to our web page:
s
m
siemens.com/sitrain
e
i
Service offer
Our range of services includes the following:
• Plant data services
• Spare parts services
• Repair services
• On-site and maintenance services
• Retrofitting and modernization services
• Service programs and contracts
You can find detailed information on our range of services in the service catalog
web page:
support.industry.siemens.com/cs/sc
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 61
4 Appendix
v
r
g
i
r
l
l
e
i
♥
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 62
4 Appendix
v
r
g
i
r
l
l
e
i
LMQTT_Client
Entry ID: 109748872, V3.0, 03/2021 63