1

I wrote this code for controlling gpio pins

everything is ok but i cant turn on or off gpio pins

message will publish but it will not turn on or off gpio pins

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

# True is off
# False is on
# WTF

P1 = 17
P2 = 4
P3 = 3
P4 = 2
P5 = 27
P6 = 15
P7 = 18
P8 = 14

GPIO.setmode(GPIO.BCM)
GPIO.setup(P1, GPIO.OUT)
GPIO.setup(P2, GPIO.OUT)
GPIO.setup(P3, GPIO.OUT)
GPIO.setup(P4, GPIO.OUT)
GPIO.setup(P5, GPIO.OUT)
GPIO.setup(P6, GPIO.OUT)
GPIO.setup(P7, GPIO.OUT)
GPIO.setup(P8, GPIO.OUT)
time.sleep(0.4)
GPIO.output(P1, True)
GPIO.output(P2, True)
GPIO.output(P3, True)
GPIO.output(P4, True)
GPIO.output(P5, True)
GPIO.output(P6, True)
GPIO.output(P7, True)
GPIO.output(P8, True)


# pins = {"P1": 17, "P2": 4, "P3": 3, "P4": 2, "P5": 27, "P6": 15, "P7": 18, "P8": 14}
# pins = {1: 17, 2: 4, 3: 3, 4: 2, 5: 27, 6: 15, 7: 18, 8: 14}
# for i in pins.items():
#     GPIO.setup(i, GPIO.OUT)
#     GPIO.output(i, True)

def on_publish(client, userdata, mid):
    print("Payload Published: " + str(mid))


def on_connect(client, userdata, rc):
    print("Connecting...")
    if rc == 0:
        print("Connected!!!\nResult code: " + str(rc))
        # Subscribing
        client.subscribe("Lights/LED1")
    elif rc == 5:
        print("Auth error!\nReturn code: " + str(rc))


def on_message(client, userdata, msg):
    print("Message Received.")
    print("topic: " + str(msg.topic))
    print("Payload" + str(msg.payload))
    print("QOS" + str(msg.retain))

    if msg.payload == "exit(0)":
        client.disconnect()
        print("exit with code 0")

    # True is off
    # False is on
    if msg.topic == "Lights/LED1":
        if msg.payload == "0":
            GPIO.output(P1, True)
        if msg.payload == "1":
            GPIO.output(P1, False)
    # if msg.payload.decode() == "exit(0)":
    # client.disconnect()


def on_disconnect(client, userdata, rc):
    print("disconnecting...")
    client.loop_stop()


def on_log(topic, userdata, level, buf):
    print("Logs:" + str(buf))


def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed: " + str(mid))


def on_unsubscribed(client, userdata, mid):
    print("Unsubscribed: " + str(mid))


# instance of Client class
client = mqtt.Client()
# Callbacks
client.on_message = on_message
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_publish = on_publish
client.on_subscribe = on_subscribe
client.on_unsubscribe = on_unsubscribed

host = "raspberrypi"
port = 1883
keep_alive = 60

client.connect(host, port, keep_alive)
time.sleep(2)

client.loop_start()

while True:
    print("type 1")
    # time.sleep(1)
    client.publish("Lights/LED1", input())
    print("type 0")
    # time.sleep(1)
    # topic, message, qos
    client.publish("Lights/LED1", input())

I think it must print the code written in on_connect() but it doesn't print them

and also i can see the message published from MQTT-Dash android app

  • if nothing prints in on_connect, perhaps rc is something OTHER than 0 or 5 - try printing what rc is - also, you're publishing to MQTT ... but you never subscribe to any topics, so, even IF you managed to connect, you'd never get any messages – Jaromanda X Mar 8 at 9:08
  • I tried to print rc in on_connect but it didn't print anything. – amirhosein arabhaji Mar 8 at 12:59
  • Update: I managed to fix this by putting client.subscribe("Lights/LED1") after client.connect(). But i don't know why on_connect() function never called. – amirhosein arabhaji Mar 8 at 15:47
  • everything is ok but i cant turn on or off gpio pins ... why are you even talking about the GPIO pins ... the problem is that you are not receiving any messages .... it is like saying i can't race my automobile when the problem is that the engine does not work – jsotola Mar 9 at 1:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.