【发布时间】:2019-04-09 04:56:49
【问题描述】:
我正在尝试触发我的 Raspberry Pi 在 GPIO #4 的上升沿关闭。我最终想在启动时自动运行这个脚本。
我的 python 代码在文件 test1.py @ /home/pi 中
#!/usr/bin/python
print("Starting")
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(4, GPIO.RISING)
def my_callback(x):
print("Event Triggered")
command = "/usr/bin/sudo /sbin/shutdown -r now"
import subprocess
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print(output)
x=1
GPIO.add_event_callback(4,my_callback)
print("End")
我的终端代码是
sudo python test1.py
我的输出是
Starting
End
当我将上面的 python 代码输入到 Python shell 中时,我得到上面的输出,并且在触发 GPIO4 时,它会关闭。
当我从终端调用它时,我得到了上述输出,但在触发 GPIO4 时没有任何反应。
我错过了什么,所以这将在终端屏幕上工作?
【问题讨论】:
-
你确定它在引用 pin 时不应该处于
GPIO.BOARD模式吗? -
我应该引用正确的引脚,因为从 python 调用代码时可以正常运行。从终端调用文件 test1.py 时是否需要更改?
-
好吧,你是对的,我只是想知道; atm 这里没有 pi。如果您从终端执行,代码甚至会等待按下,还是直接退出?
-
在终端中,当我输入 "sudo python test1.py" 时,它会打印开始、结束,然后是 "pi@raspberrypi:~ $"。
-
好的,看来您必须启动一个主循环以保持脚本运行
标签: python terminal raspberry-pi gpio