MQTT_Receive block
Short summary
Name |
MQTT_Receive |
→POU type |
|
Category |
|
Graphical interface |
|
Available since |
This block is supported for →Raspberry Pi. |
Functionality
The block pops one message from the receive buffer (a circular buffer) associated with the given connection handler.
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) |
payload |
REF_TO BYTE |
buffer where received payload can be stored |
|
payload_len |
DINT |
size of the payload buffer in bytes |
|
Outputs: |
len |
DINT |
length of the received payload in bytes |
|
rc |
MQTT_RC |
return code of involved MQTT functions as specified in data type MQTT_RC |
Return value: |
– |
STRING |
returns the topic of the received message |
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 Program2
VAR
ioImageRemote : ARRAY[0..1023] OF BYTE;
ioImageRemoteSize : DINT := 1024;
ioImageRemoteLen : DINT := -1;
topic : STRING[255];
ch : DINT := -1;
cnt : DINT := 0;
rc : MQTT_RC;
state : MQTT_SUBSCRIBER_STATE := MQTT_SUBSCRIBER_STATE#INVALID;
subscribed : BOOL := FALSE;
message_received : BOOL := FALSE;
END_VAR
state := MQTT_GetState(ch := ch, rc => rc, ENO => ENO);
IF state <> MQTT_SUBSCRIBER_STATE#CONNECTING AND state <> MQTT_SUBSCRIBER_STATE#CONNECTED THEN
ch := MQTT_Connect(address := '192.168.1.107', clientId := 'RTS2', rc => rc, ENO => ENO);
END_IF;
IF state = MQTT_SUBSCRIBER_STATE#CONNECTED THEN
/* connect to topic in case connection has been established */
IF NOT(subscribed) THEN
MQTT_Subscribe(ch := ch, topic := 'sample', rc => rc);
subscribed := rc = MQTT_RC#OK;
END_IF;
IF subscribed THEN
/* if subscribed, obtain only the latest message */
message_received := FALSE;
REPEAT
topic := MQTT_Receive(ch := ch,
payload := REF(ioImageRemote[0]),
payload_len := ioImageRemoteSize,
len => ioImageRemoteLen,
rc => rc);
IF rc <> MQTT_RC#OK THEN
EXIT;
END_IF;
message_received := TRUE;
UNTIL FALSE
END_REPEAT;
END_IF;
ELSE
/* if connection has been lost, subscription has been canceled as well*/
subscribed := FALSE;
END_IF;
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.