0

I'm pretty new with Python and Raspberry Pi so please excuse if the issue is naive. I'm writing a program where the it plays a video when triggered by a PIR sensor. The trigger sensed is visible but the video plays when I terminate/stop the program in Thonny.

import os
import sys
from subprocess import Popen
import RPi.GPIO as GPIO
import time

movie1 = ("/home/pi/Desktop/2.mp4")
movie2 = ("/home/pi/Desktop/1.mp4") 

pir_sensor = 11

GPIO.setmode(GPIO.BOARD)

GPIO.setup(pir_sensor, GPIO.IN)

current_state = 0
try:
    while True:
        time.sleep(0.1)
        current_state = GPIO.input(pir_sensor)
        if current_state == 1:
            #print("GPIO pin %s is %s" % (pir_sensor, current_state))
            os.system('killall omxplayer.bin')
            omxc = Popen(['omxplayer', '-b', movie1])

except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()

The video should play immediately when triggered.

Aivar
  • 6,814
  • 5
  • 46
  • 78
  • Your mistake is to not manage *state*. Instead you always kill and then start omx-player. The right thing to kill when no signal is received, and to start if there is a signal. And that only *once*. – deets Jul 14 '19 at 13:21
  • thanks for the help. I was caught in other work so couldn't try this. This did help – Kashish Sharma Sep 07 '19 at 12:35

0 Answers0