Program Code v0.2

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/20/2016 at 04:110 Comments

After the Program Code v0.1, I made a better but still simple code. There is no need for wsgi module anymore. But there is need of zmq module. See the new Preparing the Raspberry Pi.

The new system works with three files. The runs continuously and it is waiting for calling on port 5555. The can send a request for pattern to the service. This python file can be called from cron, from other scripts, etc. The chimes.php is the file on the webserver. It can call the chimeClient when that is needed.

import sys, zmq
import RPi.GPIO as GPIO
import time, datetime, re

port = "5555"
#Connect the RodNumbers to the GPIO board numbers.
chimeRod2Pin = {0: 37, 1: 36, 2: 33, 3: 31, 4: 29}

for pin in chimeRod2Pin:
    GPIO.setup(chimeRod2Pin[pin], GPIO.OUT,initial=GPIO.LOW) 

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:%s" % port)

power = float

def calculatePower():
    power = 0.06
    now =
    if ( now.hour > 21 ) or (now.hour < 9 ):
        power = 0.03
        power = 0.05
    return power

def hitRod( number ):    
    GPIO.output(chimeRod2Pin[number], GPIO.HIGH)
    GPIO.output(chimeRod2Pin[number], GPIO.LOW)

def hitPattern( pattern ):
    hit = map(str, pattern.split('-'))
    for i in range(0, len(hit)):
        if hit[i] != '':
            tmp = map(float, hit[i].split(':'))

def validatePattern( subject ):
    pattern = "^([0-4]{1}:\d{1}(\.\d{1,2}|)(-|$))+$"
    matchObj = re.match(pattern, subject)            
    if not matchObj:        
        return False
        return True

while True:
    #  Wait for next request from client
    message = socket.recv()
    print "Received request: ", message,
    if not validatePattern(message):
        print "=> Invalid pattern!!"
        socket.send("Invalid pattern!")
        power = calculatePower()
        print "=> Done."

import sys, zmq

port = "5555"

context = zmq.Context()
print "Connecting to server..."
socket = context.socket(zmq.REQ)
socket.connect ("tcp://localhost:%s" % port)

if len(sys.argv) > 1:
    pattern = sys.argv[1]
    print "Sending pattern: ", pattern
    socket.send (pattern)  
    message = socket.recv()
    print "Received reply: ", message


<strong>Let's make some noise!</strong><br/><br/>
Please send the request pattern in the url.<br/>
See for example: <i>?pattern=4:0.5-3:0.5-2:0.5-1:0.5-0:1-0:1</i><br/><br/>

$pattern = $_REQUEST['pattern'];
if(!validatePattern($pattern)) {
	echo "<strong>Invalid pattern! Please see the example.</strong>";

echo "<strong>Current request:</strong> ".$pattern;

/* Run the pattern without waiting for response */
exec('python /path/to/file/ "'. $pattern .'" > /dev/null 2>/dev/null &');

/* Run the pattern and wait for the response 
exec('python /path/to/file/ "'. $pattern .'" ',$output,$return);
echo "<pre>";
foreach($output as $line)
	echo $line."\n";
echo "</pre>";

function validatePattern($pattern) {
	if(preg_match('/^([0-4]{1}:\d{1}(\.\d{1,2}|)(-|$))+$/', $pattern))
		return true;
		return false;

Important notes:

Development for the future:

Do you have any other comment, idea, suggestion?