가장 좋은 방법은 확실치 않지만 제대로 작동하지 않을 때 자물쇠가 켜지면 프로그램을 잠글 때 내 기능이 작동하지 않습니다.GPIO 입력 변경시 카운트 다운 스레드 RPi가 중지
나는 x 시간 이상 문이 열려 있으면 (예 : 이메일) 같은 경고문을 보내면 문이 열리고 닫힐 때 모니터링을 시도합니다. 문이 닫히면 문제가 생깁니다. 카운트 다운은 카운트 다운을 멈추지 않을 것입니다. 카운트 다운이 멈추는 원인이됩니다. 또한 경고음을 구현하지 못하지만, 코드가 카운트 다운 이전에 문이 닫히는 경우에도 경고를 트리거합니다.
테스트를 위해 도어 센서 대신 푸시 버튼을 사용하는 순간에도 결국 문 열림과 닫음을 기록하지만 지금은 더 나은 방법이 있는지 알고 싶어합니다. 이렇게, 나는 다음과 같이 내 코드는이 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 대기 할 필요가 같은
가 보이는 누군가 내 코드에 이것을 구현하는 방법을 조언 해 주었습니까?