MQTT_Unsubscribe block
Short summary
Name |
MQTT_Unsubscribe |
→POU type |
|
Category |
more system blocks, MQTT, block with internal error diagnostic |
Graphical interface |
|
Available since |
version 2.0.17 (for library System) and version 4.5.0 of the →runtime system This block is supported for the built-in PLC and →Raspberry Pi. |
Functionality
The block unsubscribes an established connection determined by a connection handle from a given topic.
The data is transferred by means of an already existing MQTT broker
Inputs, outputs, return value
|
Identifier |
Description |
|
Inputs: |
ch |
DINT |
connection handler (as obtained by the MQTT_Connect block) |
topic |
STRING |
topic to unsubscribe from |
|
Outputs: |
rc |
MQTT_RC |
return code of involved MQTT functions as specified in data type MQTT_RC |
Input EN and output ENO are available when →calling the block. See "Execution control: EN, ENO" for information on input EN and output ENO.
Internal error diagnostic for block
The block checks the following error cases and – if they occur – the block sets the output ENO of the block to the value FALSE (or an equivalent):
An internal error occurred while sending a notification to the →runtime system .
The functionality of the block could not be executed.
Example for usage within ST-editor
PROGRAM MQTTSubscribeUnsubscribeExample
VAR
connection_handler : DINT := -1;
return_code : MQTT_RC;
subscriber_state : MQTT_SUBSCRIBER_STATE := MQTT_SUBSCRIBER_STATE#INVALID;
subscribed_to_topic : BOOL := FALSE;
subscribe_flag : BOOL := FALSE;
END_VAR
subscriber_state := MQTT_GetState(ch := connection_handler, rc => return_code, ENO => ENO);
/* Establish a MQTT connection to a broker */
IF subscriber_state <> MQTT_SUBSCRIBER_STATE#CONNECTING AND subscriber_state <> MQTT_SUBSCRIBER_STATE#CONNECTED THEN
connection_handler := MQTT_Connect(address := 'tcp://192.168.1.100:1883', clientId := 'RTS1', rc => return_code, ENO => ENO);
END_IF;
/* Check if the client is connected to the broker */
IF subscriber_state = MQTT_SUBSCRIBER_STATE#CONNECTED THEN
/* If the subscribe flag is set... */
IF subscribe_flag THEN
/* ...make sure the client is subscribed to a specific topic... */
IF NOT(subscribed_to_topic) THEN
MQTT_Subscribe(ch := connection_handler, topic := 'example-topic', rc => return_code, ENO => ENO);
subscribed_to_topic := return_code = MQTT_RC#OK;
ELSE
/* ...and publish or receive messages over MQTT inside this block */
END_IF;
/* If the subscribe flag is NOT set... */
ELSE
/* ...and the client is subscribed to a specific topic... */
IF subscribed_to_topic THEN
/* ...unsubscribe the client from this topic */
MQTT_Unsubscribe(ch := connection_handler, topic := 'example-topic', rc => return_code, ENO => ENO);
subscribed_to_topic := NOT(return_code = MQTT_RC#OK);
END_IF;
END_IF;
END_IF;
END_PROGRAM
When creating your application within the ST-editor, enter a call of a block by typing the text as requested by the syntax or use Content Assist.