moran1804
Posts: 4
Joined: Sat Apr 09, 2016 8:30 pm

Light Sensor MP3 Play

Sun Apr 10, 2016 10:43 am

Hi All,

sorry I'm fairly new to python.
i have installed a light sensor on the GPIO pins and what i want to happen is when there is light no MP3 to play.
when the room goes dark id like it to play the MP3 Looped.

i have managed to do just this with IF statements however whilst going through the loop it waits till the end of the MP3 to detect light and then kill the MP3. i have then tried to use GPIO.add_event_detect and that works however when it goes dark it starts looping the MP3 and does not shut off when light is detected.

what is the best way to program this so the minute it detects light it kills the MP3?

here is my latest script:

Code: Select all

#1 = Dark
#0 = Light
import os
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN) #Light Sensor on GPIO 4
GPIO.setup(18,GPIO.OUT) #LED im using to work along with the sound

def my_callback(channel):
    if GPIO.input(4):     # if port 18 == 1
        os.system('sudo mpg123 -q --loop -1 /home/pi/RetroPie/Test.mp3')
        print "Light is Dark"
        GPIO.output(18, True)
    else:                  # if port 18 != 1
        print "Light is Light"
        GPIO.output(18, False)
        os.system('sudo pkill mpg123')

GPIO.add_event_detect(4, GPIO.BOTH, callback=my_callback)
time.sleep(60)
GPIO.cleanup()
Really Hope someone can Help :)

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Light Sensor MP3 Play

Sun Apr 10, 2016 8:18 pm

Using os.system to launch mpg123 you turn over control of the program to mpg123

Launch mpg123 in a subprocess using subprocess.popen
This will allow your main process to run

Code: Select all


import subprocess
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN) #Light Sensor on GPIO 4
GPIO.setup(18,GPIO.OUT) #LED im using to work along with the sound

player = None


def my_callback(channel):
    global player
    if GPIO.input(4):
        # if the player is running we kill it
        # we don't want more than one copy running at a time
        # we are not tracking it so try to kill it, if it fails no harm
        try:
            player.kill()
        except:
            pass

        player = subprocess.Popen(['mpg123', '-q', '--loop', '-1', '/home/pi/RetroPie/Test.mp3'])
        print "Light is Dark"
        GPIO.output(18, True)
    else:    
        print "Light is Light"
        GPIO.output(18, False)

        # try to kill the player
        # we are not tracking it so try to kill it, if it fails no harm
        try:
            player.kill()
        except:
            pass


GPIO.add_event_detect(4, GPIO.BOTH, callback=my_callback)
time.sleep(60)
GPIO.cleanup()

moran1804
Posts: 4
Joined: Sat Apr 09, 2016 8:30 pm

Re: Light Sensor MP3 Play

Mon Apr 11, 2016 9:59 am

Wow thats Absolutely Great works just the way i want it too!!!

still trying to fully get my head around the code im confused were player.kill() comes from im gathering that will kill whatever the player variable is which is the command to run the sound.

what i need to do now is remove the time.sleep and have this run forever?

i tried using a while true: before the GPIO.add_event_detect(4, GPIO.BOTH, callback=my_callback)
and it failed due to the fact you can only do the GPIO.add_event once.

what would you suggest to loop this forever?

Thanks
Mike

tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: Light Sensor MP3 Play

Mon Apr 11, 2016 8:44 pm

everything is running in separate threads so try signal.pause() from the signal module, it should do what your looking for (no promises).

Code: Select all

import signal
signal.pause()
https://docs.python.org/2/library/signa ... gnal.pause

but if that doesn't work try a while loop

Code: Select all

while True:
    time.sleep(.5) 
As for the player.kill() and popen
https://docs.python.org/2/library/subpr ... Popen.kill
https://docs.python.org/2/library/subpr ... cess.Popen

Return to “Python”