The function block FB_MqttSubscribe is used to manage a subscription to a specific topic on the MQTT server.
The following functions are supported:
Subscription of the specified topic
Unsubscription of the specified topic
Reading out of the data according to the subscribed topic
The function block uses the connection to the MQTT server that was previously established with the FB_MqttClient.
After the function block is activated, the subscription for the specified topic is sent to the connected MQTT server. The status of the subscription and thus the possibility of receiving data is indicated by output q_xReady. New received data are indicated by the output q_xNewMessage.
Disabling the function block sends the unsubscription to the previously subscribed topic. The state of the unsubscription is indicated by the output q_xActive. A new subscription is only allowed if the output q_xActive is set to FALSE.
After the specified topic is successfully subscribed, the data received to this topic can be read by setting the command i_xExecute to TRUE.
Subscribe level |
Description |
---|---|
QoS 0 |
The last data on the subscribed topic can be read. |
QoS 1 |
|
Input |
Data type |
Description |
---|---|---|
i_xEnable |
BOOL |
The function block sends a subscription to the specified topic to the connected MQTT server upon a rising edge of this input. Refer to Behavior of Function Blocks with the Inputs i_xEnable and i_xExecute and i_xErrorQuit. |
i_stSubscribeTopic |
ST_SubscribeTopic |
Structure to specify the topic to subscribe. |
i_xExecute |
BOOL |
The function block reads the latest received application message to the subscribed topic upon a rising edge of this input. Refer to Behavior of Function Blocks with the Inputs i_xEnable and i_xExecute and i_xErrorQuit. |
i_xErrorQuit |
BOOL |
The function block acknowledges a detected error indicated by q_xError upon a rising edge of this input. Refer to Behavior of Function Blocks with the Inputs i_xEnable and i_xExecute and i_xErrorQuit. |
i_pbyBuffer |
POINTER TO BYTE |
Pointer to the buffer in which the received message is copied. |
i_udiBufferSize |
UDINT |
Size of the buffer in bytes.
NOTE: The length must not be greater than the size of the variable to which i_pbyBuffer points to.
|
i_stTimeout |
ST_Timeout |
Structure to specify the timeouts. |
Innput/Output |
Data type |
Description |
---|---|---|
iq_fbMqttClient |
FB_MqttClient |
Reference to the associated FB_MqttClient used for the data exchange with the MQTT server. |
Output |
Data type |
Description |
---|---|---|
q_xActive |
BOOL |
Indicates that the execution of the function block is active. As long this output is TRUE, the function block must be executed cyclically. |
q_xReady |
BOOL |
Indicates that the subscription to the specified topic is successfully sent to the MQTT server. |
q_xDone |
BOOL |
Indicates that the last received application message is successfully copied into the buffer specified with the input i_pbyBuffer. |
q_xBusy |
BOOL |
Indicates that the subscription is in progress. |
q_xError |
BOOL |
Indicates that an error was detected during the execution of the function block. |
q_etResult |
ET_Result |
Provides diagnostic and status information as a numeric value. |
q_sResultMsg |
STRING [80] |
Provides additional diagnostic and status information as a text message. |
q_xNewMessageAvailable |
BOOL |
Indicates that a new application message to the subscribed topic is available. |
q_udiMessageLength |
UDINT |
Indicates the number of bytes copied into the buffer specified with the input i_pbyBuffer. |
q_xTruncated |
BOOL |
If this output is set to TRUE, the copied application message has been truncated. |
The function block provides inputs and/or in/outputs of type POINTER TO… or REFENCE TO…. With the use of such a pointer or reference, the function block accesses the addressed memory area. In case of an online change event, it may happen that memory areas are moved to new addresses and in consequence a pointer or reference becomes invalid. To prevent errors associated with invalid pointers, variables of type POINTER TO… or REFERENCE TO… must be updated cyclically or at least at the beginning of the cycle in which they are used.
CAUTION | |
---|---|
Following UML sequence diagram illustrates the interaction with the function block FB_MqttClient, which needs to be called cyclically to process received messages and to detect a possible communication interruption to the server.