Publish

Publish 1:

This method is called once, in order to send a message to the broker.

Possible errors are output at the outputs bError and hrErrorCode of the function block instance.

Syntax

METHOD Publish : BOOL
VAR_IN_OUT
    sTopic       : STRING; // topic string (UTF-8) with any length (attend that MQTT topics are case sensitive)
END_VAR
VAR_INPUT
    pPayload     : PVOID;
    nPayloadSize : UDINT;
    eQoS         : TcIotMqttQos; // quality of service between the publishing client and the broker
    bRetain      : BOOL; // if TRUE the broker stores the message in order to send it to new subscribers
    bQueue       : BOOL; // for future extension
    pProps       : POINTER TO MqttPublishProperties; // optional
END_VAR

Publish 2: Return value

Name

Type

Description

Publish

BOOL

The method returns the return value TRUE if the call was successful.

Publish 3: Inputs

Name

Type

Description

pPayload

PVOID

Address for the payload of the MQTT message

nPayloadSize

UDINT

Size of the payload in bytes

eQoS

TcIotMqttQos

"Quality of Service"

bRetain

BOOL

If bRetain is TRUE, the broker stores the message in order to make it available to subsequent subscribers.

bQueue

BOOL

Reserved parameter that can always be given the value FALSE.

pProps

POINTER TO MqttPublishProperties

Pointer to Publish properties to be sent.

The parameter is optional. The FB_IotMqtt5PublishProperties can be used to specify Publish properties.

Publish 4: Inputs/outputs

Name

Type

Description

sTopic

STRING

Topic of the MQTT message

Publish 5:

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.

Publish 6:

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.