0
votes

I can't figure out where my problem is. Would be nice if anyone is able to help me. Mqtt works good, but everytime the GPIO gets triggered the program crashes.

Error:

Connected with result code 0

/test/light1 0 0 Traceback (most recent call last): File "garage.py", line 33, in client.loop_forever() File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1481, in loop_forever rc = self.loop(timeout, max_packets) File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1003, in loop rc = self.loop_read(max_packets) File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1284, in loop_read rc = self._packet_read() File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1849, in _packet_read rc = self._packet_handle() File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2305, in _packet_handle return self._handle_publish() File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2500, in _handle_publish self._handle_on_message(message) File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 2647, in _handle_on_message self.on_message(self, self._userdata, message) File "garage.py", line 22, in on_message GPIO.output(pin, GPIO.LOW) RuntimeError: The GPIO channel has not been set up as an OUTPUT

#!/usr/bin/env python
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO

pin = 15

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

GPIO.setup(pin, GPIO.OUT)
GPIO.setup(pin, GPIO.HIGH)

def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))

    client.subscribe("/test/light1")

def on_message(client, userdata, msg):
        print(msg.topic + " " + str(msg.payload))
        if msg.payload == "0":
                print("0")
                GPIO.output(pin, GPIO.LOW)
        if msg.payload == "1":
                print("1")
                GPIO.output(pin, GPIO.HIGH)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("192.168.178.60", 1883, 60)

client.loop_forever()
2
The error is clear you have not setup the GPIO channel as an output GPIO.setup(channel, GPIO.OUT, initial=GPIO.HIGH) chan_list = [11,12] # also works with tuples GPIO.output(chan_list, GPIO.LOW) # sets all to GPIO.LOW GPIO.output(chan_list, (GPIO.HIGH, GPIO.LOW)) # sets first HIGH and second LOW - jackotonye

2 Answers

1
votes

You are calling GPIO.setup(pin, GPIO.HIGH) to set the initial value (after setting mode output) when you should be calling GPIO.output(pin, GPIO.HIGH). Alternatively you could use GPIO.setup(pin, GPIO.OUT, initial=GPIO.HIGH).

0
votes

On fist glance, I would think the issue would be the GPIO.setup is not propagated to the defined functions. Rather than define them globally at the top, make a sub method

def gpio_setup():
  pin = 15

  GPIO.setwarnings(False)
  GPIO.setmode(GPIO.BCM)

  GPIO.setup(pin, GPIO.OUT)
  GPIO.setup(pin, GPIO.HIGH)

And then call that method in each of the other methods:

def on_message(client, userdata, msg):
  gpio_setup()

You could even start just by setting the GPIO.setup manually in each method to find where it breaks.