MulaiMulai sekarang secara gratis

MQTT single message

Imagine the following scenario: You have been given an MQTT Broker address and a topic name, and you are supposed to write some code to store the contents of the Datastream.

First, you should check what format the messages will be in by consuming a single message.

You can then print and inspect the message to determine how to process the data further.

This will be our basis for the next exercise, where we will be subscribing to the data stream and collecting multiple messages.

Latihan ini adalah bagian dari kursus

Analyzing IoT Data in Python

Lihat Kursus

Petunjuk latihan

  • Import the MQTT library paho.mqtt.subscribe as subscribe.
  • Retrieve one message from the topic "datacamp/iot/simple" and store the result as msg.
  • Print topic and payload from the message, separated by a comma.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# Import mqtt library
import ____ as ____

# Retrieve one message
msg = ____.____(____, hostname="mqtt.datacamp.com")

# Print topic and payload
print(f"{____}, {____}")
Edit dan Jalankan Kode