Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] Re - Unable to push MQTT message

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)
    

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


Back to the top