Reputation: 197
I've installed Mosquitto and used in the terminal without problem, also installed paho for c++, both mosquitto and paho work well. Then installed paho for python via pip. Now i'm trying to test it with this code but the on_message and on_connect functions never get called. I'm doing
mosquitto_pub -h localhost -t "test" -m "This is a message"
while the python script runs but it doesn't work. It never gets to the line that prints "on_message!"
import paho.mqtt.client as mqtt
def on_connect(self, mqtt_client, obj, flags, rc):
mqtt_client.subscribe("test")
def on_message(self, mqtt_client, obj, msg):
print "on_message()"
print "Initializing subscriber"
mqtt_client = mqtt.Client()
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.connect("localhost",1883)
print "Listening"
while True:
mqtt_client.loop()
What am i doing wrong?
Upvotes: 5
Views: 11473
Reputation: 1
I had a similar problem with the on_message
function not working in Python Paho
. After some troubleshooting, I discovered that I was subscribed to the wrong topic. Since I wasn’t subscribed to the correct topic, my on_message
function wasn’t being triggered.
The on_message
function works when you receive a message on a subscribed topic. Therefore, to receive messages, you need to ensure that you are subscribed to the correct topic.
This is a simple mistake, but it can happen. I hope sharing my experience helps others who might face the same issue.
Upvotes: 0
Reputation: 149
This is an old thread but I just hit a similar problem. I had:
def on_message (client, userdata,message):
print("received message", str(message.payload.decode("utf-8")
client.loop_start()
client.subscribe(topic)
client.on_message=on_message
time.sleep(30)
client.loop_stop()
If I changed the time from 30 to 300 then on_message was never executed. Go figure?
Upvotes: 0
Reputation: 4154
The on_message
callback gets called only if You are subscribed to a topic.
To do that, You need to call the subscribe method. For example:
print "Initializing subscriber"
mqtt_client = mqtt.Client()
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.connect("localhost", 1883) # "localhost" or IP or
mqtt_client.subscribe("test") # topic name, same as the "-t" argument in mosquitto_pub
print "Listening"
mqtt_client.loop_forever()
As for the on_connect
callback, what caused the issue for me was that I called the connack_string
method as it was Client
's method, but it is not. Looking at the source code, we see that it is defined directly in the module, not inside a class:
def connack_string(connack_code):
So, I replaced this:
def connect_callback(client, userdata, flags, rc):
print("Connection attempt returned: " + client.connack_string(rc)) # client !!
with this:
def connect_callback(client, userdata, flags, rc):
print("Connection attempt returned: " + mqtt.connack_string(rc)) # mqtt !!
What You can also try is to omit the -h localhost
part from the mosquitto_pub
command, like this:
mosquitto_pub -t "test" -m "This is a message"
Also, watch for the number of parameters that the on_connect
callback receives (4). This is advice to everybody else reading this post. :)
Upvotes: 4
Reputation: 59816
The following is working fine for me.
I have removed the self
from the callbacks and swapped the loop for mqtt_client.loop_forever()
import paho.mqtt.client as mqtt
def on_connect(mqtt_client, obj, flags, rc):
mqtt_client.subscribe("test")
def on_message(mqtt_client, obj, msg):
print "on_message()"
print "Initializing subscriber"
mqtt_client = mqtt.Client()
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
mqtt_client.connect("localhost",1883)
print "Listening"
mqtt_client.loop_forever()
Upvotes: 1