mqttclient

Create a MQTT Client connection.

Attention: Valid only with Altair Communication Extension.

Syntax

cid = mqttclient(brokeraddress)

cid = mqttclient(brokeraddress,property,value)

Inputs

brokeraddress
broker address tag
Type: string
property, values
Properties that control the appearance or behavior of the graphics object.
Type: string
Valid options are:
Property Name
Description/Values
port
Numeric port number to connect to, default: 1883.
Type: scalar
client_id
string to use as client id.Maximum size of client id is 23 characters. default: random generated string.
Type: string
clean_session
set to true to instruct the broker to clean all messages and subscriptions on disconnect, false to instruct it to keep them.Valid values are true and false (default).
Type: Boolean
timeout
Numeric timeout value in seconds (s).If float number is specified, only 3 fractional digits are considered.Set negative to use 1 second. default: 5.
Type: double
keepaliveduration
Numeric keep Alive value in seconds (s).default: 60.
Type: scalar
idletime
Numeric idle time between network loop, value in seconds (s).If float number is specified, only 3 fractional digits are considered. Minimum value is 0.001. default: 0.01.
Type: double
username
username to send as a string to connect to the broker.
Type: string
password
password to send as a string to connect to the broker.
Type: string
on_connect
Callback function that is triggered when the broker is connected and sends a CONNACK message in response to a connection. value is a function name, it must be a function that takes three arguments: clientid object, return code, mqtt message description from MQTT broker.
on_message
Callback function that is triggered when a message is received from the broker. value is a function name, it must be a function that takes four arguments: clientid object, topic, payload message from MQTT broker and message id.
on_disconnect
Callback function that is triggered when the broker has received the DISCONNECT command and has disconnected the client. value is a function name, it must be a function that takes three arguments: clientid object, return code, mqtt message description from MQTT broker.
on_publish
Callback function that is triggered when a message initiated with mqttpublish has been sent to the broker successfully. value is a function name, it must be a function that takes two arguments: clientid object,message id from MQTT broker.
on_subscribe
Callback function that is triggered when the broker responds to a subscription request. value is a function name, it must be a function that takes three arguments: clientid object, message id,grant_qos (integers indicating the granted QoS) of each subscribed topic from MQTT broker.
on_unsubscribe
Callback function that is triggered when the broker responds to a unsubscription request. value is a function name, it must be a function that takes two arguments: clientid object,message id from MQTT broker.

Outputs

cid
client id of the mqtt broker connected to.
Type: string

Examples

Connect to a broker
function test_connectcallback(clientid, rc, Mqttmessageinfo)
		printf('connectcallback clientid=: %s\n', clientid);
		printf('return code: %s\n', num2str(rc));
		printf('return message: %s\n', Mqttmessageinfo);
end

function test_publishcallback(clientid, mid)
		printf('publishcallback clientid=: %s\n', clientid);
		printf('message id: %s\n', num2str(mid));
end

function test_subscribecallback(clientid, mid,grand_qos)
		printf('subscribecallback clientid=: %s\n', clientid);
		printf('message id: %s\n', num2str(mid));
		printf('grant_qos: %s\n', num2str(grand_qos));
end
	
function test_messcallback(clientid, topic,msg, mid)
		printf('messcallback clientid=: %s\n', clientid);
		printf('topic: %s\n', topic);
		printf('msg: %s\n', msg);
		printf('msg id: %d\n', mid);
end

function test_disconnectcallback(clientid, rc, Mqttmessageinfo)
		printf('disconnectcallback clientid=: %s\n', clientid);
		printf('return code: %s\n', num2str(rc));
		printf('return message: %s\n', Mqttmessageinfo);
end

cld = mqttclient('test.mosquitto.org','port',1883, 'client_id', 'c1'...
			,'on_connect','test_connectcallback'...
			,'on_publish','test_publishcallback'...
			,'on_subscribe','test_subscribecallback'...
			,'on_message','test_messcallback' ...
			, 'on_disconnect', 'test_disconnectcallback')
cld = c1
connectcallback clientid=: c1
return code: 0
return message: Connection Accepted.

Comments

If you do not provide a client ID, a random string is generated, which includes 5 to 23 characters.

The return code of the connection response in connection callback are defined by the MQTT protocol version in use. For MQTT v5.0, refer to section 3.2.2.2 Connect Reason code at https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html. For MQTT v3.1.1, refer to section 3.2.2.3 Connect Return code athttp://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html.