Master programming syntax, solve interactive challenges & run live code in Syntaxis Explore Arena →
menu_book Course Curriculum (Raspberry Pi) expand_more
Raspberry Pi / Automation

MQTT subscribe

Learn MQTT subscribe in Raspberry Pi with a dedicated explanation, a concept-specific example, practical cautions, and a mastery check.

12 min read • Intermediate
raspberry-pi-030.py
python
import paho.mqtt.client as mqtt
def on_message(client,userdata,msg): print(msg.topic,msg.payload.decode())
client=mqtt.Client(mqtt.CallbackAPIVersion.VERSION2); client.on_message=on_message
client.connect('192.168.1.50',1883); client.subscribe('lab/#'); client.loop_forever()
What to watch
Content source & attribution
Original Syntaxis Content · Original Syntaxis Content