Introduction to Data streams

Analyzing IoT Data in Python

Matthias Voppichler

IT Developer

What is a Data Stream

  • Constant stream of Data
  • Examples
    • Twitter messages
    • Online News Articles
    • Video streams
    • Sensor data (IoT)
    • Market orders (financial)
Analyzing IoT Data in Python

What is a Data Stream

  • Constant stream of Data
  • Examples
    • Twitter messages
    • Online News Articles
    • Video streams
    • Sensor data (IoT)
    • Market orders (financial)
Analyzing IoT Data in Python

MQTT

  • Message protocol
  • Publish / subscribe
  • Small footprint

Server -> Acts as a message Broker

Client:

  • Connects to a Broker
  • Publishes data
  • Subscribes to topics

Message Queuing Telemetry Transport

MQTT data flow - From producer -> to Broker -> to consumers

Analyzing IoT Data in Python

Python library

Eclipse Paho™ MQTT Python Client

# Import MQTT library
import paho.mqtt

More information and the documentation available at GitHub https://github.com/eclipse/paho.mqtt.python

Analyzing IoT Data in Python

Single message

import paho.mqtt.subscribe as subscribe
msg = subscribe.simple("paho/test/simple", 
                       hostname="test.mosquitto.org")

print(f"{msg.topic}, {msg.payload}")

Output:

paho/test/simple, {"time": 1549481572, "humidity": 77, "temp": 21}
Analyzing IoT Data in Python

Callback

def on_message(client, userdata, message):

print(f"{message.topic} : {message.payload}")

Arguments

  • client - client instance
  • userdata - private user data
  • message - instance of MQTTMessage
Analyzing IoT Data in Python

Callback

import paho.mqtt.subscribe as subscribe


subscribe.callback(on_message, topics="datacamp/roomtemp", hostname="test.mosquitto.org")
Analyzing IoT Data in Python

MQTT Subscribe

import paho.mqtt.subscribe as subscribe

def on_message(client, userdata, message): print("{} : {}".format(message.topic, message.payload))
subscribe.callback(on_message, topics="datacamp/roomtemp", hostname="test.mosquitto.org")
datacamp/roomtemp : b'{"time": 1543344857, "hum": 34, "temp": 24}'
datacamp/roomtemp : b'{"time": 1543344858, "hum": 35, "temp": 23}'
datacamp/roomtemp : b'{"time": 1543344860, "hum": 36, "temp": 22}'
datacamp/roomtemp : b'{"time": 1543344946, "hum": 37, "temp": 22}'
datacamp/roomtemp : b'{"time": 1543345010, "hum": 36, "temp": 13}'
Analyzing IoT Data in Python

Let's practice!

Analyzing IoT Data in Python

Preparing Video For Download...