4

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?

KawaGreen
  • 141
  • 1
  • 1
  • 4

2 Answers2

3

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 &")
pieterdp
  • 91
  • 8
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')