Magic Mirror – Turn off screen –> save energy

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.

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.


The Code:


# 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

class magicHandler(BaseHTTPRequestHandler):
    BUSYTIME = 10 # in sec
    lastop = 0
    def do_GET(self):
                now = time.time()
                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)
                    process2 = subprocess.Popen(bashCommand2, stdout=subprocess.PIPE, shell=True)
                    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"
                    status = "error"
                    scode = 404

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

        server = HTTPServer(('', PORT_NUMBER), magicHandler)
        print 'Started httpserver on port ' , PORT_NUMBER       

except KeyboardInterrupt:
        print '^C received, shutting down the web server'

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.