FB_IotMqttMessage

If the TwintCAT MQTT client (FB_IotMqttClient) is used in combination with a message queue (FB_IotMqttMessageQueue), an MQTT message is represented by the function block FB_IotMqttMessage. Incoming messages are collected by the message queue and made available to the user in the form of a such a function block instance.

The topic, the payload size and the "Quality of Service" parameter of the MQTT message are immediately available as Properties at the function block output. The topic and the payload can easily be evaluated or copied via the provided methods CompareTopic(), GetTopic() and GetPayload().

FB_IotMqttMessage 1:

Message payload formatting

Note that the data type and the formatting of the content must be known to the sender and receiver side, particularly when binary information (alignment) or strings (with or without zero termination) are sent.

FB_IotMqttMessage 2:

Size of an MQTT message

The maximum size of an MQTT message to be received in the PLC depends on the hardware platform and should not exceed a few MB, even on higher-performance/larger platforms.

The maximum possible size of a message can be set using the cMaxSizeOfMqttMessage parameter in the parameter list of the Tc3_IotBase library. By default, the message size is limited to 100 kB.

When using the MQTT Message Queue, it dynamically allocates new memory from the TwinCAT Router memory for new messages.

FB_IotMqttMessage 3: Properties

Name

Type

Access

Description

eQoS

TcIotMqttQos

Get

"Quality of Service" of the MQTT message

nPayloadSize

UDINT

Get

Size of the payload in bytes

nTopicSize

UINT

Get

Size of the topic in bytes

FB_IotMqttMessage 4: Methods

Name

Description

CompareTopic()

Compares a specified topic with the topic in the MQTT message

GetTopic()

Returns the topic of an MQTT message

GetPayload()

Returns the content of an MQTT message

FB_IotMqttMessage 5:

Strings in UTF-8 format

The variables of type STRING used here are based on the UTF-8 format. This STRING formatting is common for MQTT communication as well as for JSON documents.

In order to be able to receive special characters and texts from a wide range of languages, the character set in the Tc3_IotBase and Tc3_JsonXml libraries is not limited to the typical character set of the data type STRING. Instead, the Unicode character set in UTF-8 format is used in conjunction with the data type STRING.

If the ASCII character set is used, there is no difference between the typical formatting of a STRING and the UTF-8 formatting of a STRING.

Further information on the UTF-8 STRING format and available display and conversion options can be found in the documentation for the Tc2_Utilities PLC library.

Requirements

Development environment

Target platform

PLC libraries to include

TwinCAT v3.1.4022.0

IPC or CX (x86, x64, ARM)

Tc3_IotBase