Python 2.7 重新启动后发送到pin的Raspberry Pi读取电流错误

Python 2.7 重新启动后发送到pin的Raspberry Pi读取电流错误,python-2.7,raspberry-pi,Python 2.7,Raspberry Pi,当我运行下面的脚本打开指示灯(state=on),然后在重新启动raspberry pi后再次运行脚本,它会告诉我state=on,何时应该显示off。为什么会发生这种情况?我有办法解决这个问题吗 #!/usr/bin/python import RPi.GPIO as GPIO #setup GPIO using Board numbering. pin physical number corresponds to gpio call GPIO.setmode(GPIO.BOARD) pin

当我运行下面的脚本打开指示灯(state=on),然后在重新启动raspberry pi后再次运行脚本,它会告诉我state=on,何时应该显示off。为什么会发生这种情况?我有办法解决这个问题吗

#!/usr/bin/python
import RPi.GPIO as GPIO
#setup GPIO using Board numbering. pin physical number corresponds to gpio call
GPIO.setmode(GPIO.BOARD)

pin=8
GPIO.setup(pin, GPIO.OUT)

state = GPIO.input(pin)
#print 'START: state is: ',state

if state==1:
    GPIO.output(pin, False) #lov current turns relay on which breaks circuit on NC and changes state to off
    status="off"
else:
    GPIO.output(pin, True)
    status="on"

print 'light is now: ',status

您需要调用
GPIO.cleanUp()


下面是一篇很好的文章,解释了何时/为什么/何地:

这肯定是答案,处理这种情况的正确方法是保持程序运行,然后通过sigint终止程序并调用cleanup吗?有更优雅的解决方案吗?@Rilcon42我不是UNIX专家,但使用sigint似乎是一个有效的选择。请记住,当你的电源被切断时,你的清理工作不会发生。同期发行: