PIR sensor / wakes screen up when audio is detected



  • I needed a “module” that wakes my mirror up when i started playing audio from it… here’s the python code and shell scripts needed…

    import RPi.GPIO as GPIO
    import time
    import subprocess
    isMonitorOn = False
    isPlaying = False
    sleepyTimer = 180                       #seconds until mirror goes to sleep
    i = 0
    counter = 0
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(4, GPIO.IN)
    print "Starting up!"
    print isPlaying
    while True:
           i=GPIO.input(4)
           print counter
           proc = subprocess.Popen('./isMonitorOn.sh', stdout=subprocess.PIPE)
           isMonitorOn = bool(proc.stdout.read())
           proc1 = subprocess.Popen('./isAudioOn.sh', stdout=subprocess.PIPE)
           isPlaying = bool(proc1.stdout.read())
           if i==0:
              counter += 1
              if not isMonitorOn:
                if isPlaying:
                  subprocess.call("./wakeUp.sh")
                  isMonitorOn = True
              if isMonitorOn:
                if isPlaying:
                  counter = 0
                if counter >= sleepyTimer:
                  subprocess.call("./sleep.sh")
                  isMonitorOn = False
                  counter = 0
              time.sleep(1)
           elif i==1:
              counter = 0
              if isMonitorOn:
                time.sleep(1)
              else:
                subprocess.call("./wakeUp.sh")
                time.sleep(1)
    
    

    wakeUp.sh includes:

    #!/bin/bash
    /opt/vc/bin/tvservice --preferred && sudo chvt 6 && sudo chvt 7
    
    

    sleep.sh includes:

    #!/bin/bash
    /opt/vc/bin/tvservice -o
    

    isMonitorOn.sh includes

    #!/bin/bash
    isMonitorOn=$(/opt/vc/bin/tvservice --status | grep off | wc -l)
    if [ "$isMonitorOn" = "0" ]; then
      echo True
    fi
    

    isAudioOn.sh includes

    #!/bin/bash
    if grep -q RUNNING /proc/asound/card*/pcm*/sub*/status; then echo "True"; fi
    

    Python and Bash scripts need to be in the same folder!

    for now i’m running this via screen

    screen -dmS smartMotion python smartMotion.py
    

    connect to screen session

    screen -r smartMotion
    

    detatch from session

    CTRL + A + D
    


  • remember to do chmod +x shellscriptname.sh


Log in to reply
 

Looks like your connection to MagicMirror Forum was lost, please wait while we try to reconnect.