Python Forum
Mqtt - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Mqtt (/thread-35737.html)



Mqtt - Shiri - Dec-07-2021

Hello, this is my code where i am trying to subscribe to mqtt topic and receive the messages. It works fine and I am able to receive the message. My question here is if i am able to receive the message i need to return something like PASS and if not receiving the message FAIL or ERROR . Any suggestions on how this can be achieved

def on_connect(client, userdata, flags, rc):  
    if rc == 0:
        print("Connected")  
    else:
        print("Failed to connect,return code %d\n",rc) 
def on_subscribe(client,userdata,msg):
    print(msg.topic+""+str(msg.qos)+""+str(msg.payload))
def on_message(client, userdata, msg):  
    print("Message Received " + msg.topic + " " + str(msg.payload))  

client = mqtt.Client()  
client.on_connect = on_connect  
client.on_message = on_message  
client.connect('127.0.0.1', 1883)
client.subscribe("test/temp")
client.loop_forever()