Skip to content

Instantly share code, notes, and snippets.

@ibot3
Created July 1, 2020 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibot3/dd08d8e53869c35074b9c622471ff2f2 to your computer and use it in GitHub Desktop.
Save ibot3/dd08d8e53869c35074b9c622471ff2f2 to your computer and use it in GitHub Desktop.
PIR Sensor display turn off
#!/usr/bin/python3
import RPi.GPIO as GPIO
import time
import datetime
import subprocess
from threading import Timer
last_motion = time.time()
GPIO.setmode(GPIO.BCM)
GPIO_PIR = 4
DISABLE_SECONDS = 600
GPIO.setup(GPIO_PIR,GPIO.IN)
print("Waiting for PIR initialization..")
while GPIO.input(GPIO_PIR) != 0:
time.sleep(0.1)
print("Ready..")
def motion(PIR_GPIO):
global last_motion
subprocess.call(["vcgencmd", "display_power", "1"])
last_motion = time.time()
print("{} - Motion detected!".format(datetime.datetime.now()))
GPIO.add_event_detect(GPIO_PIR, GPIO.RISING, callback=motion)
timer = None
def timer_trigger():
global last_motion
global timer
time_diff = time.time() - last_motion
print("Timer triggered. Diff: {}".format(time_diff))
if time_diff > DISABLE_SECONDS:
subprocess.call(["vcgencmd", "display_power", "o"])
print("Display powered off")
timer = Timer(DISABLE_SECONDS/2, timer_trigger)
timer.start()
timer_trigger()
[Unit]
Description=PIR Service
[Service]
Type=simple
Restart=always
WorkingDirectory=/home/pi
ExecStart=/home/pi/motion.py
[Install]
WantedBy=default.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment