我正在尝试编写一个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"
答案 0 :(得分:0)
使用带有虚拟while
内容的sleep
循环:
import time
try:
while True:
time.sleep(1) # 1s
except KeyboardInterrupt:
print("Bye!")