0
これを行う最善の方法はわかりませんし、睡眠がうまくいかないときにプログラムをロックするように私のために働いていません...GPIO入力が変更された場合のカウントダウンスレッドRPiの停止
私はドアが開かれている時間がx時間以上であれば、ドアが開かれてラズベリーパイで閉じられるのを監視しようとしています。カウントダウンが終了してカウントダウンを止めずにスレッドを停止させ、アラート側を実装していますが、コードが現時点ではカウントダウン前にドアが閉じてもアラートをトリガーします。
私はテストのためにドアセンサーの代わりにプッシュボタンを使用していますが、最終的に私はドアの開閉を記録しますが、今のところIDはもっと良い方法があるかどうかを知りたいこれをやって、私はこのpost
から使用してコードイムを持っ
#!/usr/bin/python
import threading, subprocess, sys, time, syslog
import RPi.GPIO as GPIO
lim = 2 # seconds until warning
# thread for countdown (should be interruptable)
class CountdownTask:
global dooropen
global countdone
def __init__(self):
#print("thread in")
self._running = True
def start(self):
print("thread in")
self._running = True
def terminate(self):
print("thread killed")
self._running = False
def run(self, n):
while True:
global countdone
while self._running and dooropen == False and countdone:
pass
while self._running and dooropen == False and countdone == False:
pass
while self._running and dooropen and countdone:
pass
while self._running and dooropen and countdone == False:
print("start timer")
time.sleep(5)
if dooropen:
## action when timer isup
print("timer ended, send notify")
countdone = True
c = CountdownTask()
t = threading.Thread(target=c.run, args=(lim,))
t.daemon = True
REED = 23 # data pin of reed sensor (in)
# GPIO setup
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(REED, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
dooropen = False # assuming door's closed when starting
countdone = True
def edge(channel):
global dooropen
global countdone
if GPIO.input(REED): # * no longer reached
if dooropen == False: # catch fridge compressor spike
print("Detect open")
countdone = False
dooropen = True
else:
print("Door closed")
dooropen = False
def main():
GPIO.add_event_detect(REED, GPIO.RISING, callback=edge, bouncetime=300)
t.start()
while True:
pass
#------------------------------------------------------------
if __name__ == "__main__": main()
を更新、次のように私のコードは次のとおりです。
私はthreading.Eventを使用する必要があるように見えるとCOUを待つが、誰かが私のコードにこれを実装する方法をアドバイスしてくれましたか?