Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import paho.mqtt.client as mqtt
- import time
- def on_connect(client, userdata, flags, reason_code, properties):
- print(f"Connected with result code {reason_code}")
- def on_message(client, userdata, msg):
- print(msg.topic + " " + str(msg.payload))
- def publish_message(client, topic, message):
- result = client.publish(topic, message)
- if result.rc == mqtt.MQTT_ERR_SUCCESS:
- print(f"Message {message} published to topic {topic}")
- def subscribe_to_topic(client, topic):
- client.subscribe(topic)
- print(f"Subscribed to topic '{topic}'")
- def ask_before_publish():
- time.sleep(1)
- message = input("Command untuk kontrol: ")
- publish_message(mqttc, "LBCMH/1", message)
- ask_before_publish()
- mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
- mqttc.on_connect = on_connect
- mqttc.on_message = on_message
- mqttc.username_pw_set(username="public", password="public")
- mqttc.connect("public.cloud.shiftr.io", 1883, 60)
- mqttc.loop_start()
- ask_before_publish()
- try:
- while True:
- time.sleep(1)
- except KeyboardInterrupt:
- print("Exit...")
- finally:
- mqttc.loop_stop()
- mqttc.disconnect()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement