Main Content

MQTT Subscribe

Receive messages from MQTT broker for specified topic

Add-On Required: This feature requires the Simulink Support Package for Raspberry Pi Hardware add-on.

  • MQTT Subscribe block

Libraries:
Simulink Support Package for Raspberry Pi Hardware / Internet Of Things

Description

The MQTT Subscribe block receives messages from the Message Queuing Telemetry Transport (MQTT) broker for the specified topic.

The block has three output ports: IsNew, Message, and Topic. At each time step, the block checks if a new message is available on the specified topic. If a new message is available, the block receives the message. The Message port outputs the new message. If a new message is not available, Message outputs the last received message. The Topic port outputs the topic of the received message. The string topic/level/# on the block icon is specified by the Topic parameter.

For more information on MQTT, see Publish MQTT Messages and Subscribe to Message Topics.

Note

The MQTT Subscribe block supports MQTT only over TCP/IP sockets.

Examples

Limitations

Only a maximum of 10 MQTT Subscribe blocks are allowed in a Simulink® model.

Ports

Output

expand all

If the client receives a new message since the last time step, the IsNew port outputs 1. Otherwise, the port outputs 0.

Data Types: Boolean

The message received from the MQTT broker, specified as an N-by-1 vector. N is the length of the message specified by the Message length (N) parameter.

Data Types: double

ASCII values that correspond to the topic name of the received message. For example, if the topic of the received message is model/data, the port outputs the topic as 109 111 100 101 108 47 100 97 116 97.

To convert the ASCII values into the alphanumeric equivalent, attach an ASCII to String block to the output of the Topic port. For information on topics and their structure, see Topics in MQTT.

Note

A topic can have a maximum of 128 characters.

Dependencies

The Topic port is available only if the Topic parameter value specified in the Block Parameters dialog box includes a wildcard entry. For information on wildcards, see Wildcards in MQTT Topics.

Data Types: uint8

Parameters

expand all

Specify a topic to which the client subscribes. For information on topics and its structure, see Topics in MQTT.

Quality of Service (QoS) defines the reliability of the message delivery process in MQTT. MQTT provides three QoS levels for message delivery: QoS 0, QoS 1, and QoS 2. The MQTT broker that you are using might not support all three levels of QoS. For example, ThingSpeak™ MQTT supports only QoS 0.

Select a QoS level from the list:

  • 0: The MQTT broker sends the message to the client. The client does not send an acknowledgment of the receipt.

  • 1: The MQTT broker sends the message to the client at least once. The broker resends the message if it does not receive an acknowledgment from the client.

  • 2: The MQTT broker sends the message to the client exactly once. The broker and client exchange acknowledgments to ensure that the message is received only once.

For more information on QoS levels, see Levels of QoS in MQTT.

Specify the length of the message to be received from the MQTT broker. If the length of the received message is greater than the length specified in Message length (N), the block outputs only the first N characters of the received message. If the length of the received message is less than the length specified in Message length (N), the block appends zeros to the received message.

When you select this option, the last received message is displayed on the Message output port.

  • For a high isNew output port, the new message is displayed on the Message output port.

  • For a low isNew output port, the message received at the last sample time is displayed on the Message output port.

When you clear this option, the messages received for the specified topic form a queue in a first-in-first-out order and these messages are displayed in the Message output port accordingly.

Specify how often the subscriber checks for a new message from the broker. When you specify Sample time as -1, the block inherits its sample time based on the context of the block within the model.

Version History

Introduced in R2018b

See Also

| (ThingSpeak)