Main Content

mqttclient

Create MQTT client connected to broker

Description

An icomm.mqtt.Client object represents an MQTT client in MATLAB® that connects to an external MQTT broker.

Creation

Description

example

mqttClient = mqttclient(brokerAddr) creates an MQTT client connected to the broker specified by brokerAddr. brokerAddr is a host name or IP address of the MQTT broker including the connection protocol. Supported protocols include TCP, WS, SSL, and WSS.

mqttClient = mqttclient(brokerAddr,Name=Value) specifies function options and properties of mqttClient using optional name-value pairs.

Input Arguments

expand all

Location of MQTT broker as a URL with protocol, specified as a string or character vector.

Example: "tcp://broker.hivemq.com"

Data Types: string | char

Name-Value Arguments

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:

User name for connection to broker, specified as a string or character vector.

Data Types: char | string

User password for connection to broker, specified as a string or character vector.

Data Types: char | string

Server root certificate for broker authentication during a secure connection, specified as a string or character vector.

Data Types: char | string

Certificate for client authentication during a secure connection, specified as a string or character vector.

Data Types: char | string

Private key file for client authentication, used along with ClientCertificate for authentication during secure connection.

Data Types: char | string

Password to decrypt the private ClientKey file, specified as a string or character vector.

Data Types: char | string

Properties

expand all

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

This property is read-only.

Identifier of client for connection to broker, specified as a string or character vector.

Data Types: char | string

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

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

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

This property is read-only.

Table of topics client is subscribed to.

Data Types: table

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

subscribeSubscribe to MQTT topic
unsubscribeUnsubscribe from MQTT topics
readRead available messages from MQTT topic
peekView most recent message from MQTT topic
flushClear received MQTT messages
writeWrite message to MQTT topic

Examples

collapse all

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

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

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")

Create an MQTT client connected with WS 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")

Version History

Introduced in R2022a