Thanks @cruunnerr.
In my case, no LED involved hence should be simpler :)
I run PIR.PY externally (via PM2) to call monitor on/off shell script and everything seems to work fine i.e.: turn screen blank and back on does work. No error reported. Its just the adding your script does not seems to make any difference, the turn on still instant.
Here is my PIR.py file
#!/usr/bin/env python
import sys
import time
import RPi.GPIO as io
import subprocess
io.setmode(io.BCM)
SHUTOFF_DELAY = 15 # seconds delay to turn off
PIR_PIN = 17 # Pin 11 on the board
TURNON_DELAY = 10 # seconds delay to turn on
def main():
io.setup(PIR_PIN, io.IN)
turned_off = False
last_motion_time = time.time()
while True:
if io.input(PIR_PIN):
last_motion_time = time.time()
sys.stdout.flush()
if turned_off and time.time() > TURNON_DELAY:
turned_off = False
turn_on()
else:
if not turned_off and time.time() > (last_motion_time + SHUTOFF_DELAY):
turned_off = True
turn_off()
time.sleep(.1)
def turn_on():
subprocess.call("sh monitor_on.sh", shell=True)
def turn_off():
subprocess.call("sh monitor_off.sh", shell=True)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
io.cleanup()
Here is the monitor_off.sh
#! /bin/bash
export DISPLAY=:0
xset dpms force off
And Monitor_on.sh
export DISPLAY=:0
xset dpms force on