-
Notifications
You must be signed in to change notification settings - Fork 84
/
example-mqtt.py
66 lines (58 loc) · 1.93 KB
/
example-mqtt.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
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import subprocess
import paho.mqtt.client as mqtt
import fauxmo
from debounce_handler import debounce_handler
import threading
import logging
import time
logging.basicConfig(level=logging.DEBUG)
# ---------- Network constants -----------
ECHO_LIVINGROOM = "192.168.1.53"
ECHO_KITCHEN = "192.168.1.243"
MQTT_HOST = "jarvis"
MQTT_PORT = 1883
# ---------- Device callback functions ----------
class light_handler(debounce_handler):
"""Publishes state to two different lighting MQTT topics
depending on which Echo the request came from.
"""
TRIGGERS = {"lights": 52002, "lamp": 52003}
def __init__(self, mqtt):
debounce_handler.__init__(self)
self.mqtt = mqtt
def act(self, client_address, state):
if client_address == ECHO_LIVINGROOM:
self.mqtt.publish("livingroom", state)
print "Published to living room"
elif client_address == ECHO_KITCHEN:
self.mqtt.publish("kitchen", state)
print "Published to kitchen"
return True
if __name__ == "__main__":
# Startup the MQTT client in a separate thread
client = mqtt.Client()
client.connect(MQTT_HOST, MQTT_PORT, 60)
ct = threading.Thread(target=client.loop_forever)
ct.daemon = True
ct.start()
# Startup the fauxmo server
fauxmo.DEBUG = True
p = fauxmo.poller()
u = fauxmo.upnp_broadcast_responder()
u.init_socket()
p.add(u)
# Register each device callback as a fauxmo handler
h = light_handler(client)
for trig, port in h.TRIGGERS.items():
fauxmo.fauxmo(trig, u, p, None, port, h)
# Loop and poll for incoming Echo requests
logging.debug("Entering fauxmo polling loop")
while True:
try:
# Allow time for a ctrl-c to stop the process
p.poll(100)
time.sleep(0.1)
except Exception, e:
logging.critical("Critical exception: " + str(e))
break