Skip to content
Snippets Groups Projects
Select Git revision
  • 8f8eb3972de23d6a65e8afc965a1dca96f7af625
  • master default protected
  • b24_tutorial
  • 2.4.1
  • 1.3.0
  • 1.2.0
6 results

receive.py

Blame
  • receive.py 1.23 KiB
    import time
    import logging
    import asyncio
    import datetime
    
    from wzl import mqtt
    
    ### Ask for settings and individual credentials ###
    
    MQTT_USER = ""
    MQTT_PASSWORD = ""
    
    # MQTT_BROKER = "127.0.0.1"
    # MQTT_PORT = 1883
    # MQTT_VHOST = "/"
    
    ## to connect to the central MQTT-Broker of MQ-MS use the following settings:
    MQTT_BROKER = "wzl-mbroker01.wzl.rwth-aachen.de"
    MQTT_PORT = 1883
    MQTT_VHOST = "metrology"
    
    ### Ask for settings and individual credentials ###
    
    topic = "#" # set topic to subscribe according to MQTT syntax!
    qos = 0 # set QoS according to MQTT specifications!
    
    def print_mqtt_message(topic, message):
        print("{}\r\n### {} ###\r\n{}\r\n".format(datetime.datetime.utcnow().isoformat() + "Z", topic, message.decode("utf-8")))
    
    if __name__=="__main__":
        console_log = logging.StreamHandler()
        console_log.setFormatter(mqtt.formatter)
    
        mqtt.logger.addHandler(console_log)
        mqtt.logger.setLevel(logging.INFO)
    
        client = mqtt.MQTTSubscriber(MQTT_USER)
        client.connect(MQTT_BROKER, MQTT_PORT, MQTT_VHOST + ":"+  MQTT_USER, MQTT_PASSWORD)
    
        client.set_callback("PRINT", print_mqtt_message)
        client.subscribe(topic, qos)
    
        while True:
            try:
                time.sleep(1)
            except KeyboardInterrupt:
                break