Skip to content

Added handler to use GPIO #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
**Emulated Belkin WeMo devices that work with the Amazon Echo**

Visit [this Maker Musings article](http://www.makermusings.com/2015/07/13/amazon-echo-and-home-automation/) to learn more about using this code to integrate
the Amazon Echo with your own home automation.
the Amazon Echo with your own home automation. This is fork of the [makermusings/fauxmo](https://github.com/makermusings/fauxmo) repository. I've added a handler to control gpio pins on the raspbery pi.

### Summary

Expand Down
36 changes: 28 additions & 8 deletions fauxmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import time
import urllib
import uuid
import RPi.GPIO as GPIO



Expand Down Expand Up @@ -372,12 +373,26 @@ def off(self):
r = requests.get(self.off_cmd)
return r.status_code == 200

class gpio_handler(object):
def __init__(self, pin_number):
self.pin = pin_number
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.pin, GPIO.OUT)

def on(self):
GPIO.output(self.pin, 1)
return True
def off(self):
GPIO.output(self.pin, 0)
return True
# Each entry is a list with the following elements:
#
#
# name of the virtual switch
# object with 'on' and 'off' methods
# port # (optional; may be omitted)
#



# NOTE: As of 2015-08-17, the Echo appears to have a hard-coded limit of
# 16 switches it can control. Only the first 16 elements of the FAUXMOS
Expand All @@ -386,6 +401,8 @@ def off(self):
FAUXMOS = [
['office lights', rest_api_handler('http://192.168.5.4/ha-api?cmd=on&a=office', 'http://192.168.5.4/ha-api?cmd=off&a=office')],
['kitchen lights', rest_api_handler('http://192.168.5.4/ha-api?cmd=on&a=kitchen', 'http://192.168.5.4/ha-api?cmd=off&a=kitchen')],
# use GPIO pin 11 to trigger a relay
['Bedroom lights', gpio_handler(11),58304],
]


Expand All @@ -412,12 +429,15 @@ def off(self):

dbg("Entering main loop\n")

while True:
try:
try:
while True:
try:
# Allow time for a ctrl-c to stop the process
p.poll(100)
time.sleep(0.1)
except Exception, e:
dbg(e)
break
p.poll(100)
time.sleep(0.1)
except Exception, e:
dbg(e)
break

finally:
GPIO.cleanup()