Advertisement
Astranome

MQTT_Client_Python

Jul 5th, 2020
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. import paho.mqtt.client as mqtt #import the client1
  2. import time
  3.  
  4. def on_connect(client, userdata, rc):  
  5.     print("Connected with result code: %s" % rc)
  6.     client.subscribe("hello/world")
  7.  
  8. def on_message(client, userdata, message):
  9.     print("message received " ,str(message.payload.decode("utf-8")))
  10.     print("message topic=",message.topic)
  11.     print("message qos=",message.qos)
  12.     print("message retain flag=",message.retain)
  13.  
  14. def main():  
  15.     client = mqtt.Client("P1") #create new instance
  16. #    client.on_connect = on_connect
  17.     client.on_message=on_message #attach function to callback
  18.  
  19.     client.connect("192.168.1.39")
  20.     client.loop_start() #start the loop
  21.     client.subscribe("hello/world") # subscribe on topic hello/world
  22.     time.sleep(99) # wait
  23.     client.loop_stop() #stop the loop
  24. if __name__ == "__main__":  
  25.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement