首页 文章

Raspberry Pi python在启动时

提问于
浏览
0

我试图在启动时在后台运行我的python脚本 . 当我想运行我的代码时,这种用法一直用于我,但现在似乎无法正常工作 . 我想我正在运行NOOBS .

sudo nano /etc/rc.local

#!/bin/sh -e

sudo python /home/pi/Firebase.py &


exit 0

Firebase.py

from firebase import firebase
import RPi.GPIO as GPIO
import time

firebase = firebase.FirebaseApplication('https://raspberrypi-5c0ce.firebaseio.com/', authentication = None)


#result = firebase.put('/light_switch','state', 'off')

#currentState = firebase.get('/light_switch', 'state')
#print(currentState)



#Initialize gpio settings
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#Initialize gpio board
input1 = 26
input2 = 19


#setup led color with gpio setup
GPIO.setup(input1, GPIO.OUT)
GPIO.setup(input2, GPIO.OUT)
#GPIO.setup(yellowLed, GPIO.OUT)

#output actual color of led based on variableColor and 1 for tru
while True:
    currentState = firebase.get('/light_switch', 'state')
#print(currentState)
    if currentState == "on":
        GPIO.output(input1, 1)
        GPIO.output(input2, 1)
    #print("on")
    else:
        GPIO.output(input1, 0)
        GPIO.output(input1, 0)
    #print("off")

1 回答

  • 0

    修复了睡眠所需的问题,因此它可以连接到wifi .

    #!/bin/sh -e
    # Print the IP address
    # Sleep introduced to see if Wifi acquired and IP assigned
    date >> /tmp/rc_local_b4_sleep
    sleep 15
    date >> /tmp/rc_local_after_sleep
    
    _IP=$(hostname -I) || true
    if [ "$_IP" ]; then
      printf "My IP address is %s\n" "$_IP"
    fi
    
    /sbin/ifconfig > /tmp/network_at_boot
    
    python /home/pi/Firebase.py &
    
    
    exit 0
    

相关问题