Main Content

write

Write message to MQTT topic

Since R2022a

Description

write(mqttClient,mqttTopic,mqttMsg) writes the message string mqttMsg to the topic mqttTopic from the connected client mqttClient.

write(mqttClient,mqttTopic,mqttMsg,Name=Value) uses additional options specified by one or more name-value pair arguments.

example

Examples

collapse all

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

collapse all

MQTT client specified as an icomm.mqtt.Client object, created with the mqttclient function.

Example: mqttClient = mqttclient()

Data Types: object

MQTT topic to write message to, specified as a string or character vector.

Example: "trubits/mqTop48"

Data Types: 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

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

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

See Also

Functions