Not receiving messages with subscribe using Paho MQTT - python

I'm having a problem with subscribing to MQTT broker (using test.mosquitto.org with port 1883) with paho mqtt library. When I use mosquitto_sub client, I'm getting messages normally, but when I subscribe to the same topic in my Python script, callback never gets executed.
import paho.mqtt.client as mqtt
client_get = mqtt.Client(client_id='my_client', protocol=mqtt.MQTTv31)
client_get.connect('test.mosquitto.org', 1883)
client_get.loop_start()
def callback(client, userdata, message):
print(str(message.payload.decode("utf-8")))
client_get.on_message = callback
client_get.subscribe(topic, qos=1)

Try the following:
import paho.mqtt.client as mqtt
client_get = mqtt.Client(client_id='my_client', protocol=mqtt.MQTTv31)
client_get.connect('test.mosquitto.org', 1883)
def callback(client, userdata, message):
print(str(message.payload.decode("utf-8")))
client_get.on_message = callback
client_get.subscribe(topic, qos=1)
client_get.loop_forever()
I've moved the start_loop() to the end and changed it to loop_forever() which will block and keep the script running.

Related

Python/paho MQTT - not receiving messages

I am new to using MQTT and I would like to run two Python programs simultaneously: one to publish messages to a topic and the other to print out the messages as they are published to the topic. However, the callback function to print out the message isn't working for me.
These are the two programs I have:
Publish messages:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected! Result code: " + str(rc))
client = mqtt.Client(client_id="clientID")
client.on_connect = on_connect
client.username_pw_set(username="username", password="password")
client.loop_start()
client.connect("node02.myqtthub.com", 1883)
while True:
client.publish("my/topic", "test")
Subscribe to and print messages:
import paho.mqtt.client as mqtt
def on_message(client, userdata, msg):
print(msg.payload.decode())
def on_connect(client, userdata, flags, rc):
print("Connected! Result code: " + str(rc))
client.subscribe("my/topic")
client = mqtt.Client(client_id="clientID")
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(username="username", password="password")
client.connect("node02.myqtthub.com", 1883)
client.loop_forever()
Could someone help me fix this issue?
Both programs are using client_id="clientID"; the mqtt spec states
If the ClientId represents a Client already connected to the Server then the Server MUST disconnect the existing Client [MQTT-3.1.4-2].
So whichever app connects second will cause the first to be disconnected. To fix this change the client id in one of the apps.
Note: This is a comment on something obviously wrong. When asking a question please state what you see happening (any output, errors, what you have tried etc) because there may be another problem.

How to test the python paho mqtt with mosquitto?

I want to test the mosquitto MQTT Python client ports.
import json
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
"""Called when connected to MQTT broker."""
client.subscribe("hermes/intent/#")
client.subscribe("hermes/nlu/intentNotRecognized")
print("Connected. Waiting for intents.")
def on_disconnect(client, userdata, flags, rc):
"""Called when disconnected from MQTT broker."""
client.reconnect()
def on_message(client, userdata, msg):
"""Called each time a message is received on a subscribed topic."""
nlu_payload = json.loads(msg.payload)
if msg.topic == "hermes/nlu/intentNotRecognized":
sentence = "Unrecognized command!"
print("Recognition failure")
else:
# Intent
print("Got intent:", nlu_payload["intent"]["intentName"])
# Speak the text from the intent
sentence = nlu_payload["input"]
site_id = nlu_payload["siteId"]
client.publish("hermes/tts/say", json.dumps({"text": sentence, "siteId": site_id}))
# Create MQTT client and connect to broker
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.connect("localhost", 1883)
client.loop_forever()
I run it with the command
$ python script.py`
Connected. Waiting for intents.
Does mosquitto send a POST request? or do I have to start a request with mosquitto? How should I create a request so that I get
Got intent: SetTimer
MQTT is not HTTP, POST is a HTTP verb that has no meaning in a MQTT context.
MQTT is a pub/sub protocol, where as HTTP is a request/response protocol.
The code you have posted only subscribes to 2 topics, it doesn't publish anything (until it receives a message). So unless you have another application that will publish a message to 1 of the 2 topics the python code has subscribed to it will just sit there and wait for a message.
You can use the mosquitto command line tools to send a message if needed. e.g.
mosquitto_pub -t hermes/intent/foo -m '{"intent": { "intentName": "SetTimer"}}'

MQTT : Not able to send and receive messages at the same time and in the same file

I have written a code that can do both publishing and subscribing in the same python file. My aim is to be able to send endlessly color temperature to a lamp and receive back its state (ON/OFF or online/offline).
However, when I run the code, I can send messages to the lamp but I don't receive anything (note that publisher and subscriber worked well separately)
import paho.mqtt.client as mqtt
import sys
import time
topic = "testtopic/3"
topic_casa = "testtopic/1"
def on_connect(client, userdata, flags, rc):
print("Connection to the broker. Result : "+str(rc))
client.subscribe(topic_casa, qos=1)
client.publish(topic, light.circadianLight())
def on_message(client, userdata, msg):
Message = str(msg.payload)
print("[MSG RECEBIDA] Topico: "+msg.topic+" / Mensagem: "+Message)
def on_publish(client,userdata,result):
client.publish(topic, light.circadianLight())
time.sleep(10)
def on_disconnect(client, userdata, rc):
if rc != 0:
print("Unexpected disconnection.")
try:
print("[STATUS] Inicializando MQTT...")
#inicializa MQTT:
client = mqtt.Client()
client.username_pw_set(username, password)
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.on_publish = on_publish #with this callback we won't be able to
#receive messages
client.connect(Broker, PortaBroker, KeepAliveBroker)
client.loop_forever()
except KeyboardInterrupt:
print ("\nCtrl+C pressionado, encerrando aplicacao e saindo...")
sys.exit(0)
The problem is two fold, first that you are calling time.sleep(10) in the on_publish() callback.
This is blocking the MQTT client network loop so it will not send/receive any messages for 10 seconds every time you publish a message.
You should not be doing any blocking or long running tasks in any of the MQTT client callbacks.
The other problem is that without the sleep the client will spend all it's time publishing messages (because everytime it finishes publishing a message it trigger the on_publish() callback which kicks off another publish. It will never get round to handing the incoming messages.
If you want to publish messages at a regular interval then you need to start a separate thread to do it, not try to do it in the callbacks.

How do you set puback and pubrel in python mqtt (paho library)?

How do you generate packets for PUBACK and PUBREL and most importantly, how do you know what the packetId of the received message is?
I would like to be able to handle the logic for PUBREL and PUBACKs myself. The code below should give a hint of what I'm trying to do.
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
from multiprocessing import Process, Queue
# 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))
def my_function(id):
client = mqtt.Client(client_id="id", clean_session=False,userdata=None, protocol=MQTTv311)
client.on_connect = on_connect
client.on_message = on_message
client.connect("127.0.0.1", 1883, 60)
msgs = [{'topic':"/test", 'payload':"ClientId: " +id }, 'QoS': 2, 'retain': False),
{'topic':"/test", 'payload':"ClientId: " +id }, 'QoS': 2, 'retain': False),
{'topic':"/test", 'payload':"ClientId: " +id }, 'QoS': 2, 'retain': False)]
publish.multiple(msgs, hostname="127.0.0.1", port=1883,client_id=id, keepalive=60,will=None, auth=None, tls=None, protocol=mqtt.MQTTv311)
# 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()
for i in range(0,10):
p = Process(target=my_function, args=(str(i))
I came across this while working on exploiting a possible MQTT vulnerability. To my knowledge Paho doesn't have any modules to handle more low-level packets, for that you could use Scapy's MQTT module.
Scapy is a module in Python that allows you to handle packets of a variety of protocols, here's how to send an MQTT CONNECT using scapy:
from scapy.contrib.mqtt import MQTTConnect
from scapy.all import send, IP, TCP
mqtt_broker_ip = '192.168.0.1'
mqtt_broker_port = 1883
ip_pkt = IP(dst=mqtt_broker_ip)
tcp_pkt = TCP(dport=mqtt_broker_port)
mqtt_pkt = MQTTConnect(clientId='my_client_id')
# use slashes (/) to stack protocols
final_pkt = ip_pkt/tcp_pkt/mqtt_pkt
send(mqtt_pkt)

Unable to receive more than 20 MQTT messages using Mosquitto/Paho for Python

I'm using the Mosquitto (now Paho) python MQTT client to connect to a HiveMQ broker. The code is very basic, as taken from their documentation here - https://pypi.python.org/pypi/paho-mqtt
#SUBSCRIBER
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
client.subscribe("GB/node0/", 2)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print "Topic: ", msg.topic+'\nMessage: '+str(msg.payload)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("192.168.15.4", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
client.loop_forever()
As you notice in client.subscribe(), the QoS is 2. This is the only modification to the official documentation's code.
The publishing client's code is-
#PUBLISHER
import paho.mqtt.client as mqtt
mqttc = mqtt.Client("python_pub")
mqttc.connect("192.168.15.4", 1883, 60)
mqttc.publish("GB/node0/", "Hello, World baby!",2)
Here also, the QoS is 2.
This modification of QoS results in only 20 messages being received by the subscriber. Upon further probing, I realized the problem is probably due to max_inflight_messages_set(), which is an option function which sets the maximum number of messages with QoS>0 that can be part way through their network flow at once. The default is 20.
However, changing it to any other value does not work. Also, why does the client think these messages are still inflight when they've been received? How do I resolve the problem? How do I ensure that the client understands these messages are not "inflight" and have been delivered?
Try calling mqttc.loop(2,10) after the mqttc.publish() in the publisher so the publisher can handle the QOS2 acknowledgement from the broker that it has received the publish.
The 2 second timeout and the 10 packets is probably more than is needed but it should work
#hardillb is right, you need some form of loop*() call. To be more certain, do this:
import paho.mqtt.client as mqtt
def on_publish(client, userdata, mid):
client.disconnect()
mqttc = mqtt.Client() # There's normally no need to set a client id.
mqttc.on_publish = on_publish
mqttc.connect("192.168.15.4", 1883, 60)
mqttc.publish("GB/node0/", "Hello, World baby!",2)
mqttc.loop_forever()
Or:
import paho.mqtt.publish as paho
paho.single("GB/node0/", "Hello, World baby!", qos=2, hostname="192.168.15.4")

Categories

Resources