2

First post and also first time experimenting with a PI & Python.

I have a pi-3 model B with the 7" touchscreen and PIR sensor. My goal is to have it set up so that it constantly displays the stream from my IP camera. To save power consumption I also want to include a motion detection to toggle the display on/off when motion is detected.

My startup.py script is as follows:

import os, time, logging
logging.basicConfig(filename='Startup.log',level=logging.DEBUG)
logging.info('Logging started')
from gpiozero import MotionSensor
#Start stream
os.system("omxplayer --aspect-mode stretch rtsp://Username:Password@IPaddress:port/videoMain")
#Screen OFF
os.system("bash -c 'echo 1 > /sys/class/backlight/rpi_backlight/bl_power'")
#PIR sensor function, motion detected -> screen ON, wait x seconds, screen off
pir = MotionSensor(4)
while True:
    if pir.motion_detected:
        #Screen ON
        logging.info('Motion detected')
        os.system("bash -c 'echo 0 > /sys/class/backlight/rpi_backlight/bl_power'")
        #Wait x seconds
        time.sleep(180)
        logging.info('timer reached')
        #Screen OFF
        os.system("bash -c 'echo 1 > /sys/class/backlight/rpi_backlight/bl_power'")

When running this script, omxplayer plays the stream, but it seems the python script does not continue. Only when killing omxplayer, the script continues, turning the screen on/off when motion is detected. How can I get the stream to play combined with the motion detection functionality?

2

When you call os.system(abc123) python will wait until that process has completed before continuing. You may want to consider using something that simply spawns a subprocess and continues like

import os, time, logging
from gpiozero import MotionSensor
import subprocess

logging.basicConfig(filename='Startup.log', level=logging.DEBUG)
logging.info('Logging started')

ON, OFF = 0, 1 # map these to be explicit (are these backwards?)

''' Simple func to switch screen state '''
def switch_screen(state=ON):
    logging.info('setting screen state to {}'.format(state))
    os.system("bash -c 'echo {} > /sys/class/backlight/rpi_backlight/bl_power'".format(state)) # let this block

process = subprocess.Popen("omxplayer --aspect-mode stretch rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov", shell=True) #Start stream

switch_screen(OFF)

pir = MotionSensor(4) #PIR sensor function, motion detected -> screen ON, wait x seconds, screen off

while True:
    if pir.motion_detected:
        #Screen ON
        logging.info('Motion detected')
        switch_screen(ON)
        time.sleep(180) #Wait x seconds
        logging.info('timer reached')
        switch_screen(OFF) #Screen OFF

# clean up, maybe GPIO too?
logging.info('stopping')
process.kill() # stop OMX player
switch_screen(ON) # turn this back on at the end?
logging.info('stopped')
  • Unfortunately this doesn't seem to work. I get the same result as before. However I currently use a workaround with the crontab file. As it was my intention to launch the script at startup, I lauch the omxplayer line and also the python script. This seems to work well so far but I don't know if this is best-practice. – KawaGreen Jul 26 '17 at 7:27
  • I've updated my example to better show what I meant. – tobyd Jul 26 '17 at 9:18
  • Same result, script won't continue to switch_screen(OFF). I wish to accomplish a permanent 24/7 streaming of the IP camera, I wish to turn on the screen when I walk towards it and let it turn off again after x seconds. That way I'll save power and won't burn in the screen. But it seems to be running well with my workaround. – KawaGreen Jul 26 '17 at 20:39
  • I tried this script with the Big Buck Bunny demo stuff and the only issue i had was having to run the script as sudo and adding a shell=True argument to Popen to satisfy omxplayer. I was getting an error on this call which might be what stopped you. Regardless, if you've found a work around then great, apologies for the dead end. – tobyd Jul 26 '17 at 22:34
2

for what it's worth or for others reading the question, you can solve this easily by using "&" in the call:

replace

os.system("omxplayer --aspect-mode stretch rtsp://Username:Password@IPaddress:port/videoMain")

by

os.system("omxplayer --aspect-mode stretch rtsp://Username:Password@IPaddress:port/videoMain &")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.