pir运动传感器的简单python脚本

时间:2017-10-04 05:37:44

标签: python raspberry-pi sensor light philips-hue

通过查看其他pir传感器脚本来控制一组色调条,我设法将这一点放在一起。它可能是一百万种更好的方法,但这就是我的目标。

问题是这个脚本会覆盖我说的手机应用程序。 如果我打开手机上的指示灯并且传感器旁边没有任何动作,它会在一两秒钟后熄灭灯。我想避免这种情况,我希望这个脚本能够被其他地方的控件覆盖。有人可以帮我解决这个问题吗?

编辑; time.sleep(60 * 30)被使用是因为我希望在pir传感器前面移动后光线保持30分钟。在某个地方可能有更好的解决方案吗?

正在执行的两个脚本只是一些脚本,告诉色调桥打开/关闭灯。 如果出于任何原因需要我们,请告诉我,我会发布它们。

import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.IN)
while True:
i=GPIO.input(15)
caseCommand = getinput()
if (i==0) and (caseCommand == 0):
    print "No movement detected - Turning lights off",i
    exec(open("./LightsOff.py").read(), globals())
    time.sleep(1)
if (i==1) or(caseCommand == 1):
    print "Movement detected - Turning lights on",i
    exec(open("./LightsOn.py").read(), globals())
    time.sleep(60 * 30)

编辑:代码现在如上所述。但不知道如何编写所述函数。 显然上面给我一个关于函数的错误..

1 个答案:

答案 0 :(得分:0)

你的程序正在做它应该做的事情!!!

如果有移动(i = GPIO.input(15))它会打开灯,如果没有移动则会关灯!!!

你必须在if语句中添加一些额外的案例!!!

例如:

caseCommand = getinput() # getinput() is a function you should write, it should get the input from your phone or whatever and can return 0 or 1 

# your if statement should change
if (i==0) and (caseCommand == 0): # both have to be 0 to turn off
# turn lights off
if (i==1) or(caseCommand == 1): # any one of them should turn the light on 
# turn light on

这是一个更详细的例子:

import RPi.GPIO as GPIO
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(15, GPIO.IN)

def getinput():
    path = "pathtoyourfile\yourfilename.txt"
    return_valu= ""
    with open(path ,"r") as f:
        for l in f:
            return_value = l #it will get the last line incase you have more than one line
    return return_value


while True:
    i=GPIO.input(15)
    caseCommand = getinput()
    if (i==0) and (caseCommand == 0):
        print "No movement detected - Turning lights off",i
        exec(open("./LightsOff.py").read(), globals())
        time.sleep(1)
    if (i==1) or(caseCommand == 1):
        print "Movement detected - Turning lights on",i
        exec(open("./LightsOn.py").read(), globals())
        time.sleep(60 * 30)

    # add a time.sleep 2 or 3 second so while loop takes a break and you can write on the file
    time.sleep(2)