tuxmartin

python mqtt subscriber

Jul 30th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. # https://pypi.python.org/pypi/paho-mqtt
  2.  
  3. import paho.mqtt.client as mqtt
  4.  
  5. # The callback for when the client receives a CONNACK response from the server.
  6. def on_connect(client, userdata, flags, rc):
  7.     print("Connected with result code "+str(rc))
  8.  
  9.     # Subscribing in on_connect() means that if we lose the connection and
  10.     # reconnect then subscriptions will be renewed.
  11.     client.subscribe("/hello/world")
  12.  
  13. # The callback for when a PUBLISH message is received from the server.
  14. def on_message(client, userdata, msg):
  15.     print(msg.topic+" "+str(msg.payload))
  16.  
  17. client = mqtt.Client()
  18. client.on_connect = on_connect
  19. client.on_message = on_message
  20.  
  21. client.connect("192.168.0.106", 1883, 60)
  22.  
  23. # Blocking call that processes network traffic, dispatches callbacks and
  24. # handles reconnecting.
  25. # Other loop*() functions are available that give a threaded interface and a
  26. # manual interface.
  27. client.loop_forever()
Advertisement
Add Comment
Please, Sign In to add comment