Introducere în fluxuri de date

Analiza datelor IoT în Python

Matthias Voppichler

IT Developer

Ce este un flux de date

  • Flux continuu de date
  • Exemple
    • Mesaje Twitter
    • Articole de știri online
    • Fluxuri video
    • Date de la senzori (IoT)
    • Ordine de piață (financiar)
Analiza datelor IoT în Python

Ce este un flux de date

  • Flux continuu de date
  • Exemple
    • Mesaje Twitter
    • Articole de știri online
    • Fluxuri video
    • Date de la senzori (IoT)
    • Ordine de piață (financiar)
Analiza datelor IoT în Python

MQTT

  • Protocol de mesagerie
  • Publicare / abonare
  • Amprentă redusă

Serverul -> acționează ca broker de mesaje

Clientul:

  • Se conectează la un broker
  • Publică date
  • Se abonează la subiecte

Message Queuing Telemetry Transport

Fluxul de date MQTT - De la producător -> la Broker -> la consumatori

Analiza datelor IoT în Python

Bibliotecă Python

Eclipse Paho™ MQTT Python Client

# Import MQTT library
import paho.mqtt

Informații suplimentare și documentația disponibile pe GitHub https://github.com/eclipse/paho.mqtt.python

Analiza datelor IoT în Python

Mesaj unic

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}
Analiza datelor IoT în Python

Callback

def on_message(client, userdata, message):

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

Argumente

  • client - instanța clientului
  • userdata - date private ale utilizatorului
  • message - instanță MQTTMessage
Analiza datelor IoT în Python

Callback

import paho.mqtt.subscribe as subscribe


subscribe.callback(on_message, topics="datacamp/roomtemp", hostname="test.mosquitto.org")
Analiza datelor IoT în 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}'
Analiza datelor IoT în Python

Să exersăm!

Analiza datelor IoT în Python

Preparing Video For Download...