首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >树莓派SW-420振动传感器和Python

树莓派SW-420振动传感器和Python
EN

Stack Overflow用户
提问于 2018-02-19 03:54:22
回答 1查看 2K关注 0票数 0

我正在尝试创建一个应用程序,它将感知我的洗衣机是否打开。然后,当它完成后,我希望它能检测到这一点并闪烁我的色调灯泡。我在这个项目中使用了SW-420振动传感器和Python。我已经成功地让它识别振动,但不幸的是,它不是我需要的。以下是我当前的代码。

代码语言:javascript
运行
复制
#!/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)

我遇到的问题是,即使机器正在运行传感器寄存器,它也已经停止。我试过用

代码语言:javascript
运行
复制
GPIO.add_event_detect(channel, GPIO.RISING, bouncetime=300)

代码语言:javascript
运行
复制
GPIO.add_event_detect(channel, GPIO.FALLING, bouncetime=300)

两者都有相同的结果。我只需要它来注册机器已经启动,然后停止闪烁色调灯泡,并等待机器下一次启动。

我使用一个名为Huepl的Perl库与之交互,但代码的这一部分工作得很好。如果你需要更多的信息,我很乐意提供。提前谢谢你!

EN

回答 1

Stack Overflow用户

发布于 2018-02-19 14:31:42

在第一次检测到振动时通过回调设置started。每当您检测到振动时,通过回调将lastShakeTime设置为now。

在您的主循环中,检查是否在60秒内没有检测到:

代码语言:javascript
运行
复制
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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48855938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档