User avatar
phaze3131
Posts: 45
Joined: Thu Apr 30, 2015 3:50 am

MQTT Python Script Errors

Mon Oct 19, 2015 1:12 am

I'm running this python script to set my Rpi as a broker, accept clients who want to change the LED status on the Rpi breadboard.

Just confused about errors I get when I execute the script.

Here are the errors:
mqttlight.py:23: RuntimeWarning: No channels have been set up yet - nothing to clean up! Try cleaning up at the end of your program instead!
GPIO.cleanup() # clean up resources
Traceback (most recent call last):
File "mqttlight.py", line 63, in <module>
client = paho.Client(client_id=MQTT, protocol=paho.MQTTv31)
NameError: name 'MQTT' is not defined


Thanks for any help!

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: MQTT Python Script Errors

Mon Oct 19, 2015 7:55 am

Since we can't see the code you're running we can't help.

Try posting it here wrapped in [code]...[/code] tags.

You need to install and run mosquitto as your MQTT broker before running any client programs.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: MQTT Python Script Errors

Mon Oct 19, 2015 3:45 pm

phaze3131 wrote: client = paho.Client(client_id=MQTT, protocol=paho.MQTTv31)
NameError: name 'MQTT' is not defined
You are giving a client_id that you did not define when connecting to the broker

"client_id is the unique client id string used when connecting to the broker."

You can avoid using it (when clean_session is true), but if you want to use it for sure you need to define it :)

danjperron
Posts: 3503
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: MQTT Python Script Errors

Mon Oct 19, 2015 3:55 pm

Massi is correct

I use paho and my line is

Code: Select all

client = paho.Client('RFUnit')
You will have to previously set the variable MQTT to an ID string.
ex: MQTT='MyMqttClient'

User avatar
phaze3131
Posts: 45
Joined: Thu Apr 30, 2015 3:50 am

Re: MQTT Python Script Errors

Mon Oct 19, 2015 9:27 pm

I'm not sure how it got changed but that error line 63 was wrong and I changed it back to how the code reads below.

client = paho.Client(client_id=random_client_id, protocol=paho.MQTTv31)

After this change to the correct 'random_client_id string, the error went away.

So sorry about not posting the full code.

Code: Select all

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

# device credentials
device_id        = 'admin'      # * set your device id (will be the MQTT client username)
device_secret    = '313131'  # * set your device secret (will be the MQTT client password)
random_client_id = 'MQTT'      # * set a random client_id (max 23 char)

# -------------- #
# Board settings #
# -------------- #
buttonPin = 7
ledPin = 12

GPIO.setmode(GPIO.BOARD)  # use P1 header pin numbering convention
GPIO.cleanup()            # clean up resources
GPIO.setup(ledPin, GPIO.OUT)   # led pin setup
GPIO.setup(buttonPin, GPIO.IN)   # button pin setup


# --------------- #
# Callback events #
# --------------- #

# connection event
def on_connect(client, data, flags, rc):
    print('Connected, rc: ' + str(rc))

# subscription event
def on_subscribe(client, userdata, mid, gqos):
    print('Subscribed: ' + str(mid))

# received message event
def on_message(client, obj, msg):
    # get the JSON message
    json_data = msg.payload
    # check the status property value
    print(json_data)
    value = json.loads(json_data)['properties'][0]['value']
    if value == 'on':
        led_status = GPIO.HIGH
        GPIO.output(ledPin, GPIO.HIGH)
    else:
        led_status = GPIO.LOW
        GPIO.output(ledPin, GPIO.LOW)

    # confirm changes to Leylan
    client.publish(out_topic, json_data)


# ------------- #
# MQTT settings #
# ------------- #

# create the MQTT client
client = paho.Client(client_id=random_client_id, protocol=paho.MQTTv31)  # * set a random string (max 23 chars)

# assign event callbacks
client.on_message = on_message
client.on_connect = on_connect
client.on_subscribe = on_subscribe


# device topics
in_topic  = 'devices/' + device_id + '/get'  # receiving messages
out_topic = 'devices/' + device_id + '/set'  # publishing messages

# client connection
client.username_pw_set(device_id, device_secret)  # MQTT server credentials
client.connect("178.62.108.47")                   # MQTT server address
client.subscribe(in_topic, 0)                     # MQTT subscribtion (with QoS level 0)

# ------------ #
# Button logic #
# ------------ #

prev_status = GPIO.LOW
led_status  = GPIO.LOW
updated_at  = 0  # the last time the output pin was toggled
debounce    = 0.2  # the debounce time, increase if the output flickers

# Continue the network loop, exit when an error occurs
rc = 0
while rc == 0:
    rc = client.loop()
    button = GPIO.input(buttonPin)

    if button != prev_status and time.time() - updated_at > debounce:
        prev_status = button
        updated_at  = time.time()

        if button:
            led_status = not led_status
            button_payload = 'off'
            if led_status == GPIO.HIGH:
                button_payload = 'on'
            # effectively update the light status
            GPIO.output(ledPin, led_status)
            payload = { 'properties': [{ 'id': '518be5a700045e1521000001', 'value': button_payload }] }
            client.publish(out_topic, json.dumps(payload))

print('rc: ' + str(rc))

Return to “Python”