CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Analyzing IoT Data in Python

Afficher le cours

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Define function to call by callback method
def ____(____):
    # Parse the message.payload
    data = ____
    store.append(data)
Modifier et exécuter le code