A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.
Read the statement by Michael Teeuw here.
HTTP Server for streaming podcasts / news / volume
-
Dear fellow Mirror Designers,
I wasn’t sure where to put this so I will post it here. From various sources I created a small python httpserver that changes the mirrors volume, starts a podcast (100 Seconds german news, when brushing your teeth:grimacing_face: ) or opens a live stream. This is the code, nothing special but may be helpful for some of you.
I use FHEM and Philips Hue Dimmer Switches to control it:
Uses:
python3, livestreamer, omxplayer#!/usr/bin/python3 -u host_name = '10.0.1.49' # Change this to your Raspberry Pi IP address host_port = 8000 proc = '' import RPi.GPIO as GPIO from pathlib import Path import os import subprocess import signal from subprocess import call from time import sleep from http.server import BaseHTTPRequestHandler, HTTPServer class MyServer(BaseHTTPRequestHandler): def do_HEAD(self): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() def do_GET(self): global proc html = ''' <html> <body style="width:960px; margin: 20px auto;"> <h1>Magic Mirror Control</h1> <p>Current GPU temperature is {}</p> <p>Podcast: <a href="/play">Play</a> <a href="/stop">Stop</a></p> <p>ARD Livestream: <a href="/ard">Play</a> <a href="/stop">Stop</a></p> <p>Volume: <a href="/down">down</a> <a href="/up">up</a></p> <p>System: <a href="/shutdown">Shutdown</a> <a href="/restart">Restart</a></p> <div id="podcast-status"></div> <script> document.getElementById("podcast-status").innerHTML="{}"; </script> </body> </html> ''' temp = os.popen("/opt/vc/bin/vcgencmd measure_temp").read() self.do_HEAD() status = '' if self.path=='/': #GPIO.setmode(GPIO.BCM) #GPIO.setwarnings(False) #GPIO.setup(17, GPIO.IN) status='nothing' elif self.path=='/play': if proc=='': my_file = Path("/home/pi/MagicMirror/video.mp4") if my_file.is_file(): statinfo = os.stat('/home/pi/MagicMirror/video.mp4') if statinfo.st_size > 0: proc = subprocess.Popen(["omxplayer", "-o", "alsa:hw:ALSA", "--no-osd", "/home/pi/MagicMirror/video.mp4"], preexec_fn=os.setsid) status='Playing Podcast' else: proc = subprocess.Popen(["omxplayer", "-o", "alsa:hw:ALSA", "--no-osd", "/home/pi/MagicMirror/Loading.mp4"], preexec_fn=os.setsid) status='Not yet available' else: proc = subprocess.Popen(["omxplayer", "-o", "alsa:hw:ALSA", "--no-osd", "/home/pi/MagicMirror/Loading.mp4"], preexec_fn=os.setsid) status='Not yet available' else: status='Stopping Podcast' os.killpg(os.getpgid(proc.pid), signal.SIGTERM) proc='' elif self.path=='/ard': if proc=='': proc = subprocess.Popen(["livestreamer", "hlsvariant://daserste_live-lh.akamaihd.net/i/daserste_de@91204/master.m3u8", "best", "-vnp", "omxplayer -o alsa:hw:ALSA --no-osd --timeout 20"], preexec_fn=os.setsid) status='Playing ARD Livestream' else: status='Stopping Stream' os.killpg(os.getpgid(proc.pid), signal.SIGTERM) proc='' elif self.path=='/stop': if proc!='': status='Stopping Podcast' os.killpg(os.getpgid(proc.pid), signal.SIGTERM) proc='' else: status='Nothing to stop' elif self.path=='/shutdown': status='Shutdown' subprocess.Popen(["sudo", "shutdown", "-h", "now"], preexec_fn=os.setsid) elif self.path=='/restart': status='Restart' subprocess.Popen(["sudo", "reboot"], preexec_fn=os.setsid) elif self.path=='/down': status='Volume down' subprocess.Popen(["amixer", "set", "Master", "12%-"], preexec_fn=os.setsid) elif self.path=='/up': status='Volume up' subprocess.Popen(["amixer", "set", "Master", "12%+"], preexec_fn=os.setsid) self.wfile.write(html.format(temp[5:], status).encode("utf-8")) if __name__ == '__main__': http_server = HTTPServer((host_name, host_port), MyServer) print("Server Starts - %s:%s" % (host_name, host_port)) try: http_server.serve_forever() except KeyboardInterrupt: http_server.server_close()