Set_MQTT_Settings

Overview

The Set_MQTT_Settings function block configures the PLC MQTT client settings, including:

  • Broker IP Address
  • Port
  • User Name
  • Password
  • Client ID
  • Keep Alive
  • QoS
  • Publish Topic
  • Subscribe Topics (1..3)
💡 When are settings applied?

The MQTT configuration is applied upon successful execution of the function block. The settings are sent to the COMMS module and take effect as soon as the module is active on the network.

⚠️ Should the function only be called once?

No special care is required, like a first scan call to apply settings.
The function block is called whenever changes in the settings are detected.

Broker_IP[4]        := (USINT)
Port                := (INT)
User_Name           := (STRING)
Password            := (STRING)
Client_ID           := (STRING)
Keep_Alive          := (UINT)
QoS                 := (SINT)
Publish_Topic       := (STRING)
Subscribe_Topic1    := (STRING)
Subscribe_Topic2    := (STRING)
Subscribe_Topic3    := (STRING)
STS_Done            => (BOOL)
STS_Failed          => (BOOL)
STS_Fault_Code      => (USINT)

Interface

Inputs

NameTypeRange / UnitsDescription
Broker_IPARRAY[0..3] OF USINT0-255MQTT broker IPv4 address (e.g. [192,168,11,25]).
PortINT1-65535Broker port (typical: 1883 for MQTT, 8883 for MQTTS).
User_NameSTRINGMQTT username (can be empty if broker does not require auth).
PasswordSTRINGMQTT password (can be empty if broker does not require auth).
Client_IDSTRINGMQTT Client ID (can be empty if broker allows it).
Keep_AliveUINTsecondsMQTT keep-alive interval in seconds (typical: 60).
QoSSINT0-2Quality of Service: 0 = at most once, 1 = at least once, 2 = exactly once.
Publish_TopicSTRINGTopic used for publishing messages.
Subscribe_Topic1STRINGSubscribe topic #1 (optional).
Subscribe_Topic2STRINGSubscribe topic #2 (optional).
Subscribe_Topic3STRINGSubscribe topic #3 (optional).

Outputs

NameTypeRange / UnitsDescription
STS_DoneBOOL0 / 1TRUE when all configuration data has been successfully sent to the module.
STS_FailedBOOL0 / 1TRUE if the configuration data write operation failed.
STS_Fault_CodeUSINT#Diagnostic code indicating the reason for failure.

Status Codes

CodeMeaningDescription
90Invalid Broker IPBroker_IP is not a valid IPv4 address (e.g. 0.0.0.0 / 255.255.255.255).
91Invalid PortPort is outside the valid range (1..65535).
92Invalid QoSQoS is outside the valid range (0..2).
96Unknown errorUnknown error occurred during operation.
97Module FaultedThe COMMS module is 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_MQTT_Settings
    VAR
        MqttCfg : SET_MQTT_SETTINGS;

        _Broker_IP : ARRAY [0..3] OF USINT := [192, 168, 11, 25];
        _User      : STRING := 'NewUser';
        _Pass      : STRING := 'NewPassword';
        _ClientID  : STRING := 'PLC_01';

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

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

CONFIGURATION Config0
    RESOURCE Res0 ON PLC
        TASK task1_20ms(INTERVAL := T#20ms, PRIORITY := 1);
        PROGRAM Prog_Instance_SET_MQTT_SETTINGS WITH task1_20ms : PROG_Set_MQTT_Settings;
    END_RESOURCE
END_CONFIGURATION