1

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
  • 187
  • 2
  • 5
  • 17

1 Answers1

1

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.

meuh
  • 6,119
  • 1
  • 20
  • 26
  • Command itself `xset dpms force on` and `off` works perfect in command line. The problem is, when in the python script, after command `xset 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? – tarzanno Jul 18 '15 at 12:52
  • @tarzanno Are you running any X11 client at all on the display? – meuh Jul 18 '15 at 13:05
  • I got an error on: ` if now-lastsignaled > maxidle:` `name "lastsignaled" is not defined'. How do I make it right and what does it make? Thank your for your help! – tarzanno Jul 18 '15 at 13:27
  • @tarzanno You need to initialise the variable `lastsignaled` before using it. I edited my answer. – meuh Jul 18 '15 at 13:31
  • You 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? – tarzanno Jul 18 '15 at 13:59
  • @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. – meuh Jul 18 '15 at 14:27
  • yes, 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 Jul 18 '15 at 14:35
  • How do I close the thread? It is solved. Thank you very much for your time! – tarzanno Jul 18 '15 at 15:16
  • @tarzanno click on the check mark beside the answer to toggle it from greyed out to filled in – meuh Jul 18 '15 at 15:33
  • Is there any way to completely shut down or put to stand-by the monitor now? – tarzanno Jul 18 '15 at 17:35
  • I see you've closed this thread and asked this in a [separate question](http://superuser.com/q/942468/458747). good idea – meuh Jul 19 '15 at 13:51