-
Notifications
You must be signed in to change notification settings - Fork 107
/
RPi_name_port_gpio.py
70 lines (57 loc) · 2.23 KB
/
RPi_name_port_gpio.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
67
68
69
70
""" name_port_gpio.py
This is a demo python file showing how to take paramaters
from command line for device name, port, and GPIO.
All credit goes to https://github.com/toddmedema/echo/
for making the first working versions of this code.
"""
import fauxmo
import logging
import time
import sys
import RPi.GPIO as GPIO ## Import GPIO library
from debounce_handler import debounce_handler
logging.basicConfig(level=logging.DEBUG)
class device_handler(debounce_handler):
"""Publishes the on/off state requested,
and the IP address of the Echo making the request.
"""
#TRIGGERS = {str(sys.argv[1]): int(sys.argv[2])}
#TRIGGERS = {"Light": 52000}
TRIGGERS = {"LED": 52000,"FAN":51000}
def act(self, client_address, state, name):
print("State", state, "from client @", client_address)
# GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
# GPIO.setup(int(7), GPIO.OUT) ## Setup GPIO Pin to OUTPUT
# GPIO.output(int(7), state) ## State is true/false
if name=="kitchen":
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(int(7), GPIO.OUT) ## Setup GPIO Pin to OUTPUT
GPIO.output(int(7), state) ## State is true/false
elif name =="living room":
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(int(11), GPIO.OUT) ## Setup GPIO Pin to OUTPUT
GPIO.output(int(11), state) ## State is true/false
else:
print("Device not found!")
return True
if __name__ == "__main__":
# Startup the fauxmo server
fauxmo.DEBUG = True
p = fauxmo.poller()
u = fauxmo.upnp_broadcast_responder()
u.init_socket()
p.add(u)
# Register the device callback as a fauxmo handler
d = device_handler()
for trig, port in d.TRIGGERS.items():
fauxmo.fauxmo(trig, u, p, None, port, d)
# 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 as e:
logging.critical("Critical exception: "+ e.args )
break