forked from Phantom-Cluster/Alexa-Raspberry-Single-Relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPi_name_port_gpio.py
61 lines (52 loc) · 1.94 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
""" 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 = {"Lights": 52000}
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
print("Alexa Recognise Lights")
if state==True:
GPIO.output(int(7), state) ## State is true/false
print("Lights are Turned On:D, Alexa You Are Doing Great Job")
else:
GPIO.cleanup(int(7))
print("Lights are Turned Off :( Saving Energy :D once Again Great Job")
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