MQTT is a simple way for devices to talk to each other over the internet. It uses very little data and works well even if the connection is slow or unreliable.
When you want smart home devices like lights and sensors to communicate easily.
When you have many small devices sending small messages often.
When your devices have limited power and need to save battery.
When you want to control devices remotely with low delay.
When your internet connection is not very stable or fast.
This program connects to a public MQTT broker, sends a temperature value, and listens for messages on the same topic.
Raspberry Pi
import paho.mqtt.client as mqtt
# Called when a message is receiveddef on_message(client, userdata, message):
print(f"Message received on {message.topic}: {message.payload.decode()}")
client = mqtt.Client()
client.on_message = on_message
client.connect('broker.hivemq.com', 1883)
client.subscribe('home/livingroom/temperature')
# Publish a temperature reading
client.publish('home/livingroom/temperature', '23')
client.loop_start()
import time
# Wait a bit to receive messages
time.sleep(2)
client.loop_stop()
OutputSuccess
Important Notes
MQTT uses a 'publish-subscribe' model, which means devices send messages to topics and others listen to those topics.
It is lightweight, so it works well on small devices like Raspberry Pi or sensors.
MQTT brokers handle message delivery, so devices don't need to know about each other directly.
Summary
MQTT is simple and uses little data, perfect for small devices.
It works well even with slow or unreliable internet.
It helps many devices talk easily by sending messages to topics.
Practice
(1/5)
1. Why is MQTT considered ideal for small devices in IoT?
easy
A. Because it requires a lot of processing power
B. Because it uses very little data and is simple to implement
C. Because it only works on fast internet connections
D. Because it needs complex setup and configuration
Solution
Step 1: Understand MQTT data usage
MQTT is designed to send small messages, so it uses very little data.
Step 2: Consider device capabilities
Small devices have limited power and memory, so a simple protocol like MQTT fits well.
Final Answer:
Because it uses very little data and is simple to implement -> Option B
Quick Check:
Small data + simplicity = MQTT ideal [OK]
Hint: MQTT is simple and data-light for small devices [OK]
Common Mistakes:
Thinking MQTT needs fast internet
Assuming MQTT requires heavy processing
Believing MQTT setup is complex
2. Which of the following is the correct way to publish a message using MQTT in Python?
easy
A. client.publish('topic/name', 'message')
B. client.send('topic/name', 'message')
C. client.push('topic/name', 'message')
D. client.post('topic/name', 'message')
Solution
Step 1: Recall MQTT Python client method
The standard method to send a message is publish.
Step 2: Verify method names
Methods like send, push, and post are not used in MQTT Python clients.
Final Answer:
client.publish('topic/name', 'message') -> Option A
Quick Check:
Publish method sends messages [OK]
Hint: Use publish() to send MQTT messages in Python [OK]
Common Mistakes:
Using send() instead of publish()
Confusing MQTT with HTTP methods
Using push() or post() which are invalid
3. What will be the output of this Python MQTT client code snippet?