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
| Name | Type | Range / Units | Description |
|---|---|---|---|
| CMD_Send | BOOL | 0 / 1 | While TRUE, the block attempts to queue the publish command. |
| Payload | STRING | — | Must not be empty. Payload is truncated to fit internal buffer limits. |
Outputs
| Name | Type | Range / Units | Description |
|---|---|---|---|
| STS_Done | BOOL | 0 / 1 | TRUE when the block is operational and the command is accepted/queued (or no send requested). |
| STS_Failed | BOOL | 0 / 1 | TRUE if the operation failed due to state, validation, or module/AT-layer conditions. |
| STS_Fault_Code | USINT | # | Diagnostic code indicating the reason for failure. |
Status Codes
| Code | Meaning | Description |
|---|---|---|
| 90 | Not in gateway mode | Blocked because the communication module is in configuration mode. |
| 91 | Wrong operation mode | Network_Operation_Mode is wrongly configured using Set_Ethernet_Config. |
| 92 | AT busy | AT layer is busy (tx_request = 1). Try again next PLC cycle. |
| 93 | Empty payload | Payload.len == 0 is not allowed. |
| 94 | Queue failed | Failed to queue the command using. |
| 96 | Unknown error | Unknown error occurred during operation. |
| 97 | Module Faulted | COMMS module 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_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