Paho-MQTT Error result code: 5

I managed to establish a connection between Paho-MQTT client in my RPi and MQTT broker in my VPS.

Now I'm trying to secure the MQTT connection and I have added user and password to the broker. I have changed mosquitto.conf file adding the new password file.

But now when I try to connect my Paho-MQTT cliente I get this error:

Error result code: 5

This is my paho-mqtt client script:

#!/usr/bin/env python

import paho.mqtt.client as mqtt
import subprocess
import datetime
import time

# Broker data
broker = *My Broker IP
broker_port = 1883
timeout_reconnect = 60
broker_topic = "#"  #All topics have to be read

# Topic to send payload
topic = "temp/DHT11data"

def on_connect(client, userdata, flags, rc):
    #print("Connect with result code: " + str(rc))
    if int(str(rc)) == 0:
        print("Conexion establecida")
        print ""
    else:
        print("Error result code: " + str(rc))
    #client.subscribe(broker_topic)

def on_message(client, userdata, msg):
    #print "Message topic: " + msg.topic + ", payload: " + str(msg.payload)
    #if msg.topic == topic:
    if msg.topic == "temp/test":
        if str(msg.payload) != "":
            print "Message on: " + str(datetime.datetime.now()) + " - topic: " + msg.topic + ", payload: " + str(msg.payload)
            #client.publish(topic, 30, qos=0, retain=False)
            client.publish(topic, "Reset...", qos=0, retain=False)
    pass

def on_publish(mosq, obj, mid):
    #print("Publish mid: " + str(mid))
    pass

def on_subscribed(mosq, obj, mid, granted_qos):
    print("Subscribed mid: " + str(mid) + ", qos: " + str(granted_qos))

def on_log(mosq, obj, mid, string):
    #print("Log: " + str(string))
    pass

client = mqtt.Client()

client.on_connect = on_connect
client.on_message = on_message
client.on_publish = on_publish
client.on_subscribed = on_subscribed
client.on_log = on_log

client.connect(broker, broker_port, timeout_reconnect)
client.subscribe(broker_topic, 0)

print "Cliente conectado " + str(datetime.datetime.now())
print ""

client.loop_start()

while True:
    try:
    client.publish(topic, "Hello world", qos=0, retain=False)
        time.sleep(10)

    except KeyboardInterrupt:
            break

client.disconnect()
print("Desconectado")

I have to add to add broker user and psw? How can I do this?

1 answer

  • answered 2018-01-11 22:16 hardillb

    You need to pass the username and password to the client object before calling connect

    ...
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
    client.on_publish = on_publish
    client.on_subscribed = on_subscribed
    client.on_log = on_log
    
    client.username_pw_set(“username”, “password”)
    client.connect(broker, broker_port, timeout_reconnect)
    ...