Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mqtt
#1
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  add mqtt feature to existing code Positron79 0 1,055 Jan-31-2023, 05:56 PM
Last Post: Positron79
  Paho-mqtt fully supports MQTT 5.0 protocol? nitinkothari17 0 2,011 Jan-12-2021, 11:14 AM
Last Post: nitinkothari17
  How to clean session mqtt SayHiii 0 2,584 Dec-09-2019, 07:56 AM
Last Post: SayHiii
  Arduino Uno connected to PI 0 W - Sensor Data MQTT runboy 4 4,154 Nov-07-2018, 03:55 AM
Last Post: runboy
  action on MQTT while long loop is running runboy 4 7,532 Oct-05-2018, 11:57 PM
Last Post: runboy
  Run on Flask blueprint/view like mqtt subscription service? nardev 0 3,515 Jan-25-2017, 07:02 PM
Last Post: nardev

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020