Raspberry Pi 重启后读取发送到引脚的电流是错误的

Raspberry Pi reading current sent to pin is wrong after reboot

当我 运行 下面的脚本打开灯 (state=on) 时,然后 运行 脚本在重新启动 raspberry pi 后再次出现,它告诉我 state=on ,什么时候应该说关闭。为什么会这样?有什么办法可以解决这个问题吗?

#!/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();

这是一篇解释 when/why/where 的好文章:RPi.GPIO basics 3 – How to Exit GPIO programs cleanly, avoid warnings and protect your Pi.