MQTT_Disconnect block

Short summary

Name

MQTT_Disconnect

→POU type

→function

Category

more system blocks, MQTT, block with internal error diagnostic

Graphical interface

images/download/thumbnails/443058797/MQTT_Disconnect-version-1-modificationdate-1589783792568-api-v2.png

Available since

version 2.0.17 (for library System) and version 4.5.0 of the →runtime system

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/warning.svg This block is supported for the built-in PLC and →Raspberry Pi.

Functionality

The block disconnects from a →MQTT broker using the connection handler received earlier by calling the MQTT_Connect block.

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/information.svg The data is transferred by means of an already existing MQTT broker

(see "Preparing/Realizing data transfer via MQTT")
.

Inputs, outputs, return value


Identifier

→Data type

Description

Inputs:

ch

DINT

connection handler (as obtained by the MQTT_Connect block)

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.

See:

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 MQTTConnectDisconnectExample
VAR
connection_handler : DINT := -1;
return_code : MQTT_RC;
subscriber_state : MQTT_SUBSCRIBER_STATE := MQTT_SUBSCRIBER_STATE#INVALID;
connect_flag : BOOL := FALSE;
END_VAR
subscriber_state := MQTT_GetState(ch := connection_handler, rc => return_code, ENO => ENO);
/* If the connect flag is set... */
IF connect_flag THEN
/* ...make sure the client is connected 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;
/* ...and if it is connected... */
IF subscriber_state = MQTT_SUBSCRIBER_STATE#CONNECTED THEN
/* ...deal with MQTT topics and messages inside this block */
END_IF;
/* If the connect flag is NOT set... */
ELSE
/* ...and the client is connected to a broker... */
IF subscriber_state = MQTT_SUBSCRIBER_STATE#CONNECTED THEN
/* ...disconnect the client from this broker */
MQTT_Disconnect(ch := connection_handler, rc => return_code, ENO => ENO);
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.