MQTT_Receive

Overview

The MQTT_Receive function block checks whether new MQTT data has been received from the broker, based on the active subscriptions configured with Set_MQTT_Settings.

If a new message is available, the block makes the received payload accessible to the PLC application.

💡 How does it work?

On each call, the block checks whether new MQTT data has been received for the configured subscriptions.

If new data is available, the payload is copied into Payload and STS_Received is set to TRUE.

The received message remains available until it is cleared using CMD_Clear_Received. When CMD_Clear_Received is TRUE, the stored payload is cleared and STS_Received is reset.

CMD_Clear_Received  := (BOOL)
STS_Received        => (BOOL)
Payload             => (STRING)
STS_Done            => (BOOL)
STS_Failed          => (BOOL)
STS_Fault_Code      => (USINT)

Interface

Inputs

NameTypeRange / UnitsDescription
CMD_Clear_ReceivedBOOL0 / 1When TRUE, clears the stored received payload and resets the received flag.

Outputs

NameTypeRange / UnitsDescription
STS_ReceivedBOOL0 / 1TRUE when a received MQTT payload is available in Payload.
PayloadSTRINGReceived MQTT payload (empty when no message is available).
STS_DoneBOOL0 / 1TRUE when the block executed without internal error (module checks passed).
STS_FailedBOOL0 / 1TRUE if the operation failed due to module/slot/CPU state or internal error.
STS_Fault_CodeUSINT#Diagnostic code indicating the reason for failure.

Status Codes

CodeMeaningDescription
95Buffer size invalidInternal error: invalid destination buffer size (should not occur).
96Unknown errorUnknown error occurred during operation.
97Module FaultedCOMMS module present but not active (faulted / not responding).
98Module mismatchSlot 1 is populated but not a COMMS module.
99Module not foundCOMMS module is not part of the current hardware configuration.

Example

PROGRAM PROG_Set_Ethernet_Config
    VAR
        EthCfg : SET_ETHERNET_CONFIG;
        _IP_Address  : ARRAY [0..3] OF USINT := [192, 168, 11, 2];
        _Subnet_Mask : ARRAY [0..3] OF USINT := [255, 255, 255, 0];
        _Gateway     : ARRAY [0..3] OF USINT := [192, 168, 11, 1];
        _DNS         : ARRAY [0..3] OF USINT := [8, 8, 8, 8];
    END_VAR

    EthCfg(
        Network_Operation_Mode := 2,
        IP_Allocation_Method   := 0,
        IP_Address             := _IP_Address,
        Subnet_Mask            := _Subnet_Mask,
        Gateway                := _Gateway,
        DNS                    := _DNS,
        Port                   := 5000
    );
END_PROGRAM

PROGRAM PROG_Set_MQTT_Settings
    VAR
        MqttCfg : SET_MQTT_SETTINGS;

        _Broker_IP : ARRAY [0..3] OF USINT := [192, 168, 11, 25];
        _Port      : INT := 1883;
        _User      : STRING := 'NewUser';
        _Pass      : STRING := 'NewPassword';
        _ClientID  : STRING := 'PLC_01';
        _Keep_Alive : UINT := 60;
        _QoS       : SINT := 0;

        _PubTopic  : STRING := 'monolitix/pub';
        _Sub1      : STRING := 'monolitix/sub1';
        _Sub2      : STRING := 'monolitix/sub2';
        _Sub3      : STRING := 'monolitix/sub3';
    END_VAR

    MqttCfg(
        Broker_IP        := _Broker_IP,
        Port             := _Port,
        User_Name        := _User,
        Password         := _Pass,
        Client_ID        := _ClientID,
        Keep_Alive       := _Keep_Alive,
        QoS              := _QoS,
        Publish_Topic    := _PubTopic,
        Subscribe_Topic1 := _Sub1,
        Subscribe_Topic2 := _Sub2,
        Subscribe_Topic3 := _Sub3
    );
END_PROGRAM

PROGRAM MqttReceive
    VAR
        MR_Instance : MQTT_Receive;

        _RxPayload        : STRING;   (* local copy of received payload *)
        _CMD_Clear       : BOOL := FALSE;
        _LastReceived     : BOOL := FALSE;
    END_VAR

    (* Call receive FB *)
    MR_Instance(
        CMD_Clear_Received := _CMD_Clear,
        STS_Received       => _LastReceived,
        Payload            => _RxPayload
    );
END_PROGRAM

CONFIGURATION Config0
    RESOURCE Res0 ON PLC
        TASK task1_50ms(INTERVAL := T#50ms, PRIORITY := 2);
        PROGRAM Prog_Instance_SET_ETHERNET_CONFIG WITH task1_50ms : PROG_Set_Ethernet_Config;
        PROGRAM Prog_Instance_SET_MQTT_SETTINGS WITH task1_50ms : PROG_Set_MQTT_Settings;
        PROGRAM Prog_Instance_MqttReceive WITH task1_50ms : MqttReceive;
    END_RESOURCE
END_CONFIGURATION