Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. import paho.mqtt.client as mqtt
  2.  
  3.  
  4. # The callback for when the client receives a CONNACK response from the server.
  5. def on_connect(client, userdata, flags, rc):
  6. print("Connected with result code {}".format(int(rc)))
  7.  
  8. # Subscribing in on_connect() means that if we lose the connection and
  9. # reconnect then subscriptions will be renewed.
  10. client.subscribe("$SYS/#")
  11.  
  12. # The callback for when a PUBLISH message is received from the server.
  13. def on_message(client, userdata, msg):
  14. print("[{}]: {}".format(msg.topic, str(msg.payload)))
  15.  
  16.  
  17. print("System ready")
  18. client = mqtt.Client()
  19. # client.on_connect = on_connect
  20. client.on_message = on_message
  21.  
  22. client.connect("192.168.0.154", 1883, 60)
  23. print("connected")
  24.  
  25.  
  26. client.subscribe('PI', qos=0)
  27. name = input("Input username: ")
  28.  
  29. try:
  30.  
  31. while True:
  32. client.loop_start()
  33. userinput = input("\nType: ")
  34. if userinput == "0":
  35. break
  36. else:
  37. client.publish('PI', "({}): {}".format(name, userinput))
  38. finally:
  39. print("Gracefully ended")
  40. client.disconnect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement