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
| Name | Type | Range / Units | Description |
|---|---|---|---|
| Broker_IP | ARRAY[0..3] OF USINT | 0-255 | MQTT broker IPv4 address (e.g. [192,168,11,25]). |
| Port | INT | 1-65535 | Broker port (typical: 1883 for MQTT, 8883 for MQTTS). |
| User_Name | STRING | — | MQTT username (can be empty if broker does not require auth). |
| Password | STRING | — | MQTT password (can be empty if broker does not require auth). |
| Client_ID | STRING | — | MQTT Client ID (can be empty if broker allows it). |
| Keep_Alive | UINT | seconds | MQTT keep-alive interval in seconds (typical: 60). |
| QoS | SINT | 0-2 | Quality of Service: 0 = at most once, 1 = at least once, 2 = exactly once. |
| Publish_Topic | STRING | — | Topic used for publishing messages. |
| Subscribe_Topic1 | STRING | — | Subscribe topic #1 (optional). |
| Subscribe_Topic2 | STRING | — | Subscribe topic #2 (optional). |
| Subscribe_Topic3 | STRING | — | Subscribe topic #3 (optional). |
Outputs
| Name | Type | Range / Units | Description |
|---|---|---|---|
| STS_Done | BOOL | 0 / 1 | TRUE when all configuration data has been successfully sent to the module. |
| STS_Failed | BOOL | 0 / 1 | TRUE if the configuration data write operation failed. |
| STS_Fault_Code | USINT | # | Diagnostic code indicating the reason for failure. |
Status Codes
| Code | Meaning | Description |
|---|---|---|
| 90 | Invalid Broker IP | Broker_IP is not a valid IPv4 address (e.g. 0.0.0.0 / 255.255.255.255). |
| 91 | Invalid Port | Port is outside the valid range (1..65535). |
| 92 | Invalid QoS | QoS is outside the valid range (0..2). |
| 96 | Unknown error | Unknown error occurred during operation. |
| 97 | Module Faulted | The COMMS module is present but not active (faulted / not responding). |
| 98 | Module mismatch | Slot 1 is populated but not a COMMS module. |
| 99 | Module not found | COMMS 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