我编写了一个小型 python 脚本,用于检查是否有东西从我的树莓派连接到特定 IP 地址,然后更改 3 个 GPIO 引脚之一的值,以打开已连接、未连接或连接错误的 LED 。该代码在 rpi 启动时运行,并在前 4.5-5 分钟内完美运行,然后就卡在代码中的任何点上。
#!/usr/bin/env python
# /etc/init.d/ping.py
### BEGIN INIT INFO
# Provides: ping.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Descripton: Enable service provided by daemon.
### END INIT INFO
import subprocess
import RPi.GPIO as GPIO
# Set up pins, address, GPIO
address = '10.101.60.131'
grn = 8
red = 10
yel = 12
GPIO.setmode(GPIO.BOARD)
GPIO.setup(grn, GPIO.OUT)
GPIO.setup(red, GPIO.OUT)
GPIO.setup(yel, GPIO.OUT)
GPIO.output(grn, GPIO.LOW)
GPIO.output(red, GPIO.LOW)
GPIO.output(yel, GPIO.LOW)
last = 5
# main loop, checks if network is there
while(1):
ret = subprocess.call(['ping', '-c', '3', address])
if (ret != last):
# network responded correctly
if ret == 0:
print('ping to ' + address + ' OK')
GPIO.output(grn, GPIO.HIGH)
GPIO.output(red, GPIO.LOW)
GPIO.output(yel, GPIO.LOW)
last = 0
# network was not located
elif ret == 2:
print('no response from ', address)
GPIO.output(grn, GPIO.LOW)
GPIO.output(red, GPIO.HIGH)
GPIO.output(yel, GPIO.LOW)
last = 2
# other error in setup
else:
print('ping to ', address, ' failed')
GPIO.output(grn, GPIO.LOW)
GPIO.output(red, GPIO.LOW)
GPIO.output(yel, GPIO.HIGH)
last = 3
如果我在启动之外运行代码(在 python IDLE 中启动它),它运行良好。如果需要更多信息,请告诉我。
最佳答案
我设法解决这个问题的方法是从 /etc/profile
运行 python 代码。在文件末尾的/etc/profile 中,我添加了 sudo python/home/pi/Desktop/filenamehere.py & ,然后保存并重新启动。
关于python - 我的 Python 脚本在 RPI 启动后大约 5 分钟后退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52302354/