write
Description
write(
writes the message string mqttClient
,mqttTopic
,mqttMsg
)mqttMsg
to the topic
mqttTopic
from the connected client
mqttClient
.
write(
uses additional options specified by one or more name-value pair arguments.mqttClient
,mqttTopic
,mqttMsg
,Name=Value
)
Examples
Write Messages to an MQTT Topic
Create an MQTT client and write messages to a topic with various options.
Create an MQTT client connected to the Eclipse HiveMQ™ public broker and write a
message to the topic myTopic
.
mqttClient = mqttclient("tcp://broker.hivemq.com"); write(mqttClient,"myTopic","Hello World")
Write to the topic with QualityOfService 2.
write(mqttClient,"myTopic","High Service Message",QualityOfService=2)
Write a message to be retained by the broker.
write(mqttClient,"myTopic","Msg for new subscribers",Retain=true)
Input Arguments
mqttClient
— MQTT client
Client
object
MQTT client specified as an icomm.mqtt.Client
object, created with
the mqttclient
function.
Example: mqttClient = mqttclient()
Data Types: object
mqttTopic
— MQTT topic
string | char
MQTT topic to write message to, specified as a string or character vector.
Example: "trubits/mqTop48"
Data Types: string
| char
mqttMsg
— MQTT message to write to topic
string | char
MQTT message to write to the topic, specified as a string or character vector.
Example: "Hello World"
Data Types: char
| string
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: QualityOfService=1
QualityOfService
— Quality of Service (QoS)
0 (default) | 1 | 2
Quality of Service (QoS) for message delivery, specified as an integer value of
0
, 1
, or 2
:
0
— Messages delivered at most once, not more (default).1
— Messages delivered at least once, not less.2
— Messages delivered exactly once, not more or less.
Example: QualityOfService=1
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Retain
— Specify whether broke retains message
false
(default) | true
Control whether the broker retains the message for any new subscriber to the
topic, specified as a logical false
(default) or
true
.
Example: Retain=true
Data Types: logical
Version History
Introduced in R2022a
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)