mqttclient
Description
An icomm.mqtt.Client
object represents an MQTT client in
MATLAB® that connects to an external MQTT broker.
Creation
Description
creates an MQTT client connected to the broker specified by mqttClient
= mqttclient(brokerAddr
)brokerAddr
.
brokerAddr
is a host name or IP address of the MQTT broker including
the connection protocol. Supported protocols include MQTT, TCP, WS, SSL, and WSS.
specifies function options and properties of mqttClient
= mqttclient(brokerAddr
,Name=Value)mqttClient
using optional
name-value pairs.
Input Arguments
brokerAddr
— Location of MQTT broker
string | char
Location of MQTT broker as a URL, including protocol, specified as a string or character vector.
Supported protocols are:
mqtt:// tcp:// ws:// ssl:// wss://
Example: "tcp://broker.hivemq.com"
Data Types: string
| char
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: Port=8883
Name-value arguments can specify the properties Port
,
ClientID
, Timeout
, and
KeepAliveDuration
; and the following options:
Username
— User name for connection to broker
string | char
User name for connection to broker, specified as a string or character vector.
Data Types: char
| string
Password
— User password for connection to broker
string | char
User password for connection to broker, specified as a string or character vector.
Data Types: char
| string
CARootCertificate
— Server root certificate for broker authentication
string | char
Server root certificate for broker authentication during a secure connection, specified as a string or character vector.
Data Types: char
| string
ClientCertificate
— Certificate for client authentication
string | char
Certificate for client authentication during a secure connection, specified as a string or character vector.
Data Types: char
| string
ClientKey
— Private key file for client authentication
string | char
Private key file for client authentication, used along with
ClientCertificate
for authentication during secure
connection.
Data Types: char
| string
SSLPassword
— Password to decrypt private key
string | char
Password to decrypt the private ClientKey
file, specified
as a string or character vector.
Data Types: char
| string
Properties
Port
— Socket port number for connection
integer value
This property is read-only.
Socket port number to use when connecting to the MQTT broker, specified as an integer value.
Example: 8883
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
ClientID
— Identifier of client
string | char
This property is read-only.
Identifier of client for connection to broker, specified as a string or character vector.
Data Types: char
| string
Timeout
— Time allowed to complete connection
5 (default) | integer value | duration
This property is read-only.
Time allowed for connection to be completed, specified as a numeric integer value in seconds or as a duration.
Example: Timeout=60
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| duration
KeepAliveDuration
— Maximum idle time allowed between broker and client
60 (default) | integer value | duration
This property is read-only.
Maximum idle time allowed between broker and client, specified as a numeric integer value in seconds or as a duration. If no traffic occurs in this time span, the client issues a keep alive packet.
Example: KeepAliveDuration=minutes(5)
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| duration
BrokerAddress
— Location of MQTT broker
string | char
This property is read-only.
Location of the MQTT broker, specified as a string or character vector. BrokerAddress identifies the host name or IP address of the MQTT broker, including connection protocol. Supported protocols include TCP, WS, SSL, and WSS.
Example: "tcp://broker.hivemq.com"
Data Types: char
| string
Subscriptions
— Table of topic subscriptions
table
This property is read-only.
Table of topics client is subscribed to.
Data Types: table
Connected
— Status of client connection to broker
1
| 0
This property is read-only.
Status of the client connection to the broker, returned as logical
1
(connected) or 0
(not connected). If the
Connected
status is 0
, that might indicate an
issue with the broker; check that you have the correct address, clear the object, and
try creating it again.
Example: 1
Data Types: logical
Object Functions
Examples
Connect to an MQTT Broker
Create a nonsecure MQTT client connection to a HiveMQ public broker with default settings.
mqttClient = mqttclient("tcp://broker.hivemq.com")
mqttClient = Client with properties: BrokerAddress: "tcp://broker.hivemq.com" Port: 1883 ClientID: "" Timeout: 5 KeepAliveDuration: 60 Subscriptions: [0×3 table] Connected: 1
Connect with a Specific ID and Port
Create a nonsecure MQTT client connection to a HiveMQ public broker using port 1883
and specify the client ID as myClient
.
mqttClient = mqttclient("tcp://broker.hivemq.com",ClientID="myClient",Port=1883)
mqttClient = Client with properties: BrokerAddress: "tcp://broker.hivemq.com" Port: 1883 ClientID: "myClient" Timeout: 5 KeepAliveDuration: 60 Subscriptions: [0×3 table] Connected: 1
Make a Secure Connection over SSL
Create an MQTT client with a secure connection over SSL using certificates for authentication. Connect the client to the Eclipse Mosquitto™ public broker at port 8884 and specify the broker root certificate, client certificate, and private key.
mqttClientSSL = mqttclient("ssl://mosquitto.org",Port=8884,... CARootCertificate="C:\mqtt\mosquitto.org.pem",... ClientCertificate="C:\mqtt\client.pem",... ClientKey="C:\mqtt\client.key")
Make a Connection over Websockets
Create an MQTT client connected with websockets to ThingSpeak™. Connecting with the MQTT interface on ThingSpeak requires
ClientID
, Username
, and
Password
.
mqttClient = mqttclient("ws://mqtt3.thingspeak.com",Port=80,... Username="MyUserID",ClientID="MyClientID",Password="MyPassword")
Create a client with a secure connection.
mqttClient = mqttclient("wss://mqtt3.thingspeak.com",Port=443,... Username="MyUserID",ClientID="MyClientID",Password="MyPassword")
Version History
Introduced in R2022aR2023b: Support for MQTT Protocol
The mqttclient
function is enhanced to support the
mqtt://
protocol.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)