<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[PIR sensor / wakes screen up when audio is detected]]></title><description><![CDATA[<p dir="auto">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…</p>
<pre><code>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 &gt;= 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)

</code></pre>
<p dir="auto"><a href="http://wakeUp.sh" target="_blank" rel="noopener noreferrer nofollow ugc">wakeUp.sh</a> includes:</p>
<pre><code>#!/bin/bash
/opt/vc/bin/tvservice --preferred &amp;&amp; sudo chvt 6 &amp;&amp; sudo chvt 7

</code></pre>
<p dir="auto"><a href="http://sleep.sh" target="_blank" rel="noopener noreferrer nofollow ugc">sleep.sh</a> includes:</p>
<pre><code>#!/bin/bash
/opt/vc/bin/tvservice -o
</code></pre>
<p dir="auto"><a href="http://isMonitorOn.sh" target="_blank" rel="noopener noreferrer nofollow ugc">isMonitorOn.sh</a> includes</p>
<pre><code>#!/bin/bash
isMonitorOn=$(/opt/vc/bin/tvservice --status | grep off | wc -l)
if [ "$isMonitorOn" = "0" ]; then
  echo True
fi
</code></pre>
<p dir="auto"><a href="http://isAudioOn.sh" target="_blank" rel="noopener noreferrer nofollow ugc">isAudioOn.sh</a> includes</p>
<pre><code>#!/bin/bash
if grep -q RUNNING /proc/asound/card*/pcm*/sub*/status; then echo "True"; fi
</code></pre>
<p dir="auto">Python and Bash scripts need to be in the same folder!</p>
<p dir="auto">for now i’m running this via screen</p>
<pre><code>screen -dmS smartMotion python smartMotion.py
</code></pre>
<p dir="auto">connect to screen session</p>
<pre><code>screen -r smartMotion
</code></pre>
<p dir="auto">detatch from session</p>
<pre><code>CTRL + A + D
</code></pre>
]]></description><link>https://forum.magicmirror.builders/topic/2539/pir-sensor-wakes-screen-up-when-audio-is-detected</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Jul 2026 14:44:17 GMT</lastBuildDate><atom:link href="https://forum.magicmirror.builders/topic/2539.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 10 May 2017 17:49:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to PIR sensor / wakes screen up when audio is detected on Sat, 20 May 2017 14:22:26 GMT]]></title><description><![CDATA[<p dir="auto">remember to do chmod +x <a href="http://shellscriptname.sh" target="_blank" rel="noopener noreferrer nofollow ugc">shellscriptname.sh</a></p>
]]></description><link>https://forum.magicmirror.builders/post/21861</link><guid isPermaLink="true">https://forum.magicmirror.builders/post/21861</guid><dc:creator><![CDATA[andr1]]></dc:creator><pubDate>Sat, 20 May 2017 14:22:26 GMT</pubDate></item></channel></rss>