Receive messages from MQTT broker for specified topic
Simulink Support Package for Raspberry Pi Hardware / Internet Of Things
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 and Subscribe to MQTT Messages.
The MQTT Subscribe block supports MQTT only over TCP/IP sockets.
Only a maximum of 10 MQTT Subscribe blocks are allowed in a Simulink® model.
IsNew — New message indicator
0 (default) |
If the client receives a new message since the last time step, the
IsNew port outputs
1. Otherwise, the port
Message — Received message
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.
Topic — Topic of message
ASCII values that correspond to the topic name of the received message. For
example, if the topic of the received message is
port outputs the topic as
109 111 100 101 108 47 100 97 116 97.
A topic can have a maximum of 128 characters.
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.
Topic — Topic for subscription
topic/level (default) | any valid topic name
Specify a topic to which the client subscribes. For information on topics and its structure, see Topics in MQTT.
QoS — Levels of QoS
0 (default) |
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 QoS Level for Subscribe.
Message length (N) — Length of message to be received
1 (default) | integer between 1 and 64 inclusive
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.
Output the last received message — Option to view last received message on Message port
off (default) |
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.
Sample time — How often to check for messages from MQTT broker
1 (default) |
-1 | any positive integer
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.