Read the statement by Michael Teeuw here.
Turning monitor on/off when not in use.
-
following
-
This post is deleted! -
This post is deleted! -
@wi_brewer Hi I have a dual approach.( FYI my mirror turns the monitor off with the command
vcgencmd display_power 0
(raspberry pi 3, Philips monitor on HDMI). and on again withvcgencmd display_power 1
. Your mileage may vary. )In crontab I put my mirror off during the night and on again in the morning. My /etc/crontab has added the following:
# m h dom mon dow user command 30 0 * * * root mmscreenoff 00 6 * * * root mmscreenon
The script mmscreenoff is a shell script that I put in the standard path that does the following:
touch /var/lock/displayscreenoff.lck vcgencmd display_power 0 >/dev/null
As you can see it turns off the screen and puts a lock file, so other programs will know that the screen really need to be turned off, whetever else.
The mmscreenon script does the opposite.rm -f /var/lock/displayscreenoff.lck vcgencmd display_power 1 >/dev/null
So… These are the basics… The screen goes off every night.
Now I want during the day also to see if me, my wife or my son is at home. For this I adapted the script from https://github.com/RpDp-git/PiDisplaySleep (thanks!). I have my router assign IP …100,…101 and …102 to the three phones of myself, my wife and my son. The script is modified to first check if there is a lockfile, in that case it waits but does nothing useful. If there is no lockfile it will scan the network.
from os import popen,system,path from time import sleep state=3 #State of the display 1 On 0 Off, 3 = unknown ip="192.168.2.100-102" #Enter the IP address of the device that should keep the display awake while True: if not path.exists('/var/lock/displayscreenoff.lck') : nmap_out=str(popen('nmap -sP '+ip).read()) #nmap command to scan on the given IP address if nmap_out.find('latency') == -1: #looks for the word "latency" in the output if state==0 : #this nested if makes sure that commands are not repeated pass else : system('vcgencmd display_power 0') #Bash command that turns off the display state=0 #Updating the display state variable elif nmap_out.find('latency') > 1: if state==1: pass else : system('vcgencmd display_power 1') #Bash command to turn on the display state=1 sleep(900) #keep on for at least 15 minutes sleep(10) #Scan rate in seconds
I run this script via
pm2 start mmdaysleep.py
or any other way you want to keep the script running. If nmap isn’t installed at your system you should install it viasudo apt-get install nmap
.For the purists: it has grown this way, with the crontab job first and the python script later. Variations of the python scripts being stopped and started by crontab and/or having system timing considerations in the python script are also possible and maybe more logical. I just happen to like the relative simplicity and modularity of this solution.
Hope this helps.
Martin -
Thanks for the input, @cruunnerr and @martinkooij.
I ended up going with @cruunnerr’s approach as it required fewer changes to my existing setup.
Here is what I did:
- Created a file that lists the devices and their IPs that we want to evaluate. Filename: “devices”.
myphone,192.168.0.200 mywatch,192.168.0.201 wifephone,192.168.0.202 wifewatch,192.168.0.203
- Created a perl script, “ping.pl”, that pings the devices listed in the file. If it gets a response it will write the results to a file named “ping_results”.
#!/usr/bin/perl use Net::Ping; open(INFILE, "; close(INFILE); #open(OUTFILE, ">", "ping_results") or die("unable to write output: $!"); chomp(@ip_array); $p = Net::Ping->new(); foreach(@ip_array) { if($_ =~ /\d+.\d+.\d+.\d+/) { if($p->ping($&)) { open(OUTFILE, ">", "ping_results") or die("unable to write output: $!"); print OUTFILE ("$`is responding to ping.\n"); close(OUTFILE); } } }
- Added a crontab job to run the perl script at 04:55.
# m h dom mon dow command # Execute Perl Script to check for devices 55 4 * * * perl /home/pi/ping.pl
- Created a shell script, “rpi-hdmi-on.sh” that will check to see if “ping_results” exists. If it does it will turn the monitor on and then delete the “ping_results” file. Added a crontab job to execute this script at 05:00.
if [ -e ping_results ] then vcgencmd display_power 1 >/dev/null rm -f /home/pi/ping_results fi
# m h dom mon dow command # Turn HDMI On (05:00/5:00am) 0 5 * * * /home/pi/rpi-hdmi-on.sh
- Created a shell script, “rpi-hdmi-off.sh” that will turn the monitor off. Added a crontab job to execute this script at 21:30.
vcgencmd display_power 0 >/dev/null
# m h dom mon dow command # Turn HDMI Off (21:30/9:30pm) 30 21 * * * /home/pi/rpi-hdmi-off.sh
I’ve done some testing and it seems to be working well.