Screen-saver on and off at Raspberry Pi - script in Python
Install the x11-xserver-utils
package to get the xset
command.
Then you can use it to force the DPMS signals to the monitor to on or off.
You may need to set the DISPLAY
variable in the environment. Eg:
DISPLAY=:0 xset dpms force on
sleep 10
DISPLAY=:0 xset dpms force off
You can do something like this in python. Poll every second. Remember if you have set the display on or off. Note the time-of-day whenever your signal is active. When the time since last active is over 2 minutes, switch display off. Loosely:
import os, subprocess, time
os.environ['DISPLAY'] = ":0"
displayison = False
maxidle = 2*60 # seconds
lastsignaled = 0
while True:
now = time.time()
if GPIO.input(PIR):
if not displayison:
subprocess.call('xset dpms force on', shell=True)
displayison = True
lastsignaled = now
else:
if now-lastsignaled > maxidle:
if displayison:
subprocess.call('xset dpms force off', shell=True)
displayison = False
time.sleep(1)
If you are interacting with the screen, and want it to stay on during this time independently of your gpio, you are probably better off letting the standard X11 idle mechanism detect that 2 minutes idle have elapsed and so automatically switching the screen off. Just use your program to force the screen on.
You can set a 120 second idle timeout with a single call of:
xset dpms 120 120 120
and can then remove the force off from the python.
Related videos on Youtube
tarzanno
Updated on September 18, 2022Comments
-
tarzanno over 1 year
I'm a noob at programming in python. How do I make a script that awakes the monitor and put it to sleep on a condition?
import RPi.GPIO as GPIO PIR = 23 GPIO.setmode(GPIO.BCM) GPIO.setup(PIR, GPIO.IN) while True: if GPIO.input(PIR): """ There should be the "awake monitor" function """" else: """" There should be something that makes my script run over and over but after for example 2 minutes after there is no signal on PIR.
As you can see, I have a motion sensor, which I want to make my monitor awake from sleep every time it senses a motion, but after there is no motion in its area any more, after two minutes it should put monitor to sleep.
Can you please help me?
-
tarzanno almost 9 yearsCommand itself
xset dpms force on
andoff
works perfect in command line. The problem is, when in the python script, after commandxset dpms force off
the screen flashes black once, then returns to normal state. The other problem is, that my motion sensor is giving the data to RPi for 3 seconds as it maintains voltage on one pin, so what should be the loop I am using to break it after finishing the loop once? -
meuh almost 9 years@tarzanno Are you running any X11 client at all on the display?
-
meuh almost 9 years@tarzanno You need to initialise the variable
lastsignaled
before using it. I edited my answer. -
tarzanno almost 9 yearsYou are THE MAN!:) It works fine, but just once. After one black screen and coming back to the state without screensaver, the script is not breaking, but it is not working any more either. Maybe it is because the loop is not coming back to the start?
-
meuh almost 9 years@you will need to debug this yourself. You should know how to add
print
statements in the code to show variable values and your gpio signal. -
tarzanno almost 9 yearsyes, of course. I think I will just add some mouse cursor movement after waking up, because when I move it after waking up, the script makes the screen go black again.
-
tarzanno almost 9 yearsHow do I close the thread? It is solved. Thank you very much for your time!
-
meuh almost 9 years@tarzanno click on the check mark beside the answer to toggle it from greyed out to filled in
-
tarzanno almost 9 yearsIs there any way to completely shut down or put to stand-by the monitor now?
-
meuh almost 9 yearsI see you've closed this thread and asked this in a separate question. good idea