Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Unable to push MQTT message for a Topic (I am using latest msg_info.wait_for_publish() and it never get over)
Unable to push MQTT message for a Topic [message #1768131] Thu, 13 July 2017 21:40
Shailendra  Singh  is currently offline Shailendra Singh Friend
Messages: 1
Registered: July 2017
Junior Member
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

1. The MID of the published message .
2 . Also when i run subscriber code on my local machine, i don't get any message.
3. 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)


In my last attempt, i used the

msg_info = client.publish(...)
 
if msg_info.is_published() == False:
    print("Message is not yet published.")
 
# This call will block until the message is published.
msg_info.wait_for_publish()


and this block everything as i can see clearly that i am not able to send the message. I also tried threading i.e. to pass this my_mqtt_publish function to some other thread to make it non-blocking but even it didn't helped.




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.
Previous Topic:Paho client using asynchronous API
Next Topic:Connect to AWS S3 Using Athena jdbc driver
Goto Forum:
  


Current Time: Thu Apr 25 02:06:24 GMT 2024

Powered by FUDForum. Page generated in 0.03273 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top