0
0
IOT Protocolsdevops~30 mins

Wildcard subscriptions (+ and #) in IOT Protocols - Mini Project: Build & Apply

Choose your learning style9 modes available
Wildcard Subscriptions (+ and #) in MQTT Topics
📖 Scenario: You are setting up an MQTT client to subscribe to sensor data topics. MQTT topics use slashes to separate levels, like home/kitchen/temperature. You want to use wildcards to subscribe to multiple topics easily.The + wildcard matches exactly one level, and the # wildcard matches all remaining levels.
🎯 Goal: Learn how to create MQTT topic subscriptions using the + and # wildcards to receive messages from multiple topics efficiently.
📋 What You'll Learn
Create a list of MQTT topics to subscribe to
Add wildcard subscription patterns using + and #
Filter topics matching the wildcard subscriptions
Print the matched topics
💡 Why This Matters
🌍 Real World
MQTT is widely used in IoT devices to send sensor data. Wildcard subscriptions let you listen to many related topics easily, saving network and processing resources.
💼 Career
Understanding MQTT wildcards is important for IoT developers, DevOps engineers managing IoT infrastructure, and anyone working with message brokers and real-time data streams.
Progress0 / 4 steps
1
Create a list of MQTT topics
Create a list called topics with these exact MQTT topic strings: 'home/kitchen/temperature', 'home/kitchen/humidity', 'home/livingroom/temperature', 'office/room1/temperature', 'office/room2/humidity'.
IOT Protocols
Need a hint?

Use square brackets to create a list and include all topics as strings separated by commas.

2
Add wildcard subscription patterns
Create a list called subscriptions with these exact wildcard subscription strings: 'home/+/temperature' and 'office/#'.
IOT Protocols
Need a hint?

Use a list with the two wildcard subscription strings exactly as shown.

3
Filter topics matching the wildcard subscriptions
Create a function called match_topic that takes subscription and topic strings and returns True if the topic matches the subscription using MQTT wildcard rules for + and #. Then create a list called matched_topics that includes topics from topics matching any subscription in subscriptions.
IOT Protocols
Need a hint?

Split both subscription and topic by '/' and compare each level. The '+' wildcard matches one level, '#' matches all remaining levels.

4
Print the matched topics
Write a print statement to display the matched_topics list.
IOT Protocols
Need a hint?

Use print(matched_topics) to show the list of matched topics.