Package: wzl-mqtt
WZL-MQTTCurrent stable version: 2.6.1DocumentationFor the full API documentation, viewhttps://iot.wzl-mq.rwth-aachen.de/documentation/libs/mqtt/.InstallationRequires at least Python 3.6Install the WZL-MQTT package via pippip install wzl-mqttUsagePublish messagesfromwzlimportmqtt# username and password required to connect to the brokerMQTT_USER=""MQTT_PASSWORD=""# address, port and virtual host of the broker to connect toMQTT_BROKER="127.0.0.1"MQTT_PORT=1883MQTT_VHOST="/"# initialize publisher and connect to the brokerclient=mqtt.MQTTPublisher()client.connect(MQTT_BROKER,MQTT_USER,MQTT_PASSWORD,vhost=MQTT_VHOST,port=MQTT_PORT)# create message and publish the message as UTF-8 encoded stringmessage=json.dumps({"value":[random.uniform(0,5)foriinrange(3)],"timestamp":datetime.datetime.utcnow().isoformat()+"Z","covariance":[[2,0,0],[0,2,0],[0,0,0]],"nonce":str(uuid.uuid4()),"hash":None,"unit":"MTR"})client.publish(MQTT_USER+"/channel-001",message.encode("utf-8"))Subscribe to topics and receive messagesfromwzlimportmqtt# username and password required to connect to the brokerMQTT_USER=""MQTT_PASSWORD=""# address, port and virtual host of the broker to connect toMQTT_BROKER="127.0.0.1"MQTT_PORT=1883MQTT_VHOST="/"# initialize loggerlogger=mqtt.Logger.get('Receiver')# change 'Receiver' to any string you like# define callback which will be executed when a message is receiveddefprint_mqtt_message(topic,message):logger.info("###{}###\r\n{}\r\n".format(topic,message.decode("utf-8")))# initialize subscriber and connect to the brokerclient=mqtt.MQTTSubscriber()client.connect(MQTT_BROKER,MQTT_USER,MQTT_PASSWORD,vhost=MQTT_VHOST,port=MQTT_PORT)# register the callback and subscribe topicclient.set_callback("PRINT",print_mqtt_message)client.subscribe('#')# start waiting loop to prevent program from exitingwhileTrue:try:time.sleep(1)exceptKeyboardInterrupt:breakChangelog2.6.1- 2024-01-11decoupled name of the client used locally from the name used when connecting to the broker to ensure independent clients using this library can use the same names locally2.6.0- 2024-01-10added the classmethod "client_names()" to client to retrieve the name of all instancesthe classmethod "get(username)" of the client raises the "ChildNotFoundError" if there is no instance having the specified username2.5.4- 2023-04-18fixed usage of a randomized client_id, so that the same credentials can be used for multiple clients in parallel2.5.3- 2023-04-15if the port is specified as string it is also correctly processed nowimproved user feedback in case of successful connection to the brokerimproved user feedback if the combination of port, ssl and websocket is invalid2.5.2- 2023-03-29client handles slash in at the end of prefix and the beginning of topic to avoid multiple consecutive slashes correctly now2.5.1increased verbosity in case of errors by including the full stack trace2.5.0changed signature of connect methodspecifying the port is optional now, if not specified the port automatically is derived from the "websocket" and "ssl" flags2.4.2fixed a bug of websocket connection2.4.1made the error message of code 1 more precise2.4.0added vhost parameter to connect function of clientchanged default logging behaviournow by default logging is only written to consoleif logging should also create log-files, the logger must be explicitly initialized2.3.0a prefix can now be defined which is prepended to every published or subscribed topic2.2.0it is possible to connect with more than one client with an identical username to the broker2.1.0removed wzl-utilities dependency2.0.0renamed the MQTReceiver to MQTTSubscriber for the sake of convenienceadded extensive documentation1.3.0added wzl-utilities dependency by sourcing out logging functionality