Magic Mirror – Turn off screen and save energy - Raspberry Pi

Robin Henniges
#hashtag

(This is a restored old article from 2016) My Smart Mirror is running 24h a day. I unplug the screen at night but well that really impractical. From our server-dashboard at work (3 HD TV displaying server load, memory, cpu usage etc.) i knew that it is possible to turn off the screen programmically on the raspberry pi. I wrote a short python script which allows you to shut down the screen by calling a url.

http://0.0.0.0:5511/on
http://0.0.0.0:5511/off

Solutions I had in mine.

  1. The one that I already presented

  2. Use a crontab to define a the moment when the screen should be turned on and when it should be turned off.

  3. User presence – scan wifi for the users smartphone mac. Also check the proximity based on the signal strength (libpcap would be helpful for this).

  4. Use a PIR sensor so that the mirror would react to movements.

#!/usr/bin/python

# author: robin henniges
# date: 05.05.2016
# description:  starts a http server and allows you to turn on/off
#               the screen on the hdmi port. Simply call this urls:
#               http://ip-adr:5511/on
#               http://ip-adr:5511/off
#
#               It may answer with { "status" : "busy" } that happens 
#               when you call it within the cool down phase. (see BUSYTIME)


from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from os import curdir, sep
import subprocess
import time
import os

# you need to set those env var for the xset command
os.environ['DISPLAY'] = ":0"
os.environ['XAUTHORITY'] = "/home/pi/.Xauthority"

# the port
PORT_NUMBER = 5511

class magicHandler(BaseHTTPRequestHandler):
    BUSYTIME = 10 # in sec
    lastop = 0
        
    def do_GET(self):
                now = time.time()
                mimetype='text/css'
                status = "unknown"
                scode = 200
                print "now: {} lastop: {} delta: {}".format(now, self.lastop,(now - self.lastop))
                busy = (now - self.lastop) < self.BUSYTIME
                print busy
                if self.path=="/on" and not busy:
                    bashCommand1 =  ["tvservice -p"]
                    bashCommand2 =  ["export DISPLAY=:0.0"]
                    bashCommand3 =  ["xset dpms force on"]

                    magicHandler.lastop = time.time()
                    process1 = subprocess.Popen(bashCommand1, stdout=subprocess.PIPE, shell=True)
                    time.sleep(1)
                    process2 = subprocess.Popen(bashCommand2, stdout=subprocess.PIPE, shell=True)
                    time.sleep(1)
                    process3 = subprocess.Popen(bashCommand3, stdout=subprocess.PIPE, shell=True)
                    #for line in iter(process3.stdout.readline,''):
                    #    print line.rstrip()
                    status = "on"
                    print "call on"
                elif self.path=="/off" and not busy:
                    bashCommand = "tvservice -o"
                    magicHandler.lastop = time.time()
                    process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
                    status = "off"
                    print "call off"
                elif busy:
                    status = "busy"
                else:
                    status = "error"
                    scode = 404

                self.send_response(scode)
                self.send_header('Content-type', mimetype)
                self.end_headers()
                self.wfile.write('{ "status": "'+status+'" }')
                return

try:
        server = HTTPServer(('', PORT_NUMBER), magicHandler)
        print 'Started httpserver on port ' , PORT_NUMBER       
        server.serve_forever()

except KeyboardInterrupt:
        print '^C received, shutting down the web server'
        server.socket.close()
logo

Follow me on social media

CONTACT

E-Mail :

mail at robinhenniges.com

ADDRESS

Gotenstr. 11
10829 Berlin

© 2023. All Rights Reserved.