Hi,
my first tough on this problem is that you don't "wait" enough to
let message be published.
You do connect, (submit as message to) publish, wait 1 second,
and - I assuming - terminate the program.
One second may be enough to complete the message publishing
depending on your connection speed (which may explain why the same
code work on your local machine but not on DO). But you can't be
sure.
The correct solution is not to wait for an arbitrary number of
seconds, but to wait for on_publish to be called for all your
messages, and then disconnect from here. The easiest way to
achieve this (connect, publish, wait, disconnect) is to use the
publish helper.
You are not the first one to fall in this trap, I'll try to
improve documentation to avoid this for future one:
https://github.com/eclipse/paho.mqtt.python/issues/210
Regards,
PierreF
Le 13/07/2017 à 22:16, Shailendra Singh
a écrit :
I am facing a
very strange issue. I am working on a DIY project of home
automation using MQTT + Python. I have hosted my code on
Digital ocean and using UFW i have allowed traffic on 1883
port (MQTT broker port). I am running a copy of the code
on my local machine in which i am calling a function x from
a python file which take topic and payload as
a argument and push the topic + payload to
a MQTT broker.
This happen successfully and even i get the mid of
the published message. Below is the code which i am using
import paho.mqtt.client as paho
import time
def on_publish(client, userdata, mid):
print("mid: "+str(mid))
client = paho.Client()
client._on_publish_ = on_publish
client.connect("broker.mqttdashboard.com", 1883)
client.loop_start()
def my_mqtt_publish(topic, payload):
try:
(rc, mid) = client.publish(topic, payload, qos=2)
time.sleep(1)
print "going to publish topic and i am in the other module"
print "going to publish topic : {0} and payload {1}".format(topic,payload)
except Exception as e:
print e.args, e.message
Also when i run
a subscriber code on the digital ocean, i am able to get
the message.
Now when i run
the same publishing code on digital ocean, i don't get
- The MID of the
published message .
- Also when i
run subscriber code on my local machine, i don't get any
message.
-
Also when i
run a test code on the digital ocean and the
corresponding subscriber code on the local machine, i
get the message. Below is my test code
import paho.mqtt.client as paho
import time
def on_publish(client, userdata, mid):
print("mid: "+str(mid))
client = paho.Client()
client._on_publish_ = on_publish
client.connect("broker.mqttdashboard.com", 1883)
client.loop_start()
def my_mqtt_publish():
try:
client.publish( "message/msg", "A", qos=1)
print "going to publish topic : {0} and payload {1}".format(" message/msg","A")
except Exception as e:
print e.args, e.message
if __name__ == "__main__":
while 1:
my_mqtt_publish()
time.sleep(5)
I am really
clueless what am i missing here in the code and really
feel akward to ask this kind of question where everything
is infront of my eyes and i am not able to find the
problem.
With regards.
Shailendra Singh
+91-9910908382
Here's to the crazy ones, the misfits, the rebels, the
troublemakers,the
round pegs in the square holes... the ones who see
things differently --
they're not fond of rules... You can quote them,
disagree with them, glorify
or vilify them, but the only thing you can't do is
ignore them because they
change things... they push the human race forward, and
while some may see
them as the crazy ones, we see genius, because the
ones who are crazy enough
to think that they can change the world, are the ones
who do.
-- Steve Jobs, US computer engineer &
industrialist (1955 - )*
_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/paho-dev
|