保持Python脚本不断活动以检查更改

时间:2015-04-25 20:34:20

标签: python python-2.7 raspberry-pi

我正在尝试编写一个Python脚本来持续监视覆盆子Pi上的GPIO引脚。我使用event_detection编写了一个脚本,但它在完成时结束。有没有办法让它检查GPIO引脚的更改?

#!/usr/bin/python
import sys
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)

pin=37

GPIO.setup(pin, GPIO.IN)

def alarmChange(channel):
    if(GPIO.input(pin)):
        print 'Sensor tripped',channel
    else:
        print 'sensor no longer tripped', channel

GPIO.add_event_detect(pin, GPIO.BOTH, callback=alarmChange)

GPIO.cleanup()
print "DONE....I never want to get here, unless I kill the process"

1 个答案:

答案 0 :(得分:0)

使用带有虚拟while内容的sleep循环:

import time

try:
    while True:
        time.sleep(1)  # 1s
except KeyboardInterrupt:
    print("Bye!")