简体   繁体   English

2个Python脚本使用相同的GPIO引脚RPI

[英]2 Python scripts use the same GPIO pin RPI

Is it possible to write 2 scripts 是否可以编写2个脚本

1 For setting a GPIO pin 1用于设置GPIO引脚

and 1 for reading out what the status of the gpio pin is. 和1用以读取gpio引脚的状态。

I now have written these two scripts in python. 我现在用python编写了这两个脚本。 but when I launch them both only 1 will work 但是当我启动它们时,只有1个可以工作

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.IN)
print GPIO.input(18)

The other one listens to a button and if the button is pressed the pin 18 is set to high, if he is pressed again the pin is set to low 另一个听一个按钮,如果按下按钮,则引脚18设置为高电平;如果再次按下按钮,则引脚设置为低电平。

#!/usr/bin/python
import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BCM)
pushbutton = 2
relay = 18

GPIO.setup(pushbutton, GPIO.IN)
GPIO.setup(relay, GPIO.OUT)

def main():
    ingedrukt = GPIO.input(pushbutton)
    try:
        while (True):
            if(ingedrukt == False):
                if(GPIO.input(pushbutton) == False):
                    sleep(0.5)
                    if(GPIO.input(pushbutton) ==False):
                        GPIO.output(relay, GPIO.HIGH)
                        ingedrukt = True
                        print "Pushed"
            else:
                if(GPIO.input(pushbutton) == True):
                    GPIO.output(relay, GPIO.LOW)
                    ingedrukt = False
                    print "Not pushed"
    except KeyboardInterrupt:
        print "Quit"
        GPIO.cleanup()
main()

Is this possible anyway if so, what am I doing wrong? 如果可以,这是否可能,我在做什么错?

You don't need to set the relay as a GPIO input to read the state. 您无需将继电器设置为GPIO输入即可读取状态。 All you need is the print GPIO.input(18) command. 您只需要print GPIO.input(18)命令。 So your first script should read: 因此,您的第一个脚本应为:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
print GPIO.input(18)

I would add the line 我会加线

GPIO.setwarnings(False)

You're trying to concurrently run two scripts? 您正在尝试同时运行两个脚本吗? Why not have the print statement in your listening script? 为什么在侦听脚本中没有打印语句? Also, your printing status of the pin script needs a while loop 另外,您的固定脚本打印状态需要一个while循环

What do you mean only one works? 您是什么意思? Maybe this helps? 也许有帮助吗?

It is possible that you have more than one script/circuit on the GPIO of your Raspberry Pi. Raspberry Pi的GPIO上可能有多个脚本/电路。 As a result of this, if RPi.GPIO detects that a pin has been configured to something other than the default (input), you get a warning when you try to configure a script. 结果,如果RPi.GPIO检测到某个引脚已配置为默认值(输入)以外的其他值,则在尝试配置脚本时会收到警告。 To disable these warnings: GPIO.setwarnings(False) 要禁用这些警告: GPIO.setwarnings(False)

from: http://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/ 来自: http : //sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM