Advertisement
candale

TMP_Mqtt

Feb 18th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import time
  2.  
  3. import paho.mqtt.client as mqtt
  4.  
  5.  
  6. # The callback for when the client receives a CONNACK response from the server.
  7. def on_connect(client, userdata, flags, rc):
  8. print("Connected with result code "+str(rc))
  9.  
  10.  
  11. client = mqtt.Client()
  12. client.on_connect = on_connect
  13.  
  14. client.connect("localhost", 1883, 60)
  15. client.loop_start()
  16.  
  17. counter = 0
  18. while True:
  19. client.publish("lol/data", counter)
  20. counter += 1
  21. time.sleep(1)
  22. ==============================================================================
  23. import paho.mqtt.client as mqtt
  24.  
  25.  
  26. def on_connect(client, userdata, flags, rc):
  27. print("Connected with result code "+str(rc))
  28. client.subscribe("lol/data")
  29. client.subscribe("$SYS/broker/subscriptions/#")
  30.  
  31.  
  32. def on_message(client, userdata, msg):
  33. print(msg.topic+" "+str(msg.payload))
  34.  
  35.  
  36. client = mqtt.Client()
  37. client.on_connect = on_connect
  38. client.on_message = on_message
  39.  
  40. client.connect("localhost", 1883, 60)
  41. client.loop_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement