我正在尝试创建一个应用程序,它将感知我的洗衣机是否打开。然后,当它完成后,我希望它能检测到这一点并闪烁我的色调灯泡。我在这个项目中使用了SW-420振动传感器和Python。我已经成功地让它识别振动,但不幸的是,它不是我需要的。以下是我当前的代码。
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess
#GPIO SETUP
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)
def start(channel):
if GPIO.input(channel):
print "Dryer has started!"
time.sleep(1)
else:
print "Dryer has stopped"
time.sleep(1)
#f = open("status", "w") #Used to create light status file
#subprocess.call(["perl", "/home/pi/huepi/huepi", "lights"], stdout=f); # creates status file
#subprocess.call(["perl", "/home/pi/huepi/huepi", "for", "1 3 4 5 6 10 11 12", "do", "blinkonce"]); # blinks hue bulbs
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300) # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, start) # assign function to GPIO PIN, Run function on change
# infinite loop
while True:
time.sleep(1)
我遇到的问题是,即使机器正在运行传感器寄存器,它也已经停止。我试过用
GPIO.add_event_detect(channel, GPIO.RISING, bouncetime=300)
和
GPIO.add_event_detect(channel, GPIO.FALLING, bouncetime=300)
两者都有相同的结果。我只需要它来注册机器已经启动,然后停止闪烁色调灯泡,并等待机器下一次启动。
我使用一个名为Huepl的Perl库与之交互,但代码的这一部分工作得很好。如果你需要更多的信息,我很乐意提供。提前谢谢你!
发布于 2018-02-19 14:31:42
在第一次检测到振动时通过回调设置started
。每当您检测到振动时,通过回调将lastShakeTime
设置为now。
在您的主循环中,检查是否在60秒内没有检测到:
import datetime
# infinite loop
while True:
time.sleep(1)
now = datetime.datetime.now()
if started and (now - lastShakeTime).total_seconds() > 60:
# do something with your oleds
轮询与基于事件:https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/
https://stackoverflow.com/questions/48855938
复制相似问题