MQTT broker for testing - python

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/

Related

Mqtt Subscriber, loop_forever() does not work

I am having an issue with the code below.
The code works perfectly at the beginning. First it says Connected to MQTT Broker! and receives data from it. But after a long time (like 6 hours, or 10 hours etc.) it says again Connected to MQTT Broker! and after that id does not receive any other data.
I am trying to make this program work forever, but i don't know what i have done wrong.
Any ideas?
# python3.6
import random
import mysql.connector
from paho.mqtt import client as mqtt_client
import json
# Code for MQTT Connection
broker = 'YOUR_BROKER'
port = 1883
topic = "YOUR_TOPIC"
# generate client ID with pub prefix randomly
client_id = f'python-mqtt-{random.randint(0, 100)}'
username = "THE_USERNAME"
password = "THE_PASSWORD"
# Function to connect on mqtt
def connect_mqtt() -> mqtt_client:
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client
# function to subscribe from mqtt
def subscribeFunc(client: mqtt_client):
def on_messageFunc(client, userdata, msg):
print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
client.subscribe(topic)
client.on_message = on_messageFunc
def run():
client = connect_mqtt()
subscribeFunc(client)
client.loop_forever()
if __name__ == '__main__':
run()
I tried to find the problem but it seems that nothing changed significantly.
I am expecting this program to receive data without stopping.
Network connections may not be 100% reliable (and servers etc are restarted from time to time) so expecting the connection to remain up forever is unrealistic. The issue with your code is that it connects then subscribes; if the connection is dropped it does not resubscribe. As the connection is clean_session=true and subscription qos=0 (the defaults) the broker will forget about any active subscriptions when the connection drops (so the client will reconnect but not receive any more messages).
The Simple solution is to use the approach shown in the docs and subscribe in the on_connect callback (that way the subscription will be renewed after reconnection):
def on_connect(client, userdata, flags, rc):
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("$SYS/#")
client = mqtt.Client()
client.on_connect = on_connect
client.connect("mqtt.eclipseprojects.io", 1883, 60)
You may also want to consider the advice in this answer (as per the comment from #mnikley) because that way the broker will queue up messages for you while the connection is down (otherwise these will be lost).

How to use both subscribe and publish at the same time in mqtt python?

I am working with a device that publishes to the topic test/123, where 123 is the name of the device. I need to subscribe to that topic (and processes received messages); in addition I also need to send a word to the same topic (test/123). The device only looks at this topic.
How can I distinguish between incoming and outgoing by content? More precisely, how to send correctly. In the on_message method you need to do this or you need to create another method, but then how to receive incoming messages there. From the incoming messages I need to get the name of the device and then work with it.
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
# 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("/test/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
imei = msg.topic.split('test/')[1]
data = msg.payload.decode()
print(imei)
print(data)
publish(imei)
def publish(imei):
client = mqtt.Client()
user = 'test'
passw = '1111'
client.username_pw_set(user,passw)
client.connect("localhost",1883)
topic = '/test/'+ imei
client.publish(topic,'hello')
print('SEND')
client.disconnect()
client = mqtt.Client()
user = 'test'
passw = '1111'
client.username_pw_set(user,passw)
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
client.loop_forever()
MQTT does not differentiate between clients in any way, that means if a client subscribes to a given topic it will receive ALL messages on that topic, including the ones it publishes it's self. So with your current design you will always get the message you publish in response to the first message back and this will trigger re-sending that message.
MQTT messages do NOT contain any information about who published the message unless you choose to add it to the payload, so you have no way to identify the incoming message as being the one you just published and this will cause a message loop storm.
The CORRECT solution is to not use the same topic for the 2 messages.
MQTT v5 has a flag that can be passed as part of establishing the connection
which prevents messages being returned to the client that published them. At this time it does not appear that the Paho Python library has a way to set this flag.
If you are using MQTT v3.1.1 and the mosquitto or RSMB MQTT broker then there is an undocumented option (this is not part of the MQTT spec) that can be set which will also prevent messages being returned. The following code will ONLY work with the 2 brokers I have mentioned.
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("test/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
imei = msg.topic.split('test/')[1]
data = msg.payload.decode()
print(imei)
print(data)
publish(client, imei)
def publish(client,imei):
topic = 'test/'+ imei
client.publish(topic,'hello')
print('SEND')
client = mqtt.Client()
user = 'test'
passw = '1111'
client.username_pw_set(user,passw)
client.on_connect = on_connect
client.on_message = on_message
client.enable_bridge_mode()
client.connect("localhost", 1883, 60)
client.loop_forever()
p.s. do not start topics with a leading / while legal according to the spec, it will break things like shared subscriptions and adds an extra null to the start of the topic tree.

How to configure the paho mqtt subscribe to get the data with high speed like 1000 message per second

I am using python paho mqtt client to subscribe the MQTT broker to get the data and I am publishing the data through ESP32 using pubsubclient.h.
Data frequency is 1000 messages per sec. for testing I published 100000 messages and I used python paho mqtt to subscribe and collect data. I tested several times but I am getting 22000-30000 messages out of 100000 message.
This the code I am using to subscribe the broker:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
client.subscribe("pub_test",2)
else:
print("Connection failed")
def on_message(client, userdata, message):
print (message.payload)
def updateMqtt():
client = mqtt.Client(client_id="Sathish", clean_session=False, userdata=None, transport="tcp")
#client = mqtt.Client()
client.on_connect= on_connect
print("after on connect")#attach function to callback
client.on_message= on_message
print("after on message")#attach function to callback
client.connect("192.168.3.101", port=1883)
client.loop_forever()
# client.loop_start()
updateMqtt()
You won't be loosing the messages between the broker and this code since you are subscribing at QOS2, it's much more likely the loss is between the ESP32 and the broker since the PubSubClient only supports QOS0

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())

Can't connect to mqtt broker

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.

Categories