├── README.md └── main.py /README.md: -------------------------------------------------------------------------------- 1 | # PiDisplaySleep 2 | 3 | A powersaver code written in python3 to put your Pi's display to sleep when you are not near it (checks if your phone is connected to the wifi !). This code is mainly aimed to run on smart mirrors(I use MagicMirror). If you live in a dorm room like me and dont want your mirror up all day long when you are away: this does the purpose ! 4 | 5 | Set up : 6 | Enter your phone's or whatever devices' IP address in the script. 7 | Run the script at boot OR just run the main.py as : " python main.py & " through ssh and exit ssh with Ctrl + D. This will run the script in background until the next power down. 8 | 9 | Note: this project uses the nmap package to do network scans; make sure you install it on your device using 10 | $ sudo apt-get install nmap 11 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from os import popen,system 2 | from time import sleep 3 | state=1 #State of the display 1 On 0 Off 4 | ip="192.168.1.3" #Enter the IP address of the device that should keep the display awake 5 | 6 | while True: 7 | nmap_out=str(popen('nmap -sP '+ip).read()) #nmap command to scan on the given IP address 8 | sleep(2) 9 | 10 | if nmap_out.find('latency') == -1: #looks for the word "latency" in the output 11 | if state==0 : #this nested if makes sure that commands are not repeated 12 | pass 13 | else : 14 | system('vcgencmd display_power 0') #Bash command that turns off the display 15 | state=0 #Updating the display state variable 16 | 17 | elif nmap_out.find('latency') > 1: 18 | if state==1: 19 | pass 20 | else : 21 | system('vcgencmd display_power 1') #Bash command to turn on the display 22 | state=1 23 | 24 | sleep(5) #Scan rate in seconds 25 | --------------------------------------------------------------------------------