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
xEnableBOOLTRUE: Activates the defined operationFALSE: Aborts/resets the defined operationLCon
Output
xDoneBOOLTRUE: Ready condition reachedLCon
xBusyBOOLTRUE: Operation is runningLCon
xErrorBOOLTRUE: Error condition reachedLCon
Input
uiPortUINTPort of MQTT Broker Server
xUseTLSBOOLObsolete, use itfTLSContext instead !
TRUE: Crypted Connection, FALSE: Uncrypted Connection
uiKeepAliveUINT5
Keep Alive Time in Seconds (optional)
pbWillMessagePOINTER TO BYTE
0
Pointer to the ‚Last Will‘ message (optional)
uiWillMessageSizeUINT0
Size of ‚Last Will‘ message (optional)
xWillRetainBOOLTRUE: Saves the ‚Last Will‘ message on server. If Client subscribes later, then it receives the last stack message from server.
eWillQoSQoS Level of ‚Last Will‘ message
xCleanSessionBOOLTRUE: Creates a new session, FALSE: Uses already existing session if available
wsUsernameWSTRING(255)„“
User Name (optional)
wsPasswordWSTRING(1024)„“
User Password (optional)
wsWillTopicWSTRING(1024)„“
Will Topic (optional)
sClientIdSTRING(255)‚‘
Client ID, if empty then new ID is generated, allowed are only these characters 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ (optional)
tPingIntervalTIMETIME#2s0ms
Interval time in seconds how often should be pinged, if Time<=0 then no pings
hCertRTS_IEC_HANDLERTS_INVALID_HANDLE
Obsolete, use itfTLSContext instead !
Handle to the client certificate (optional)
itfTLSContextNBS.ITLSContext0
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
itfAsyncPropertyNBS.IAsyncProperty0
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)
udiTimeOutUDINT0
Defines the time (µs) after which the connection setup aborts with xError.
eCommunicationModeCOMMUNICATION_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.
sWebSocketUrlREFERENCE 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.
eMQTTVersionMQTT_VERSION.V3_1_1
MQTT protocol version, default: 3.1.1
mQTTConnectPropertiesREFERENCE TO MQTTConnectProperties
Connection properties for MQTT V5
mQTTWillPropertiesREFERENCE TO MQTTWillProperties
Will properties for MQTT V5
Inout Const
sHostnameSTRING(80)Host name of Broker MQTT Server (is IPAddress)
Output
eMQTTErrorMQTT Error Type
xConnectedToBrokerBOOLFALSE
FALSE when there is no Connection to MQTT Broker Server, otherwise TRUE
eReaonCodeReason code of the last operation (MQTT V5 only)
mQTTConnackPropertiesConnection response properties (MQTT V5 only)
Methods:
Structure: