簡體   English   中英

Raspberry Pi:GPIO-pin 通過 GPIO.setup() 變高

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

我目前遇到的問題是,當我使用 Gpio.setup(17, GPIO.OUT) function 時,引腳會通電。 我已經閱讀了很多關於這個問題的內容,但對我沒有任何幫助。 我什至重新安裝了 Raspbian。

腳本應該像這樣工作:

如果我從服務器收到信號,則會調用 function messageDecoder()。 如果消息的主題是“rpi/gpio”,則應調用 function setup_GPIO(),然后打開 function 為引腳供電。 但是當 setup_GPIO() 被調用時,該引腳已經通電。 但不知為什么? 有人有解決方案嗎?

這是我的代碼:

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

先謝謝了

將引腳設置為 output 后,默認 output 似乎是為了使該值變高。 根據文檔,您可以使用參數initial=GPIO.HIGH設置初始值。

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

上面的代碼根據 OP 將初始值設置為低。 不知道為什么會這樣。 知道的請填寫。

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

根據 OP 在評論中提供的信息進行編輯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM