Can't connect to mqtt broker - python

I installed MQTT broker Mosquitto on my pi and are having some problems getting it to work with boxes in my network. Locally, if I putty in to the RPi running the Mosquitto MQTT broker everything is OK. I can use the client commands (mosquitto_sub, mosquitto_pub) to subscribe and publish to topics, no problem. BUT, if I try to connect from another box, Win2k12 server with a python script it states it cant connect.
I've tried turning the firewall off in my router
I've tried turning the firewall off on my Win2k12 server
I've added TCP 1883 to allowed ports outbound from my Win2k12 server
The Python script:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
client.publish("test_mqtt", "test")
client.subscribe("test")
def on_disconnect(client, userdata, rc):
print("Disconnect, reason: " + str(rc))
print("Disconnect, reason: " + str(client))
client = mqtt.Client("testclient")
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.connect("192.168.1.20", 1883, 60)
client.loop_forever()
The output here is
Disconnect, reason: <paho.mqtt.client.Client object at 0x01F41EF0>
Disconnect, reason: 1
I've tried to have a look at the documentation but it only mentioned the flags, not defining what they are.
The raspberry pi that is running Mosquitto is also running Node-red. It has no problem connecting to the MQTT broker (both of them are running on the same rpi)
Has enyone set up MQTT on Raspberry Pi and got it to work with other devices? I want it to work with a NodeMCU thingy, but when I had problems I started working on a python script to further debug the problem.

You can force the paho client to use the 3.1 level of the protocol by adding an option to the mqtt.Client constuctor:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
client.publish("test_mqtt", "test")
client.subscribe("test")
def on_disconnect(client, userdata, rc):
print("Disconnect, reason: " + str(rc))
print("Disconnect, reason: " + str(client))
client = mqtt.Client("testclient", protocol=mqtt.MQTTv31)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.connect("192.168.1.20", 1883, 60)
client.loop_forever()

First you have to make sure that you can connect to the Raspberry Pi. You can try using libraries other than Paho or using one MQTT client:
http://www.hivemq.com/blog/seven-best-mqtt-client-tools
The other thing you can try is setting both client and broker to use port 80 to see if they can communicate through that port.

Related

Error 128 (0x80) while trying to subscribe to topic with paho-mqtt

I'm using hbmqtt 0.9.6 as a broker and paho-mqtt 1.5.1 as a client. Both of them support MQTT v3.1.1.
Broker configuration:
listeners:
default:
type: tcp
halp:
max-connections: 33
bind: 127.0.0.3:1883
topic-check:
enabled: false
auth:
allow-anonymous: true
Client code:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
client.subscribe('abc')
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
def on_subscribe(mosq, obj, mid, qos):
print("Subscribed: " + str(mid))
print("Granted QoS: " + str(qos[0]))
client = mqtt.Client(client_id="subscriber", transport="tcp")
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
client.connect("127.0.0.3", 1883)
client.loop_forever()
The client establishes a connection to the broker correctly and can send messages to the broker without any problems. But when I try to subscribe to a topic, I receive Granted QoS: 128. What can cause that kind of problem?
What I've tried:
Change topic name
Change IP address and port
Change transport from TCP to ws
Change OS (tried on Armbian and Ubuntu)
Use authentication with login:password
Fraschbi was right. The problem was with the specified version of hbmqtt: it ignored permission configurations. A workaround is to specify what topics the client can subscribe to.

MQTT broker for testing

Trying to create a Python script that subscribes to an MQTT broker and works with the data. However, the MQTT broker is so far not receiving any data, which makes it difficult to test.
I found the following script to subscribe to a topic and print out the payloads, but it doesn't seem like I can connect to the test broker:
import paho.mqtt.client as mqtt
broker_url = "iot.eclipse.org"
broker_port = 1883
def on_connect(client, userdata, flags, rc):
print("Connected With Result Code "+rc)
def on_message(client, userdata, message):
print("Message Recieved: "+message.payload.decode())
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker_url, broker_port)
client.subscribe("TestingTopic", qos=1)
client.publish(topic="TestingTopic", payload="TestingPayload", qos=1, retain=False)
client.loop_forever()
So I would need some MQTT broker and topic that I can subscribe to in order to test my script. Any recommendations how I can find one?
There are two options:
Create local broker, so install on your PC - example https://mosquitto.org/download/
Use one online free broker like I am using now: https://www.cloudmqtt.com/

Python paho-mqtt Connect to MQTT broker

I'm using this python script to implement a Paho(MQTT) subscriber but i am unable get any responce messages.i am able to subscribe mqtt brokerin command prompt by using mosquitto_sub -t "" -d -h -p 8883 --psk foo --psk-identity bar --insecure --tls-version tlsv1
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("*********")
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("*********", 8883, 60)
client.loop_forever()
When I run above python script then it does not respond any error or message but keep going with loop , I also run it line by line and when I run client.connect("*********", 8883, 60) then it shows only 0 . please note here without psk and psk-identity we cannot connect to broker.
Thanks
Please check with your topic carefully, sometimes missing of / or # cause this problem.
or
Try This
def on_message(client, userdata, msg):
print("Message Recieved from broker: " + msg.payload.decode())

Getting [errno 111] using mosquitto sending to web interface

I have setup a mosquitto client that sends data to a dygraph script which plots the data real time. My issue is that sometimes, when i close the dygraph html window, the terminal from which my mqtt script sends the data comes with the line "errno 104 connection reset by peer". After this, if I close and reopen my mqtt script, it won't run with the message "socket.error: [Errno 111] Connection refused"
I have enabled websockets in order to send data to my html file, so there must be a conflict there.
The mqtt code is:
import paho.mqtt.client as mqtt
import time
import random
def on_connect(client, userdata, flags, rc): #function to call on connect
print("Connected with result code "+str(rc))
client.subscribe("topic1/")
def on_message(client, userdata, msg): #function to call on message
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.reinitialise() #initiate mqtt client
#client.on_connect = on_connect
#client.on_message = on_message
client.connect("localhost", 1883, 60) #connect to broker
client.loop_start()
while True:
client.publish("topic1/", random.uniform(-20, 20))
time.sleep(0.5)
and the websocket from which my html file reads the data is:
var MQTTport = 9001;
9001 is what I have said to be the websocket in the mqtt config file.
So why does it crash when i press I close the tab in the html window but runs normally if I keep it open?
Thank you in advance.

Send python input through mosquitto

Here is the code I'm running for my client. It works pretty well, but it doesn't allow python inputs to be made. I considered making another .py-file for typing and sending messages, but I'm not sure how to import the established connection.
Is it somehow possible to enable a python input chat using mqtt?
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("hello/world")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+"| "+ userdate + " said: "+str(msg.payload))
id = raw_input('username: ')
client = mqtt.Client(id)
client.on_connect = on_connect
client.on_message = on_message
client.connect_async("192.168.0.24", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()

Categories