The user and password are working fine when using the mosquitto pub/sub in the command line but when I try to run the .py file I get the "Received CONNACK (0, 5)" "bad connection Returned".What am I missing?
#! c:\python34\python.exe
#!/usr/bin/env python
import paho.mqtt.client as mqtt
import time
def on_log(client, userdata, level, buf):
print("log: "+buf)
client.tls_set()
def on_connect(client, userdata, flags, rc):
if rc==0:
print("connected OK")
else:
print("Bad connection Returned code=",rc)
def on_disconnect(client, userdata, flags, rc=0):
print("DisConnected result code "+str(rc))
def on_message(client,userdata,msg):
topic=msg.topic
m_decode=str(msg.payload.decode("utf-8","ignore"))
print("message received",m_decode)
broker="test.mosquitto.org"
client = mqtt.Client("python1")
user="teste"
password="teste"
client.username_pw_set(user,password=password)
client.on_connect=on_connect
client.on_disconnect=on_disconnect
client.on_log=on_log
client.on_message=on_message
print("Connecting to broker ",broker)
client.connect(broker,1884)
client.loop_start()
client.subscribe("house/sensor1")
client.publish("house/sensor1","my first message")
time.sleep(4)
client.loop_stop()
client.disconnect()
As mentioned in the comments
You must use the username/password pairs mentioned on http://test.mosquitto.org when using the test.mosquitto.org broker.
The authenticated listeners require a username / password:
rw / readwrite : read/write access to the # topic hierarchy ro / readonly : read only access to the # topic hierarchy wo / writeonly : write only access to the # topic hierarch