AddClient

TwinCAT
Network MQTT Client

ns=2;s=DEPRAG Smart/Network/MQTT/AddClient

Creates a new, fully parameterized MQTT client. The MQTT client can be retrieved from the ActiveClients list via a zero-based index.

Parameters:

    BrokerPort
    Port of the MQTT broker. By default, MQTT uses port 1833 for unencrypted connections and port 8883 for encrypted connections.
    Username
    Username used to authenticate with the specified MQTT broker.
    Password
    Password used to authenticate to the specified MQTT broker.
    AcceptSelfSignedCertificates
    Determines whether unsigned certificates are accepted. If this value is set to yes, the application can connect to MQTT brokers with self-signed certificates.
    EnableSsl
    Specifies whether the connection to the MQTT server is performed using the SSL protocol.
    ConnectionTimeoutInMilliseconds
    Maximum time in milliseconds for establishing a connection to the MQTT server. If no connection can be established within the time interval, an error is output.

Returns:

    AddClient
    A variable of type OpcMethodReturnValueInt32. The Result property contains the command-specific return values.
      Result
      Unique zero-based index that can be used to retrieve the MQTT client from the ActiveClients array.

Examples:

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

Beckhoff parameter:

    bMqttAddClient
    A boolean variable that can be used to trigger the execution of the AddClient command.
    OutputDataMqttAddClient
    An instance of the ST_Int32OutputData structure into which the return value of the AddClient method is written.
    hrMqttAddClient
    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.
    sBrokerUrl
    A string(255) variable representing the IP or DNS address of the MQTT broker to connect to.
    nBrokerPort
    A DINT variable representing the port of the MQTT broker. By default, MQTT uses port 1833 for unencrypted connections and port 8883 for encrypted connections.
    sUsername
    A string(255) variable representing the username used to authenticate with the specified MQTT broker.
    sPassword
    A string(255) variable representing the password used to authenticate with the specified MQTT broker.
    bAcceptSelfSignedCertificates
    A Boolean variable that determines whether self-signed certificates are accepted. If this value is set to yes, the application can connect to MQTT brokers with self-signed certificates.
    bEnableSsl
    A boolean variable used to specify whether the connection to the MQTT broker is performed over the SSL protocol.
    nConnectionTimeoutInMilliseconds
    A DINT variable that defines the maximum time in milliseconds for establishing a connection to the MQTT broker. If no connection can be established within the time interval, an error is output.
    ConnectionHandle
    "ConnectionHandle" received when establishing a connection between DEPRAG Smart Server and your OPC UA client.


VAR
  bMqttAddClient          : BOOL;
  OutputDataMqttAddClient        : ST_Int32OutputData;
  hrMqttAddClient          : HRESULT;

  fbMqtt  : FB_Mqtt;
END_VAR

hrMqttAddClient := fbMqtt.AddClient(
     bExecute        := bMqttAddClient, 
     ConnectionHandle      := depragSmartConnection.ConnectionHandle, 
     sBrokerUrl        := '10.10.222.101', 
     nBrokerPort        := 8883, 
     sUsername        := 'default', 
     sPassword        := '0000', 
     bAcceptSelfSignedCertificates   := TRUE, 
     bEnableSsl        := TRUE, 
     nConnectionTimeoutInMilliseconds  := 5000, 
     OutputData        => OutputDataMqttAddClient);
IF hrMqttAddClient <> S_FALSE THEN 
    bMqttAddClient := 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".