[英]keeping Python script alive constantly to check for changes
我正在嘗試編寫Python腳本來連續監視樹莓派上的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"
使用帶有偽sleep
內容的while
循環:
import time
try:
while True:
time.sleep(1) # 1s
except KeyboardInterrupt:
print("Bye!")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.