MQTT Subscribe
This block subscribes to an MQTT topic and triggers when messages are received. Use it to receive data from MQTT brokers and other devices.
Overview
The MQTT Subscribe block listens for messages on specified MQTT topics, enabling your device to receive commands, data, and notifications from cloud services and other devices.
Configuration
- Topic: MQTT topic to subscribe to (supports wildcards)
- QoS: Quality of Service level (0, 1, or 2)
- Output: Variable to store received message
Topic Wildcards
MQTT supports wildcards in subscriptions:
Single-level Wildcard (+)
sensors/+/temperature
Matches:
- sensors/room1/temperature
- sensors/room2/temperature
- Does NOT match: sensors/room1/other/temperature
Multi-level Wildcard (#)
sensors/#
Matches:
- sensors/temperature
- sensors/room1/temperature
- sensors/building/floor2/room3/temperature
Use # only at end of topic pattern.
Quality of Service
Subscribe with appropriate QoS:
- QoS 0: At most once (may miss messages)
- QoS 1: At least once (may get duplicates)
- QoS 2: Exactly once (highest reliability)
Actual QoS is minimum of publish and subscribe QoS levels.
Message Processing
When message received:
- MQTT Subscribe block triggers
- Message payload available in output variable
- Process message content
- Extract data and take action
Message Format
Handle different message formats:
Plain Text
Parse simple text messages
JSON
{"command": "turn_on", "device": "light1"}
Parse JSON to extract values
Binary
Process raw binary data
Dynamic Subscriptions
Subscribe to topics dynamically:
- Include device ID in pattern
- Location-based subscriptions
- Role-based topic filtering
Example: devices/${deviceId}/commands/#
Use Cases
Common MQTT subscription scenarios:
- Receive commands from cloud
- Listen for sensor data from other devices
- Subscribe to alerts and notifications
- Receive configuration updates
- Monitor status of multiple devices
Multiple Subscriptions
Create multiple subscribe blocks for:
- Different topic patterns
- Separate message handlers
- Priority-based processing
- Organized application logic
Message Filtering
Filter messages after receiving:
- Check sender device ID
- Validate message format
- Filter by priority
- Discard invalid messages
Use Comparison blocks for filtering.
Subscription Persistence
MQTT subscriptions:
- Automatically resubscribe after reconnection
- Persist across application restarts
- Restored on broker reconnection
Use On MQTT Connected to verify subscription status.
Retained Messages
When subscribing to topic with retained message:
- Immediately receive last retained message
- Useful for initial state synchronization
- Get latest value without waiting
Error Handling
Handle subscription errors:
- Invalid topic pattern: Check topic syntax
- Authorization failed: Verify topic permissions
- Connection lost: Wait for automatic reconnection
- Buffer overflow: Reduce subscription scope
Security
Secure MQTT subscriptions:
- Use TLS/SSL for encryption
- Implement topic-based ACLs
- Validate message source
- Sanitize received data
Performance
Optimize subscription performance:
- Use specific topics instead of broad wildcards
- Limit number of subscriptions
- Process messages efficiently
- Avoid blocking operations in handlers
Related Blocks
- MQTT Publish: Send messages to topics
- On MQTT Connected: Check connection status
- Comparison: Filter messages
- Variable Set: Process message data
Command Pattern
Implement command/response pattern:
- Subscribe to
devices/${deviceId}/commands
- Receive command message
- Process command
- Publish response to
devices/${deviceId}/responses
Integration Examples
Subscribe to messages from:
- Cloud platforms (AWS, Azure, Google Cloud)
- Home automation systems
- Industrial SCADA systems
- Mobile applications
- Other IoT devices
Troubleshooting
Common subscription issues:
- No messages received: Check topic name and wildcards
- Missing messages: Increase QoS level
- Duplicate messages: Normal with QoS 1, handle idempotently
- Authorization error: Check broker topic permissions