覆盆子等待新闻microswitch

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了覆盆子等待新闻microswitch相关的知识,希望对你有一定的参考价值。

我正在用覆盆子创建我简单的家庭自动化。现在我可以打开车库门,门和阅读温度。服务器是节点js。现在我想检测车库门何时打开。我认为使用微动开关,但是如何检测何时被按下的最佳方式? python脚本中的无限循环?一个投票脚本?你能帮助我吗?谢谢

答案

基本上你正在寻找的是实现GPIO中断,你需要的只是RPi.GPIO模块。

但是在RPi.GPIO模块描述中,有一个警告:

请注意,此模块不适用于实时或时序关键应用程序。这是因为你无法预测Python什么时候会忙于垃圾收集。它也在Linux内核下运行,它不适合实时应用程序 - 它是多任务操作系统,而另一个进程可能优先于CPU,从而导致程序抖动。如果您追求真正的实时性能和可预测性,那就给自己买个Arduino吧!

我找到了三种实现任务的方法(我相信应该有更多):

轮询不断检查某些事情。例如,如果要确保程序尽快对按下按钮做出反应,则可以每秒检查按钮状态大约一万次。如果您需要快速反应,这很好,但它使用了相当多的计算机处理能力。就像您期望交付时无法完全关注一样,您的大部分CPU已用完此轮询。

#!/usr/bin/env python2.7
# script by Alex Eames http://RasPi.tv/
# http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

# GPIO 23 set up as input. It is pulled up to stop false signals
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

print "Make sure you have a button connected so that when pressed"
print "it will connect GPIO port 23 (pin 16) to GND (pin 6)
"
raw_input("Press Enter when ready
>")

print "Waiting for falling edge on port 23"
# now the program will do nothing until the signal on port 23
# starts to fall towards zero. This is why we used the pullup
# to keep the signal high and prevent a false interrupt

print "During this waiting time, your computer is not"
print "wasting resources by polling for a button press.
"
print "Press your button when ready to initiate a falling edge interrupt."
try:
    GPIO.wait_for_edge(23, GPIO.FALLING)
    print "
Falling edge detected. Now your program can continue with"
    print "whatever was waiting for a button press."
except KeyboardInterrupt:  
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit

当在第二个线程中检测到事件时,它会将其传回主线程(回调)。我们现在在RPi.GPIO中具有为中断启动新线程的能力,并指定在第二个线程中发生中断时将运行的一组指令(函数)。这是一个线程回调函数。

#!/usr/bin/env python2.7  
# script by Alex Eames http://RasPi.tv  

import RPi.GPIO as GPIO  
GPIO.setmode(GPIO.BCM)  

# GPIO 23 & 24 set up as inputs. One pulled up, the other down.  
# 23 will go to GND when button pressed and 24 will go to 3V3 (3.3V)  
# this enables us to demonstrate both rising and falling edge detection  
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  

# now we'll define the threaded callback function  
# this will run in another thread when our event is detected  
def my_callback(channel):  
    print "Rising edge detected on port 24 - even though, in the main thread,"  
    print "we are still waiting for a falling edge - how cool?
"  

print "Make sure you have a button connected so that when pressed"  
print "it will connect GPIO port 23 (pin 16) to GND (pin 6)
"  
print "You will also need a second button connected so that when pressed"  
print "it will connect GPIO port 24 (pin 18) to 3V3 (pin 1)"  
raw_input("Press Enter when ready
>")  

# The GPIO.add_event_detect() line below set things up so that  
# when a rising edge is detected on port 24, regardless of whatever   
# else is happening in the program, the function "my_callback" will be run  
# It will happen even while the program is waiting for  
# a falling edge on the other button.  
GPIO.add_event_detect(24, GPIO.RISING, callback=my_callback)  

try:  
    print "Waiting for falling edge on port 23"  
    GPIO.wait_for_edge(23, GPIO.FALLING)  
    print "Falling edge detected. Here endeth the second lesson."  

except KeyboardInterrupt:  
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit  
GPIO.cleanup()           # clean up GPIO on normal exit

实际上,我们并没有做很多与上次非常不同的事情,除了现在还有更多。我们将添加另一个按钮和另一个与第一个相同的线程回调函数(但在不同的GPIO端口上)。这只是为了表明您可以在一个程序中执行多个线程回调。在那之后,你的想象力是极限。 (实际上,GPIO端口的数量可能是限制。)

#!/usr/bin/env python2.7  
# script by Alex Eames http://RasPi.tv  
# http://RasPi.tv/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3  
import RPi.GPIO as GPIO  
GPIO.setmode(GPIO.BCM)  

# GPIO 23 & 17 set up as inputs, pulled up to avoid false detection.  
# Both ports are wired to connect to GND on button press.  
# So we'll be setting up falling edge detection for both  
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)  

# GPIO 24 set up as an input, pulled down, connected to 3V3 on button press  
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)  

# now we'll define two threaded callback functions  
# these will run in another thread when our events are detected  
def my_callback(channel):  
    print "falling edge detected on 17"  

def my_callback2(channel):  
    print "falling edge detected on 23"  

print "Make sure you have a button connected so that when pressed"  
print "it will connect GPIO port 23 (pin 16) to GND (pin 6)
"  
print "You will also need a second button connected so that when pressed"  
print "it will connect GPIO port 24 (pin 18) to 3V3 (pin 1)
"  
print "You will also need a third button connected so that when pressed"  
print "it will connect GPIO port 17 (pin 11) to GND (pin 14)"  
raw_input("Press Enter when ready
>")  

# when a falling edge is detected on port 17, regardless of whatever   
# else is happening in the program, the function my_callback will be run  
GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback, bouncetime=300)  

# when a falling edge is detected on port 23, regardless of whatever   
# else is happening in the program, the function my_callback2 will be run  
# 'bouncetime=300' includes the bounce control written into interrupts2a.py  
GPIO.add_event_detect(23, GPIO.FALLING, callback=my_callback2, bouncetime=300)  

try:  
    print "Waiting for rising edge on port 24"  
    GPIO.wait_for_edge(24, GPIO.RISING)  
    print "Rising edge detected on port 24. Here endeth the third lesson."  

except KeyboardInterrupt:  
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit  
GPIO.cleanup()           # clean up GPIO on normal exit

希望这一系列的教程能帮助你更好地理解Raspberry Pi GPIO中断,但我建议对Arduino进行一些研究,或者至少在C或C ++这样的低级语言中,你可以找到great tutorial on interrupts there

你也可以加入qazxsw poi,也许你会在那里找到更多的帮助。

以上是关于覆盆子等待新闻microswitch的主要内容,如果未能解决你的问题,请参考以下文章

《java精品毕设》基于javaweb宠物领养平台管理系统(源码+毕设论文+sql):主要实现:个人中心,信息修改,填写领养信息,交流论坛,新闻,寄养信息,公告,宠物领养信息,我的寄养信息等(代码片段

html 产品新闻模板片段

我实施滑动标签有什么问题?

JUC并发编程 共享模式之工具 JUC CountdownLatch(倒计时锁) -- CountdownLatch应用(等待多个线程准备完毕( 可以覆盖上次的打印内)等待多个远程调用结束)(代码片段

我的C程序在覆盆子pi上创建4Gb的文件时,它们的大小应为8字节

Raspberry Pi 和 Mplayer 声音循环错误