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 callbackuserdata, the private user data set when creating the instancemessage, an instance ofMQTTMessage. For this exercise,payloadis 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.
Este exercício faz parte do curso
Analyzing IoT Data in Python
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Define function to call by callback method
def ____(____):
# Parse the message.payload
data = ____
store.append(data)