MQTT_Publish

Overview

The MQTT_Publish function block publishes an MQTT message to the broker, using the configured settings from Set_MQTT_Settings.

💡 How does it work?

While CMD_Send is TRUE, the function block attempts to publish the message specified in Payload to the configured topic.

This means that with each call of the function block, a new publish request is queued and sent to the broker.

To publish messages only when the transmitted values change, the CMD_SEND trigger must be conditioned by detecting differences in the values being sent.

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

Interface

Inputs

NameTypeRange / UnitsDescription
CMD_SendBOOL0 / 1While TRUE, the block attempts to queue the publish command.
PayloadSTRINGMust not be empty. Payload is truncated to fit internal buffer limits.

Outputs

NameTypeRange / UnitsDescription
STS_DoneBOOL0 / 1TRUE when the block is operational and the command is accepted/queued (or no send requested).
STS_FailedBOOL0 / 1TRUE if the operation failed due to state, validation, or module/AT-layer conditions.
STS_Fault_CodeUSINT#Diagnostic code indicating the reason for failure.

Status Codes

CodeMeaningDescription
90Not in gateway modeBlocked because the communication module is in configuration mode.
91Wrong operation modeNetwork_Operation_Mode is wrongly configured using Set_Ethernet_Config.
92AT busyAT layer is busy (tx_request = 1). Try again next PLC cycle.
93Empty payloadPayload.len == 0 is not allowed.
94Queue failedFailed to queue the command using.
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 MqttPublish
    VAR
        MP_Instance : MQTT_Publish;

        _Payload    : STRING;
        DINT_VALUE2 : DINT := 0;
        Slot2_Ch0   : BOOL := FALSE;
    END_VAR

    DINT_VALUE2 := DINT_VALUE2 + 2;


    _Payload := '{ "DINT_VALUE": ';
    _Payload := CONCAT(_Payload, DINT_TO_STRING(DINT_VALUE2));
    _Payload := CONCAT(_Payload, ' }');

    MP_Instance(
        CMD_Send := TRUE,
        Payload  := _Payload
    );
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_MqttPublish WITH task1_50ms : MqttPublish;
    END_RESOURCE
END_CONFIGURATION