Skip to content

Instantly share code, notes, and snippets.

@cyrus007
Created October 3, 2019 05:27
Show Gist options
  • Save cyrus007/93f493c855baf3f92f2c0fbf413d7234 to your computer and use it in GitHub Desktop.
Save cyrus007/93f493c855baf3f92f2c0fbf413d7234 to your computer and use it in GitHub Desktop.
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()
@cyrus007
Copy link
Author

cyrus007 commented Oct 3, 2019

import RPi.GPIO as GPIO
import time
 
SENSOR_PIN = 23
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN, GPIO.IN)
 
def my_callback(channel):
    # Here, alternatively, an application / command etc. can be started.
    print('There was a movement!')
 
try:
    GPIO.add_event_detect(SENSOR_PIN , GPIO.RISING, callback=my_callback)
    while True:
        time.sleep(100)
except KeyboardInterrupt:
    print "Finish..."
GPIO.cleanup()

@cyrus007
Copy link
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)

@cyrus007
Copy link
Author

cyrus007 commented Oct 3, 2019

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