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.