MQTT_Publish block
Short summary
Name |
MQTT_Publish |
→POU type |
|
Category |
|
Graphical interface |
|
Available since |
This block is supported for →Raspberry Pi. |
Functionality
The block publishes a message for a given topic via the connection that is specified by the connection handler. The messages are published asynchronously.
The data is transferred by means of an already existing MQTT broker
Inputs, outputs
|
Identifier |
Description |
|
Inputs: |
ch |
DINT |
connection handler (as obtained by the MQTT_Connect block) |
topic |
STRING |
topic for the message |
|
payload |
REF_TO BYTE |
payload of the message |
|
len |
DINT |
length of the payload to publish |
|
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.
Example for usage within ST-editor
PROGRAM Program1
VAR
ioImageLocal : ARRAY[0..1023] OF BYTE;
ioImageLocalLen : DINT := 2;
ch : DINT := -1;
cnt : DINT := 0;
rc : MQTT_RC;
state : MQTT_SUBSCRIBER_STATE := MQTT_SUBSCRIBER_STATE#INVALID;
END_VAR
state := MQTT_GetState(ch := ch, rc => rc, ENO => ENO);
/* make sure MQTT connection is established and messages are received */
IF state <> MQTT_SUBSCRIBER_STATE#CONNECTING AND state <> MQTT_SUBSCRIBER_STATE#CONNECTED THEN
ch := MQTT_Connect(address := '192.168.1.107', clientId := 'RTS1', rc => rc, ENO => ENO);
END_IF;
IF ENO AND state = MQTT_SUBSCRIBER_STATE#CONNECTED THEN
/* prepare data to be transferred over MQTT */
ioImageLocal[0] := 16#CA;
ioImageLocal[1] := 16#FE;
/* send updated io image to remote */
MQTT_Publish(ch := ch, topic := 'sample_topic',
payload := REF(ioImageLocal[0]),
len := ioImageLocalLen,
rc => rc);
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.