Created
July 1, 2020 09:13
-
-
Save ibot3/dd08d8e53869c35074b9c622471ff2f2 to your computer and use it in GitHub Desktop.
PIR Sensor display turn off
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
#!/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() |
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
[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