Introduction to Data Streams

การวิเคราะห์ข้อมูล IoT ด้วย Python

Matthias Voppichler

IT Developer

Data Stream คืออะไร

  • กระแสข้อมูลต่อเนื่อง
  • ตัวอย่าง
    • ข้อความ Twitter
    • บทความข่าวออนไลน์
    • สตรีมวิดีโอ
    • ข้อมูลเซนเซอร์ (IoT)
    • คำสั่งซื้อในตลาด (การเงิน)
การวิเคราะห์ข้อมูล IoT ด้วย Python

Data Stream คืออะไร

  • กระแสข้อมูลต่อเนื่อง
  • ตัวอย่าง
    • ข้อความ Twitter
    • บทความข่าวออนไลน์
    • สตรีมวิดีโอ
    • ข้อมูลเซนเซอร์ (IoT)
    • คำสั่งซื้อในตลาด (การเงิน)
การวิเคราะห์ข้อมูล IoT ด้วย Python

MQTT

  • โปรโตคอลรับส่งข้อความ
  • Publish / Subscribe
  • ใช้ทรัพยากรน้อย

Server -> ทำหน้าที่เป็น Message Broker

Client:

  • เชื่อมต่อกับ Broker
  • เผยแพร่ข้อมูล
  • Subscribe หัวข้อที่ต้องการ

Message Queuing Telemetry Transport

การไหลของข้อมูล MQTT - จาก Producer ไปยัง Broker ไปยัง Consumer

การวิเคราะห์ข้อมูล IoT ด้วย Python

ไลบรารี Python

Eclipse Paho™ MQTT Python Client

# Import MQTT library
import paho.mqtt

ข้อมูลเพิ่มเติมและเอกสารอ้างอิงดูได้ที่ GitHub https://github.com/eclipse/paho.mqtt.python

การวิเคราะห์ข้อมูล IoT ด้วย Python

ข้อความเดียว

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

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

ผลลัพธ์:

paho/test/simple, {"time": 1549481572, "humidity": 77, "temp": 21}
การวิเคราะห์ข้อมูล IoT ด้วย Python

Callback

def on_message(client, userdata, message):

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

อาร์กิวเมนต์

  • client - อินสแตนซ์ของ client
  • userdata - ข้อมูลส่วนตัวของผู้ใช้
  • message - อินสแตนซ์ของ MQTTMessage
การวิเคราะห์ข้อมูล IoT ด้วย Python

Callback

import paho.mqtt.subscribe as subscribe


subscribe.callback(on_message, topics="datacamp/roomtemp", hostname="test.mosquitto.org")
การวิเคราะห์ข้อมูล IoT ด้วย 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}'
การวิเคราะห์ข้อมูล IoT ด้วย Python

มาฝึกกันเถอะ!

การวิเคราะห์ข้อมูล IoT ด้วย Python

Preparing Video For Download...