Skip to content
Snippets Groups Projects
Select Git revision
  • 0cc2093341052bad1b0541cc7bea2c3c328e4e97
  • main default protected
  • feat/final
  • feature/Michelle
  • feat/010125
  • Luca
  • feat/knew
  • feat/try
  • feat/katha2
  • feature/katharina
10 results

kclient.py

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    Consumer.py 854 B
    import keyboard
    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, reason_code, properties):
        print(f"Connected with result code {reason_code}")
        client.subscribe("Topic1")
    
    # The callback for when a PUBLISH message is received from the server.
    def on_message(client, userdata, msg):
        print(msg.topic+" "+str(msg.payload))
    
    mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, userdata=None)
    mqttc.on_connect = on_connect
    mqttc.on_message = on_message
    mqttc.username_pw_set(username="standardUser", password="GreatHHZ4Ever!")
    mqttc.connect("localhost", 1883)
    while True:
        mqttc.loop_start()
        if keyboard.read_key == "space":
            mqttc.disconnect()
            mqttc.loop_stop()
            print("goodbye!")
            break
    mqttc.loop_stop()