Giới thiệu về luồng dữ liệu

Phân tích dữ liệu IoT bằng Python

Matthias Voppichler

IT Developer

Luồng dữ liệu là gì

  • Luồng dữ liệu liên tục
  • Ví dụ
    • Tin nhắn Twitter
    • Bài báo trực tuyến
    • Luồng video
    • Dữ liệu cảm biến (IoT)
    • Lệnh thị trường (tài chính)
Phân tích dữ liệu IoT bằng Python

Luồng dữ liệu là gì

  • Luồng dữ liệu liên tục
  • Ví dụ
    • Tin nhắn Twitter
    • Bài báo trực tuyến
    • Luồng video
    • Dữ liệu cảm biến (IoT)
    • Lệnh thị trường (tài chính)
Phân tích dữ liệu IoT bằng Python

MQTT

  • Giao thức thông điệp
  • Mô hình publish/subscribe
  • Nhẹ, ít tài nguyên

Server -> đóng vai trò Broker thông điệp

Client:

  • Kết nối tới Broker
  • Publish dữ liệu
  • Subscribe chủ đề (topics)

Message Queuing Telemetry Transport

Luồng dữ liệu MQTT - Từ nhà sản xuất -> tới Broker -> tới người tiêu thụ

Phân tích dữ liệu IoT bằng Python

Thư viện Python

Thư viện Eclipse Paho™ MQTT cho Python

# Import thư viện MQTT
import paho.mqtt

Thông tin và tài liệu tại GitHub https://github.com/eclipse/paho.mqtt.python

Phân tích dữ liệu IoT bằng Python

Một thông điệp

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

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

Kết quả:

paho/test/simple, {"time": 1549481572, "humidity": 77, "temp": 21}
Phân tích dữ liệu IoT bằng Python

Hàm gọi lại (callback)

def on_message(client, userdata, message):

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

Tham số

  • client - thể hiện client
  • userdata - dữ liệu người dùng riêng
  • message - thể hiện MQTTMessage
Phân tích dữ liệu IoT bằng Python

Hàm gọi lại (callback)

import paho.mqtt.subscribe as subscribe


subscribe.callback(on_message, topics="datacamp/roomtemp", hostname="test.mosquitto.org")
Phân tích dữ liệu IoT bằng Python

Đăng ký MQTT

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}'
Phân tích dữ liệu IoT bằng Python

Ayo berlatih!

Phân tích dữ liệu IoT bằng Python

Preparing Video For Download...