Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paho-MQTT Error result code: 5

Tags:

python

mqtt

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?

like image 738
Kali Avatar asked Jan 11 '18 20:01

Kali


People also ask

What is MQTT error?

The Magic xpi Server Flow Manager checks for errors during the execution of a step. If there is an error in an MQTT connector step, the Error Handler is invoked and handles the error. The first error in the step causes the termination of the step execution.

What is MQTT PAHO?

The MQTT protocol is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. Designed as an extremely lightweight publish/subscribe messaging transport, it is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.

How does Python connect to MQTT?

To establish a connection to an MQTT broker using the Python client you use the connect method of the client object. The method can be called with 4 parameters. The connect method declaration is shown below with the default parameters. The only parameter you need to provide is the host name.


1 Answers

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)
...
like image 110
hardillb Avatar answered Oct 01 '22 23:10

hardillb