BaşlayınÜcretsiz Başlayın

Cache Datastream

You will now gather data from 2 energy counters. This is a very minimalistic data stream, which only provides the measurement device and the value, separated by a comma.

This looks as follows: C331,6020

Since there is no time in the measurement itself, you need to rely on the time the message was submitted to the MQTT broker. This timestamp is available as message.timestamp in epoch format.

Bu egzersiz

Analyzing IoT Data in Python

kursunun bir parçasıdır
Kursu Görüntüle

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

cache = []

def on_message(client, userdata, message):
 	# Combine timestamp and payload
    data = f"{____.____},{____.____}"
    # Append data to cache
    ____.____(____)

# Connect function to mqtt datastream
subscribe.callback(on_message, topics="datacamp/energy", hostname=MQTT_HOST)
Kodu Düzenle ve Çalıştır