Python Raspberry Pi:GPIO引脚通过GPIO.setup()变高

Python Raspberry Pi:GPIO引脚通过GPIO.setup()变高,python,raspberry-pi,gpio,raspbian-buster,Python,Raspberry Pi,Gpio,Raspbian Buster,我目前的问题是,当我使用Gpio.setup(17,Gpio.OUT)功能时,引脚会通电。关于这个问题,我读了很多书,但没有一本对我有用。我甚至重新安装了拉斯宾 脚本的工作方式应如下所示: 如果我从服务器收到信号,就会调用函数messageDecoder()。如果消息主题为“rpi/gpio”,则应调用函数setup_gpio(),然后调用(channel1)上的函数为管脚供电。但是调用setupgpio()时,pin已经通电了!但我不知道为什么。 有人有自己的解决方案吗 这是我的密码: imp

我目前的问题是,当我使用Gpio.setup(17,Gpio.OUT)功能时,引脚会通电。关于这个问题,我读了很多书,但没有一本对我有用。我甚至重新安装了拉斯宾

脚本的工作方式应如下所示:

如果我从服务器收到信号,就会调用函数messageDecoder()。如果消息主题为“rpi/gpio”,则应调用函数setup_gpio(),然后调用(channel1)上的函数为管脚供电。但是调用setupgpio()时,pin已经通电了!但我不知道为什么。 有人有自己的解决方案吗

这是我的密码:

import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time
import datetime as datetime

def setup_GPIO():  # !!! when that function is called the pin gets power
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(channel1, GPIO.OUT)

def on(pin):

    print("ON", pin)
    GPIO.output(pin, GPIO.HIGH) # !!! here the pin should get power, but it gets it already before

def off(pin):
    print("OFF", pin)
    GPIO.output(pin, GPIO.LOW)
    GPIO.cleanup()

def connectionStatus(client, userdata, flags, rc):
    mqttClient.subscribe("time")
    mqttClient.subscribe("rpi/gpio")


def messageDecoder(client, userdata, msg):
    print("topic: " , msg.topic, "payload: " , msg.payload,)

    if msg.topic == "time":
        ...
    
    elif msg.topic == "rpi/gpio":
        messageActiv = str(msg.payload.decode(encoding='UTF-8'))
    
        if messageActiv == "on":
            setup_GPIO() # !!! here I call the setup_GPIO() function and the pin gets power
        
            print("System is ON!")
            on(channel1) # !!! I could leave out that function and the pin would have power
        
        elif messageActiv == "off":
            print("System is OFF!")
            off(channel1)
        else:
            print("Unknown message!")
        
    else:
        print("Unknown topic!")

channel1 = 17

clientName = "RPI"
serverAddress = "192.168.8.138"

mqttClient = mqtt.Client(clientName)
mqttClient.connect(serverAddress)

if __name__ == '__main__':
    i = 0
    try:
        now = datetime.datetime.today()
        
        mqttClient.on_connect = connectionStatus
        mqttClient.on_message = messageDecoder
    
        mqttClient.loop_forever()
        
    except KeyboardInterrupt:
        print("Interrupt")
        mqttClient.disconnect()

提前感谢:D

设置要输出的管脚后,默认输出为高值。根据文档,您可以使用参数
initial=GPIO.HIGH
设置初始值

GPIO.setup(channel1,GPIO.OUT,initial=GPIO.HIGH)

上面的代码根据OP将初始值设置为低。不确定为什么会发生这种情况。如果你知道,请填写


根据OP评论中提供的信息编辑

我不知道。这很奇怪。我在RPi GPIO方面的经验有限。谢谢你的回答。现在,当我调用函数setup_GPIO()时,该引脚没有更多的电源。但是现在命令GPIO.output(channel1,GPIO.HIGH)不再起作用了。如果您运行命令
GPIO.output(channel1,GPIO.LOW)
我想知道初始设置是否会每隔一次调用就翻转一次?