使用python的带有覆盆子的简单存在检测器

使用python的带有覆盆子的简单存在检测器,python,raspberry-pi,detection,led,Python,Raspberry Pi,Detection,Led,我想用树莓皮制作一个状态检测器,用来判断房间里是否有人 到目前为止,原理非常简单和基本:我使用pir探测器来检测运动。在这第一步之后,我想使用一个led(例如),如果房间已满,led将为红色,如果房间空闲,led将为绿色。我不知道在那之后我能做些什么,但我想先在那方面取得成功。 使用网络,我编写了以下程序(有效): 我现在想做的是有一条包含房间状态的消息,因此该消息可以是“房间已满”或“房间已空” 例如,如果检测到移动(终端将打印“检测到移动”并在5秒后打印“准备检测”),则消息应为“房间已满”

我想用树莓皮制作一个状态检测器,用来判断房间里是否有人

到目前为止,原理非常简单和基本:我使用pir探测器来检测运动。在这第一步之后,我想使用一个led(例如),如果房间已满,led将为红色,如果房间空闲,led将为绿色。我不知道在那之后我能做些什么,但我想先在那方面取得成功。 使用网络,我编写了以下程序(有效):

我现在想做的是有一条包含房间状态的消息,因此该消息可以是“房间已满”或“房间已空”

例如,如果检测到移动(终端将打印“检测到移动”并在5秒后打印“准备检测”),则消息应为“房间已满”。如果10秒后未检测到任何移动,则信息将切换到“room empty”等

就这样!我知道用python做这件事非常简单和基本(这不是一个简单的问题),但我对python不是很熟悉,我不知道如何在所有这些“if”和“while”块中使用它。 你能帮我修一下吗,谢谢你,你离我太近了

让我们先跳到正确的地方。在第二个
while True
块中,代码已
休眠(等待)一段时间
在继续之前。下面是一些注释和格式更正:

while True:

    # this reads from your GPIO to set a variable
    Current_State = GPIO.input(GPIO_PIR)

    # if it detected motion or if there wasn't motion last time we checked..
    if Current_State==1 and Previous_State==0:
        print " Mouvement detecte !"

        # wait five seconds so we're not checking as fast as the cpu
        # will allow!
        time.sleep(5)
        Previous_State=1

    # this block has the same logic as above, but in reverse!
    elif Current_State==0 and Previous_State==1:
        # if we don't detect motion on gpio, print ready
        # this is where we need to keep track of how many times we didn't
        # detect motion.
        print " Pret "
        Previous_State=0

        time.sleep(1)
现在,让我们来做这件事。您可能不希望在GPIO.input(GPIO\u PIR)=1时阻止第一个
,因为它只会阻止线程,设置
Current\u State
,即使我们稍后才重新定义它(这也可能会阻止您的程序进入执行我们工作的实际
,而True:
循环)

以下是实现所需逻辑的清理版本的外观:

import RPi.GPIO as GPIO
import time
import urllib

GPIO.setmode(GPIO.BCM)
GPIO_PIR = 7
GPIO.setup(GPIO_PIR,GPIO.IN)

Previous_State = 0
Pret_Counter = 0
pret_message = None

try:
    # this will only print the first time.
    print "Attente detection..."

    # this loop will continuously run
    while True:

        Current_State = GPIO.input(GPIO_PIR)

        # if we have a message, print it!
        if pret_message:
            print pret_message

        if Current_State and Previous_State==0:
            print "Mouvement detecte!"
            time.sleep(5)
            Previous_State=1
            # Someone moved.  reset detection counter.
            Pret_Counter = 0

        elif Pret_Counter > 9:
            # if we've been empty for 10 seconds,
            # don't bother incrementing our counter
            pret_message = "Room empty"

        elif Current_State==0 and Previous_State:
            print "Pret"
            Pret_Counter += 1
            Previous_State=0
            time.sleep(1)

except KeyboardInterrupt:
    print "Quit"
GPIO.cleanup()
我手头没有树莓pi来测试GPIO或pir检测器的行为,但这应该可以做到

另外,您可能想稍微使用一些阈值——就像您的代码现在一样,您每5秒只检查一次运动。如果两次都没有检测到运动,则您将房间标记为空。我建议使用与新的空逻辑类似的技巧——每2秒检查一次,可能10次(有时会议很无聊,人们在决定会议是空的之前都会做些小动作)

作为旁注,您应该阅读Python教程,例如,如果您想继续学习旧版本,则应阅读官方版本,或者阅读了解Python编程当前状态的版本

import RPi.GPIO as GPIO
import time
import urllib

GPIO.setmode(GPIO.BCM)
GPIO_PIR = 7
GPIO.setup(GPIO_PIR,GPIO.IN)

Previous_State = 0
Pret_Counter = 0
pret_message = None

try:
    # this will only print the first time.
    print "Attente detection..."

    # this loop will continuously run
    while True:

        Current_State = GPIO.input(GPIO_PIR)

        # if we have a message, print it!
        if pret_message:
            print pret_message

        if Current_State and Previous_State==0:
            print "Mouvement detecte!"
            time.sleep(5)
            Previous_State=1
            # Someone moved.  reset detection counter.
            Pret_Counter = 0

        elif Pret_Counter > 9:
            # if we've been empty for 10 seconds,
            # don't bother incrementing our counter
            pret_message = "Room empty"

        elif Current_State==0 and Previous_State:
            print "Pret"
            Pret_Counter += 1
            Previous_State=0
            time.sleep(1)

except KeyboardInterrupt:
    print "Quit"
GPIO.cleanup()