Close

Program Code v0.1 [outdated]

A project log for Audible notifications by a Grandfather's clock

Eight chimes can sound in different patterns whenever I receive a notification through IFTTT

borazsloborazslo 04/15/2016 at 14:320 Comments

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.

# -*- 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:

Development for the future:

Do you have any other comment, idea, suggestion?

Discussions