-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.py
55 lines (40 loc) · 1.49 KB
/
subscribe.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
53
54
55
import paho.mqtt.client as mqtt
from collections import namedtuple
TEMPERATURE='temperature'
HUMIDITY='humidity'
Property = namedtuple('Property', ['topic', 'type'])
devices =[
{
"id": "d55c4200",
"iota_id": "xxx",
"properties":[
Property("temperature/degrees", type=TEMPERATURE),
Property("humidity/percentage", type=HUMIDITY)
]
},
{
"id": "30aea4379688",
"iota_id": "xxx",
"properties":[
Property("temperature/degrees", type=TEMPERATURE),
Property("humidity/percentage", type=HUMIDITY)
]
},
]
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
for device in devices:
for prop in device["properties"]:
client.subscribe("homie/{}/{}".format(device["id"], prop.topic))
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
#print(msg.topic+" "+str(msg.payload))
device = [device for device in devices if device["id"]==msg.topic.split("/")[1]][0]
if device:
prop = [prop for prop in device["properties"] if prop.topic == "/".join(msg.topic.split("/")[2:])][0]
print "SEND TO IOTA, DEVICE:{}, TYPE:{}, VALUE:{}".format(device["id"], prop.type, msg.payload)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.178.37", 1883, 60)
client.loop_forever()