Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import paho.mqtt.client as mqtt
- # The callback for when the client receives a CONNACK response from the server.
- def on_connect(client, userdata, flags, rc):
- print("Connected with result code {}".format(int(rc)))
- # Subscribing in on_connect() means that if we lose the connection and
- # reconnect then subscriptions will be renewed.
- client.subscribe("$SYS/#")
- # The callback for when a PUBLISH message is received from the server.
- def on_message(client, userdata, msg):
- print("[{}]: {}".format(msg.topic, str(msg.payload)))
- print("System ready")
- client = mqtt.Client()
- # client.on_connect = on_connect
- client.on_message = on_message
- client.connect("192.168.0.154", 1883, 60)
- print("connected")
- client.subscribe('PI', qos=0)
- name = input("Input username: ")
- try:
- while True:
- client.loop_start()
- userinput = input("\nType: ")
- if userinput == "0":
- break
- else:
- client.publish('PI', "({}): {}".format(name, userinput))
- finally:
- print("Gracefully ended")
- client.disconnect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement