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