For those using my
motion sensor script with their R'Pints tap list display who might be interested in having the 'Pi play an audio track when the screen wakes up, it doesn't take much to do.
Basically install/update the sound drivers, install an mp3 player, add a couple of lines to the python script that runs the motion sensor, plug in a speaker and you're done.
First, install the alsa audio drivers and MP3 Player:
$ sudo apt-get install alsa-utils
$ sudo apt-get install mpg321
Reboot the system
Set the system to load the sound drivers on startup, configured for the 3.5mm jack output:
$ sudo modprobe snd_bcm2835
$ sudo amixer cset numid=3 1
Plug a cheap speaker into the audio jack. I dug up a pair of ancient Labtec peecee speakers (with the original wall wart!) and they work just peachy.
Note: for those using an HDMI display with integrated sound, use this amixer setting instead to send sound through the HDMI port:
$ sudo amixer cset numid=3 2
Now you can play an mp3 file using mpg321 to verify the sound is working.
$ mpg321 filename.mp3
To have the motion sensor script play sound when triggered, edit pir_run.py (should be in /home/pi) and add the two bolded lines, noting you want to provide the file name of your intended mp3 file (put it in /home/pi and all you need to change is the 'filename' string).
[And don't try to copy the code in its entirety from this, the formatting is fubar and you'll just get python parsing errors up the wazoo. Do the edits!]
$ sudo nano pir_run.py
#!/usr/bin/env python
import os
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
cmd = 'xscreensaver-command -deactivate'
playsound = 'mpg321 /home/pi/filename.mp3'
PIR_PIN = 7
GPIO.setup(PIR_PIN, GPIO.IN)
def MOTION(PIR_PIN):
os.system(cmd)
os.system(playsound)
try:
GPIO.add_event_detect(PIR_PIN, GPIO.RISING, callback=MOTION)
while 1:
time.sleep(100)
except KeyboardInterrupt:
GPIO.cleanup()
GPIO.cleanup()
Save the file (ctrl-o ctrl-x) and you should be good to go...
Cheers!