UPDATE: This log is outdated. See Program Code v0.2 for new version.
My python script is the /var/www/cgi/knockknock.wsgi. If you wan to run it, you can call http://your_ip_address/knockknock.
- You can send a pattern to make music. For example http://your_ip_address/knockknock?pattern=1-0.5-1-0.5-4-1. That means: hit the rod 1, then wait 0.5 sec, then hit the rod 1, then wait 0.5 sec, then hit the rod 4, then wait 1 sec.
- You can use the IFTTT's Maker channel to make a specific knockknock web request. So the chimes can be triggered when you receive in important email, or you wife comes home, or there is a new instagram photo, etc. etc.
# -*- coding: utf-8 -*-
from cgi import parse_qs, escape
def application(environ, start_response):
output = 'Let\'s make some noise!'
import RPi.GPIO as GPIO
from time import sleep
# Connection between the chime rods (0-4) and the GPIO pins
GPIO.setmode(GPIO.BOARD)
chimeRod2Pin = {0: 37, 1: 36, 2: 33, 3: 31, 4: 29}
GPIO.setwarnings(False)
for pin in chimeRod2Pin:
GPIO.setup(chimeRod2Pin[pin], GPIO.OUT,initial=GPIO.LOW)
#Get the chime pattern from the query string:
parameters = parse_qs(environ.get('QUERY_STRING', ''))
if 'pattern' in parameters:
pattern = escape(parameters['pattern'][0])
else:
pattern = '1-0.5-1-0.5-4-1'
pattern = map(float, pattern.split('-'))
#The chimes should sound softly during the night.
#The power is defined in seconds.
import datetime
now = datetime.datetime.now()
output += "\n"
output += str(now)
if ( now.hour > 22 ) or (now.hour < 9 ):
power = 0.02
else:
power = 0.09
def hitRod( number ):
GPIO.output(chimeRod2Pin[number], GPIO.HIGH)
sleep(power)
GPIO.output(chimeRod2Pin[number], GPIO.LOW)
def hitPattern( array ):
for i in range(0, len(array), 2):
hitRod(array[i])
if (0 <= i + 1) and (i + 1 < len(array)):
sleep(array[i + 1])
hitPattern(pattern)
output += "\n"
output += "We have done for now."
status = '200 OK'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Important notes:
- The GPIO access needs superuser role (sudo). But the apache uses the www-data user who is not a superuser. One possible solution is to add the www-data user to the group pi as described here. (Or to add www-data to the root group.)
- Be aware of the security issues!
- As this code is run through the apache webserver, it is not easy to debug the code. You can continually check the last ten apache error log in the terminal with this command:
tail -F /var/log/apache2/error.log | uniq -c
Development for the future:
- The music get confused when there are two events in the same time.
- The webpage does not give a response until the pattern is finished. So a longer music pattern can run into a timeout.
- Anybody can make me crazy if he knows my public ip.
- It cannot hit more than one rod in the same time.
- API for switch off/on? It can be useful for an afternoon nap.
Do you have any other comment, idea, suggestion?
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.