Problem: I want to turn the screen off/on depending on when the PIR sensor detects motion. It seems that the sensor is working, but when the TV is “off” instead of a black screen, it displays “No signal.” The TV is CEC-enabled, so I am under the impression that my goal should be possible.
I am using a RBP 4. Given that the screen does activate when movement is detected, I am assuming that the PIR sensor is properly connected.
Currently, I have the following Python script, pir.py
, run on start:
#!/usr/bin/env python
import sys
import time
import RPi.GPIO as io
import subprocess
io.setmode(io.BCM)
SHUTOFF_DELAY = 10 # in seconds, how long the monitor will be on until next button press or PIR detection
PIR_PIN = 4 # 7 on the board (this needn't to be a PIR. Can be a button also)
#LED_PIN = 16 # optional, don't use as Relay-PIN. It just shows detection time of the PIR without delay time
def main():
io.setup(PIR_PIN, io.IN)
# io.setup(LED_PIN, io.OUT)
turned_off = False
last_motion_time = time.time()
while True:
if io.input(PIR_PIN):
last_motion_time = time.time()
# io.output(LED_PIN, io.LOW)
print ".",
sys.stdout.flush()
if turned_off:
turned_off = False
turn_on()
else:
if not turned_off and time.time() > (last_motion_time +
SHUTOFF_DELAY):
turned_off = True
turn_off()
# if not turned_off and time.time() > (last_motion_time + 1):
# io.output(LED_PIN, io.HIGH)
time.sleep(.1)
def turn_on():
subprocess.call("sh /home/pi/monitor_on.sh", shell=True)
def turn_off():
subprocess.call("sh /home/pi/monitor_off.sh", shell=True)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
io.cleanup()
monitor_on.sh
reads: sudo vcgencmd display_power 1 7
monitor_off.sh
reads: sudo vcgencmd display_power 0 7
I have tried:
- Using
tvservice -o
and tvservice -p
instead of vcgencmd display_power
. While the TV would turn off, it would not turn back on. (I only tried -p option, never -e. I don’t know if that makes a difference.)
- Using the MMM-PIR-Module which is a module for MagicMirror. These were the module settings I used:
{
module: 'MMM-PIR-Sensor',
position: "top_center", // Remove this line to avoid having an visible indicator
config: {
sensorPin: 4,
powerSavingDelay: 60, // Turn HDMI OFF after 60 seconds of no motion, until motion is detected again
preventHDMITimeout: 4, // Turn HDMI ON and OFF again every 4 minutes when power saving, to avoid LCD/TV timeout
supportCEC: true,
presenceIndicator: "fa-eye", // Customizing the indicator
presenceOffIndicator: "fa-eye", // Customizing the indicator
presenceIndicatorColor: "#f51d16", // Customizing the indicator
presenceOffIndicatorColor: "#2b271c" // Customizing the indicator
}
}
Sometimes I felt the module was not working, not turning the TV back on when there was motion. Other times, if working, it still produced the No Signal screen, instead of a blank screen.
Thank you in advance for any help!