PublishString

TwinCAT TIA
Network MQTT Client ActiveClients One client

ns=2;s=DEPRAG Smart/Network/MQTT/ActiveClients/0/PublishString

The command sends a message with associated topic to the MQTT broker.

Parameters:

    QualityOfServiceLevel
    Configures the Quality of Service level of the transmission:
    AtMostOnce(0): The message is published exactly once and no acknowledgement of receipt from the broker is awaited.
    AtLeastOnce(1): The message is sent until the client receives an acknowledgement from the MQTT broker. This ensures that the message has arrived at the receiver at least once.
    ExactlyOnce(2): The message is transmitted exactly once from the client to the broker.
    WithRetainFlag
    Marks the Retain flag in the MQTT message. The broker is instructed to cache the last message sent to the topic. Clients that connect to the broker at a later time will receive this message.

Returns:

Examples:

In addition to the parameters already explained, the following variables must be created / transferred for the implementation in TwinCAT 3:

Beckhoff parameter:

    bMqttPublishString
    A boolean variable that can be used to trigger the execution of the PublishString command.
    OutputDataMqttPublishString
    An instance of the ST_VoidOutputData structure into which the return value of the PublishString method is written.
    hrMqttPublishString
    This can be used to monitor the execution of the command. Error codes have the type HRESULT.
    fbMqtt
    An instance of the "FB_Mqtt" function block supplied with the DEPRAG Smart library.
    sTopic
    A string(255) variable representing the topic of the MQTT message.
    sMessageData
    A string(255) variable representing the contents of the message to be transmitted.
    QualityOfServiceLevel
    An enum of type "MqttQualityOfServiceLevel" which specifies the Quality of Service level of the transmission.
    bWithRetainFlag
    A boolean variable that sets the retain flag in the MQTT message.
    sClientId
    A String(255) variable that specifies the client that should execute the command.
    ConnectionHandle
    "ConnectionHandle" received when establishing a connection between DEPRAG Smart Server and your OPC UA client.


VAR
   bMqttPublishString         : BOOL;
   OutputDataMqttPublishString       : ST_VoidOutputData;
   hrMqttPublishString         : HRESULT;

   fbMqtt  : FB_Mqtt;
END_VAR

hrMqttPublishString := fbMqtt.PublishString(
     bExecute        := bMqttPublishString, 
     ConnectionHandle      := depragSmartConnection.ConnectionHandle, 
     sTopic         := 'TestSmartTwinCat', 
     sMessageData       := 'TestSmartTwinCatContent', 
     QualityOfServiceLevel     := MqttQualityOfServiceLevel.ExactlyOnce, 
     bWithRetainFlag       := FALSE, 
     sClientId        := '0', 
     OutputData        => OutputDataMqttPublishString);
IF hrMqttPublishString <> S_FALSE THEN 
    bMqttPublishString := FALSE;
END_IF
                                                    

For examples and explanations regarding the calls of nodes via Siemens TIA Portal please refer to the sub-menu "Siemens TIA Portal" in section "Getting Started".