Python脚本无法读取GPIO

时间:2018-02-22 20:54:40

标签: python raspberry-pi

我正在尝试在我的RPi背景中运行以下python脚本(mission_buttons.py)。其目的是等待两个物理按钮(GPIO引脚23和24)上的输入,并在读取任一引脚上的逻辑HI时,将字母“x”打印到终端或调用updateMission.js。

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BCM)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)

upload_mission = GPIO.input(23)
execute_mission = GPIO.input(24)

while(1):

    if execute_mission == True:
        print 'x'

    if upload_mission == True:
        subprocess.call(["node", "/home/pi/QRIDrone/RPIBuild/updateMission.js"], shell=False)

但是当我运行脚本时,按下按钮似乎什么也没做。 如果我将代码调整为:

if execute_mission == False:
    print 'x'

if upload_mission == False:
    subprocess.call(["node", "/home/pi/QRIDrone/RPIBuild/updateMission.js"], shell=False)

程序打印x并重复调用updateMission.js。是否存在阻止RPi读取GPIO的内容?

1 个答案:

答案 0 :(得分:0)

  1. 检查您是否连接了正确的引脚
  2. 您没有在同时阅读upload_mission = GPIO.input(23)。你之前只读过一次然后不是 你需要:

    upload_mission = GPIO.input(23)
    execute_mission = GPIO.input(24)
    while(1):
        upload_mission = GPIO.input(23)
        execute_mission = GPIO.input(24)
        if execute_mission == True:
            print 'x'
    
        if upload_mission == True:
            subprocess.call(["node", "/home/pi/QRIDrone/RPIBuild/updateMission.js"], shell=False