mqttclient
Create a MQTT Client connection.
Attention: Available only with Twin Activate commercial edition.
Syntax
cid = mqttclient(brokeraddress)
cid = mqttclient(brokeraddress,property,value)
Inputs
- brokeraddress
- broker address tag
- property, values
- Properties that control the appearance or behavior of the graphics object.
Outputs
- cid
- client id of the mqtt broker connected to.
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.