forked from Sensirion/libsensors-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreaming_client.py
executable file
·52 lines (39 loc) · 1.51 KB
/
streaming_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
Sample app that connects to a mqtt server and prints all sensor data.
It is possible to subscribe to only some sensors or to all of them.The program
blocks until cancelled by a keyboard interrupt.
To run the script you need to install the paho MQTT library:
pip install paho-mqtt
http://www.eclipse.org/paho/clients/python/docs/
"""
import json
import signal
import sys
import paho.mqtt.client as mqtt
client = mqtt.Client()
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
print("Connected with result code " + str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
# this subscribes only to the sfm sensor
#client.subscribe("sensors/+/sfm/#")
# this subscribes to all sensors
client.subscribe("sensors/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
payload = json.loads(msg.payload)
sys.stdout.write('{0}, {1}: '.format(msg.topic, payload['timestamp']))
for v, u in zip(payload['values'], payload['units']):
sys.stdout.write(u'{0} {1} '.format(v, u))
sys.stdout.write('\n')
def signal_handler(signal, frame):
print('terminating')
client.disconnect()
signal.signal(signal.SIGINT, signal_handler)
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.1.10")
client.loop_forever()