MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    HTTP Server for streaming podcasts / news / volume

    Scheduled Pinned Locked Moved General Discussion
    1 Posts 1 Posters 672 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Y Offline
      yep_DD
      last edited by yep_DD

      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:

      0_1540136723272_IMG_20181021_174249.jpg

      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()
      
      
      1 Reply Last reply Reply Quote 0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • 1 / 1
      • First post
        Last post
      Enjoying MagicMirror? Please consider a donation!
      MagicMirror created by Michael Teeuw.
      Forum managed by Sam, technical setup by Karsten.
      This forum is using NodeBB as its core | Contributors
      Contact | Privacy Policy