Created
October 3, 2019 05:27
-
-
Save cyrus007/93f493c855baf3f92f2c0fbf413d7234 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(23, GPIO.IN) #PIR | |
GPIO.setup(24, GPIO.OUT) #BUzzer | |
try: | |
time.sleep(2) # to stabilize sensor | |
while True: | |
if GPIO.input(23): | |
GPIO.output(24, True) | |
time.sleep(0.5) #Buzzer turns on for 0.5 sec | |
GPIO.output(24, False) | |
print("Motion Detected...") | |
time.sleep(5) #to avoid multiple detection | |
time.sleep(0.1) #loop delay, should be less than detection delay | |
except: | |
GPIO.cleanup() |
Author
cyrus007
commented
Oct 3, 2019
import time
from gpiozero import MotionSensor
pir = MotionSensor(4)
while True:
if pir.motion_detected:
print('Motion detected')
time.sleep(10)
import time
from threading import Thread
import RPi.GPIO as GPIO
import subprocess
class AutoTrigger():
def call_omxplayer(self):
print ("playing " + self.file_path)
pid = subprocess.call(['omxplayer', self.file_path], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
self.is_running = False
def play_song(self):
if not self.is_running:
self.song_thread = Thread(target=self.call_omxplayer, args=())
self.song_thread.start()
self.is_running = True
def __init__(self, pin, file_path):
self.pin = pin
self.file_path = file_path
self.is_running = False
GPIO.setup(pin, GPIO.IN)
'''
This is a hack (the callback) thanks for python closures!
'''
GPIO.add_event_detect(self.pin, GPIO.FALLING, callback=lambda x: self.play_song(), bouncetime=10)
def main():
GPIO.setmode(GPIO.BCM)
AutoTrigger(25, '/home/pi/FolderName/SongName.wav')
AutoTrigger(24, '/home/pi/FolderName/SongName2.mp3')
print ("Ready: !")
try:
while True:
pass
except KeyboardInterrupt:
GPIO.cleanup()
if __name__ == '__main__':
main()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment