Get startedGet started for free

Save Datastream

You will now take an MQTT Data stream and append each new data point to the list store.

Using the library paho.mqtt, you can subscribe to a data stream using subscribe.callback().

Each new message will result in one call to our function, which is required to have the following arguments:

  • client, the client instance for this callback
  • userdata, the private user data set when creating the instance
  • message, an instance of MQTTMessage. For this exercise, payload is the only attribute we're interested in.

You need to parse the data as JSON string using json.loads() and append it the list store. You'll then convert this to a DataFrame and store the DataFrame as CSV file.

json, pandas as pd, MQTT_HOST and topic are available in your session.

This exercise is part of the course

Analyzing IoT Data in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Define function to call by callback method
def ____(____):
    # Parse the message.payload
    data = ____
    store.append(data)
Edit and Run Code