2

I currently have the problem that when I use the Gpio.setup(17, GPIO.OUT) function, the pin gets power. I have read a lot about this problem, but nothing has worked for me. I have even reinstalled Raspbian.

The script should work like this:

If I get a signal from the server the function messageDecoder() is called. 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. But the pin already has power when setup_GPIO() is called! 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

hardillb
  • 54,545
  • 11
  • 67
  • 105
Lukas
  • 251
  • 2
  • 13

1 Answers1

0

It appears the default output once setting the pin to be output is for the value to be high. Based on the docs, you can use the parameter initial=GPIO.HIGH to set the initial value.

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

The code above sets the initial value to low according to the OP. Not sure why this happens. Please fill in if you know.

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

Edited based on info provided in comment by OP

user1558604
  • 947
  • 6
  • 20
  • I do not. That is strange. I only have limited experience with RPi GPIO. – user1558604 Jul 05 '20 at 16:49
  • Thanks for the answer. Now the pin gets no more power when I call the function setup_GPIO(). But now the command GPIO.output(channel1, GPIO.HIGH) does not work anymore. – Lukas Jul 05 '20 at 16:54
  • what if you run the command `GPIO.output(channel1, GPIO.LOW)` I wonder if that initial setting flips every other call you have to make? – user1558604 Jul 05 '20 at 16:56