MQTTClient (FB)¶
FUNCTION_BLOCK MQTTClient EXTENDS CBML.LCon
Function block to connect with a MQTT broker
- InOut:
- Scope - Name - Type - Initial - Comment - Inherited from - Input - xEnable- BOOL- TRUE: Activates the defined operation- FALSE: Aborts/resets the defined operation- LCon - Output - xDone- BOOL- TRUE: Ready condition reached- LCon - xBusy- BOOL- TRUE: Operation is running- LCon - xError- BOOL- TRUE: Error condition reached- LCon - Input - uiPort- UINT- Port of MQTT Broker Server - xUseTLS- BOOL- Obsolete, use itfTLSContext instead ! - TRUE: Crypted Connection, FALSE: Uncrypted Connection - uiKeepAlive- UINT- 5 - Keep Alive Time in Seconds (optional) - pbWillMessage- POINTER TO BYTE - 0 - Pointer to the ‘Last Will’ message (optional) - uiWillMessageSize- UINT- 0 - Size of ‘Last Will’ message (optional) - xWillRetain- BOOL- TRUE: Saves the ‘Last Will’ message on server. If Client subscribes later, then it receives the last stack message from server. - eWillQoS- QoS Level of ‘Last Will’ message - xCleanSession- BOOL- TRUE: Creates a new session, FALSE: Uses already existing session if available - wsUsername- WSTRING(255)- “” - User Name (optional) - wsPassword- WSTRING(1024)- “” - User Password (optional) - wsWillTopic- WSTRING(1024)- “” - Will Topic (optional) - sClientId- STRING(255)- ‘’ - Client ID, if empty then new ID is generated, allowed are only these characters 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ (optional) - tPingInterval- TIME- TIME#2s0ms - Interval time in seconds how often should be pinged, if Time<=0 then no pings - hCert- RTS_IEC_HANDLE- RTS_INVALID_HANDLE - Obsolete, use itfTLSContext instead ! - Handle to the client certificate (optional) - itfTLSContext- NBS.ITLSContext- 0 - Encapsulates all the data neccecray to handle encrypted tcp connections - Static initialization, as shown in the following code snippet. - VAR commonName : STRING := 'MyRrasPi'; ciCertInfo : NBS.CERT_INFO := (psInfo:=ADR(commonName), udiSize:=LEN(commonName)); myTLSContext : NBS.TLSContext := ( ePurpose:=NBS.PURPOSE.CLIENT_SIDE, sUseCaseName:='NBSTest', sTLSVersion:='1.3', ciCertInfo:=ciCertInfo, udiVerificationMode:=2 ); END_VAR - itfAsyncProperty- NBS.IAsyncProperty- 0 - Runs the connect process in a own background task. Use this property if the connection setup takes longer than one task cycle (e.g. TLS connections) - udiTimeOut- UDINT- 0 - Defines the time (µs) after which the connection setup aborts with xError. - eCommunicationMode- COMMUNICATION_MODE.TCP - Communication mode: TCP or WEB_SOCKET, default: TCP
- COMMUNICATION_MODE.TCP: Configure the connection via the inputs: sHostname, uiPort and itfTLSContext 
- COMMUNICATION_MODE.WEB_SOCKET: Configure the connection via the inputs: sWebSocketUrl and itfTLSContext 
 
 - Additional WebSocket options can be set via the method SetWebSocketOptions. - sWebSocketUrl- REFERENCE TO STRING(1024) - The uri of the websocket server. e.g ‘ws://localhost:8080’ ws-URI = “ws:” “//” host [ “:” port ] path [ “?” query ] wss-URI = “wss:” “//” host [ “:” port ] path [ “?” query ] - The input is only relevant for eCommunicationMode = COMMUNICATION_MODE.WEB_SOCKET. - eMQTTVersion- MQTT_VERSION.V3_1_1 - MQTT protocol version, default: 3.1.1 - mQTTConnectProperties- REFERENCE TO MQTTConnectProperties - Connection properties for MQTT V5 - mQTTWillProperties- REFERENCE TO MQTTWillProperties - Will properties for MQTT V5 - Inout Const - sHostname- STRING(80)- Host name of Broker MQTT Server (is IPAddress) - Output - eMQTTError- MQTT Error Type - xConnectedToBroker- BOOL- FALSE - FALSE when there is no Connection to MQTT Broker Server, otherwise TRUE - eReaonCode- Reason code of the last operation (MQTT V5 only) - mQTTConnackProperties- Connection response properties (MQTT V5 only) 
Methods:
Structure: