简体   繁体   English

Raspberry Pi:GPIO-pin 通过 GPIO.setup() 变高

[英]Raspberry Pi: GPIO-pin gets high by GPIO.setup()

I currently have the problem that when I use the Gpio.setup(17, GPIO.OUT) function, the pin gets power.我目前遇到的问题是,当我使用 Gpio.setup(17, GPIO.OUT) function 时,引脚会通电。 I have read a lot about this problem, but nothing has worked for me.我已经阅读了很多关于这个问题的内容,但对我没有任何帮助。 I have even reinstalled Raspbian.我什至重新安装了 Raspbian。

The script should work like this:脚本应该像这样工作:

If I get a signal from the server the function messageDecoder() is called.如果我从服务器收到信号,则会调用 function messageDecoder()。 If the message has the topic "rpi/gpio" the function setup_GPIO() should be called and then the function on(channel1) to supply the pin with power.如果消息的主题是“rpi/gpio”,则应调用 function setup_GPIO(),然后打开 function 为引脚供电。 But the pin already has power when setup_GPIO() is called.但是当 setup_GPIO() 被调用时,该引脚已经通电。 But I do not know why?但不知为什么? Does anyone have s solution?有人有解决方案吗?

Here is my code:这是我的代码:

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()

Thanks in advance:D先谢谢了

It appears the default output once setting the pin to be output is for the value to be high.将引脚设置为 output 后,默认 output 似乎是为了使该值变高。 Based on the docs, you can use the parameter initial=GPIO.HIGH to set the initial value.根据文档,您可以使用参数initial=GPIO.HIGH设置初始值。

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

The code above sets the initial value to low according to the OP.上面的代码根据 OP 将初始值设置为低。 Not sure why this happens.不知道为什么会这样。 Please fill in if you know.知道的请填写。

https://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/ https://sourceforge.net/p/raspberry-gpio-python/wiki/BasicUsage/

Edited based on info provided in comment by OP根据 OP 在评论中提供的信息进行编辑

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

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