Python Forum
Simple python script for pir motion sensor
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple python script for pir motion sensor
#1
Hey guys, first post here so be gentle.

I managed to throw together this bit below by looking at other pir sensor scripts to control a set of hue strips. Its probably a million better ways to do this but this is where im at.

The problem is that this scripts overrides whatever i do from say my phone app.
If i turn on the lights from my phone and there is no movement next to the sensor it will kill the lights after a sec or two. This i want to avoid, i would like this script to be able to be overrun by the controls elsewhere. Could someone please help me out with this?

import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.IN)         #Read output from PIR motion sensor
while True:
       i=GPIO.input(15)
       if i==0:                 #When output from motion sensor is LOW
             print "No movement detected - Turning lights off",i
             exec(open("./LightsOff.py").read(), globals())
             time.sleep(1)
       elif i==1:               #When output from motion sensor is HIGH
             print "Movement detected - Turning lights on",i
             exec(open("./LightsOn.py").read(), globals())
             time.sleep(60 * 30)
Edit; the time.sleep (60 * 30) is used because i want the light to stay on for 30 minutes after there has been movement infront of the pir sensor. There is probably a better solution to this somewhere?

The two scripts being executed is merely a couple of scripts telling the hue bridge to turn lights on/off.
Let me know if those are needed for any reason and i will post them.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  motion tracking script ig? k4ne 0 435 Dec-13-2023, 02:00 AM
Last Post: k4ne
  Howto do motion event on solely window and not the widgets on it? janeik 3 789 Jul-11-2023, 12:10 AM
Last Post: deanhystad
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,009 Jun-29-2023, 11:57 AM
Last Post: gologica
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,620 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  Simple Python script, path not defined dubinaone 3 2,654 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Need help creating a simple script Nonameface 12 4,426 Jul-14-2020, 02:10 PM
Last Post: BitPythoner
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,790 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  Creating Data Set for Changing Oscillation Motion jcraw77 0 1,557 Jun-19-2020, 12:25 AM
Last Post: jcraw77
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,248 May-28-2020, 05:27 PM
Last Post: micseydel
  Detect event by motion/image - Help Zarqua 0 1,500 May-12-2020, 10:41 AM
Last Post: Zarqua

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020