OK, now I am confident my motion detection script enabling HDMI signal enable/disable is working correctly. For the info of anyone not up to date on the thread there have been a few attempts to get the motion sensor to put the monitor in and out of sleep mode when no motion is detected from the sensor. I came up with the script below that has been working for me.
Anyone interested in giving it a try can simply backup the existing pir_run.py script in /home/pi and then paste in the code below.
@ day_trippr. I know you have not had much luck but I am curious is changing the order of the commands would work any better? My theory is changing to another Virtual Terminal before killing the HDMI signal may hopefully save the colour settings for your X session?
Hope this helps others trying to get their monitor in and out of sleep mode via the PIR.
Code:
#!/usr/bin/env python
import pwd,os
import time
import RPi.GPIO as GPIO
pir_pin = 7
GPIO.setmode(GPIO.BCM)
GPIO.setup(pir_pin, GPIO.IN)
global start_time
start_time=time.time()
sleep_threshold = 60
monitor_status = 1
cmd_sleep = 'chvt 6 && tvservice -o'
cmd_awake = 'tvservice -p && chvt 7'
time.sleep(10)
while 1:
time.sleep(10)
elapsed_time = time.time() - start_time
print elapsed_time
print monitor_status
# turn off monitor after the sleep threshold
# monitor_status value avoids turning of HDMI signal if it is already off
if elapsed_time > sleep_threshold and monitor_status == 1:
print "Turning Off HDMI"
monitor_status = 0
os.system(cmd_sleep)
# turn monitor back on upon detection
if GPIO.input(pir_pin) and monitor_status == 0:
os.system(cmd_awake)
print "Turning on HDMI"
monitor_status = 1
time.sleep(10)
start_time=time.time()
if GPIO.input(pir_pin):
start_time=time.time()
print "Resetting elapsed time to keep monitor on while movement detected"
time.sleep(10)