I'm working on a project which will use a PIR motion sensor to turn on a connected HDMI monitor when there's someone in the room, and turn it off when they leave. I'm new to Linux, but not to Python. My code so far is:
import RPi.GPIO as GPIO
import time
from subprocess import call
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.IN)
while true:
i = GPIO.input(11)
if i ==0:
print("Screen off", i)
call("vcgencmd display_power 0")
time.sleep(1)
elif i ==1:
print("Screen on", i)
call("vcgencmd display_power 1")
time.sleep(30)
If I just type in vcgencmd display_power 0
or vcgencmd display_power 1
to a terminal, the commands work fine, but this script produces a
FileNotFoundError: [Errno 2] No such file or directory: 'vcgencmd display_power 1'
I've found questions asking how to turn the screen on and off, and all of these seem to do it from the command line, not a Python script.
How can I run the command from the Python script?
call
take more than one argument? e.g.,call("vcgencmd", "display_power", "0")
- i.e. the first argument is the command, and the subsequent arguments are the command line parameters passed to the command – Jaromanda X Jul 24 '18 at 02:21call(["vcgencmd", "display_power", "0"])
- it's very hard getting documentation for python for something likecall
! – Jaromanda X Jul 24 '18 at 02:22/usr/bin/vcgencmd
or usingPopen
rather thancall
? – PiEnthusiast Jul 24 '18 at 05:56