Advertisement
anta40

tripwire.py

Dec 17th, 2015
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import subprocess
  3. import datetime
  4. import os
  5. import time
  6. import signal
  7. import threading
  8.  
  9. # set PIN 2 (mode BCM) sebagai input
  10. GPIO.setmode(GPIO.BCM)
  11. CHANNEL = 2
  12. GPIO.setup(CHANNEL, GPIO.IN)
  13.  
  14. is_playing = False
  15.  
  16. # Mengeksekusi perintah dalam interval waktu tertentu. Jika interval sudah dilewati, eksekusi perintah tsb dimatikan
  17. def timeout_cmd(command, timeout):
  18.     global is_playing
  19.     cmd = command.split(" ")
  20.     start = datetime.datetime.now()
  21.     proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  22.    
  23.     is_playing = True
  24.  
  25.     while proc.poll() is None:
  26.         time.sleep(0.1)
  27.         now = datetime.datetime.now()
  28.  
  29.         if (now-start).seconds > timeout:
  30.             os.kill(prod.pid, signal.SIGKILL)
  31.             os.waitpid(-1, os.WNOHANG)
  32.             is_playing = False
  33.         return None
  34.  
  35. while 1:
  36.     if GPIO.input(CHANNEL):
  37.         if not is_playing:
  38.             timeout_cmd("omxplayer /home/pi/video/blablabla.mp4", 15)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement