当按下按钮时,python 脚本在 Raspberry Pi 上自动运行
When press button, python script runs automatically on Raspberry Pi
网上找不到教程。
当我按下一个按钮时,我想要一些 python 脚本到 运行。我不想先在 Raspberry Pi 的终端上 运行 python 脚本,然后像一些教程提到的那样等待按钮被按下。我还希望在按下按钮后整个脚本变为 运行,而不是我必须在整个脚本持续时间内按下按钮才能变为 运行。
基本上,我希望脚本 运行 无需将 HDMI 显示器或鼠标连接到 Raspberry Pi 或 GUI 东西。只需按下一个按钮。
此外,如果有人有关于如何使用 GPIO 和代码设置按钮的图表,那将非常有帮助。
我该怎么做??我在上面找不到任何东西,它看起来很简单。
您总是需要一些程序来监控您的输入,无论是来自键盘、鼠标还是连接到 GPIO 的按钮。对于键盘和鼠标,OS 为您提供了此功能。因此,要从 GPIO 引脚触发程序,您需要编写如下脚本:
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(18)
if input_state == False:
subprocess.call(something)
# block until finished (depending on application)
这是一个按钮电路(来自this tutorial)
轮询的更有效替代方法是使用中断:
#!/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)\n"
raw_input("Press Enter when ready\n>")
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.\n"
print "Press your button when ready to initiate a falling edge interrupt."
try:
GPIO.wait_for_edge(23, GPIO.FALLING)
print "\nFalling 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
(来自 http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio)
网上找不到教程。
当我按下一个按钮时,我想要一些 python 脚本到 运行。我不想先在 Raspberry Pi 的终端上 运行 python 脚本,然后像一些教程提到的那样等待按钮被按下。我还希望在按下按钮后整个脚本变为 运行,而不是我必须在整个脚本持续时间内按下按钮才能变为 运行。
基本上,我希望脚本 运行 无需将 HDMI 显示器或鼠标连接到 Raspberry Pi 或 GUI 东西。只需按下一个按钮。
此外,如果有人有关于如何使用 GPIO 和代码设置按钮的图表,那将非常有帮助。
我该怎么做??我在上面找不到任何东西,它看起来很简单。
您总是需要一些程序来监控您的输入,无论是来自键盘、鼠标还是连接到 GPIO 的按钮。对于键盘和鼠标,OS 为您提供了此功能。因此,要从 GPIO 引脚触发程序,您需要编写如下脚本:
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
while True:
input_state = GPIO.input(18)
if input_state == False:
subprocess.call(something)
# block until finished (depending on application)
这是一个按钮电路(来自this tutorial)
轮询的更有效替代方法是使用中断:
#!/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)\n"
raw_input("Press Enter when ready\n>")
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.\n"
print "Press your button when ready to initiate a falling edge interrupt."
try:
GPIO.wait_for_edge(23, GPIO.FALLING)
print "\nFalling 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
(来自 http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio)