Skip to content
Snippets Groups Projects
Select Git revision
  • 19bf2281ca70b8431acd0baef00d2aef49f996ee
  • main default protected
  • develop-visual-gen
  • develop
  • feature-usermngmt
  • refactoring
6 results

soil-editor

WZL-MQTT

pipeline status

Current stable version: 2.4.0

Installation

Requires at least Python 3.6

  1. Install the WZL-MQTT package via pip
pip install --extra-index-url https://package-read:gkYP4xrm2PxicUbW1wra@git-ce.rwth-aachen.de/api/v4/projects/1708/packages/pypi/simple wzl-mqtt

Usage

Publish messages

# username and password required to connect to the broker
MQTT_USER = ""
MQTT_PASSWORD = ""

# address, port and virtual host of the broker to connect to 
MQTT_BROKER = "127.0.0.1"
MQTT_PORT = 1883
MQTT_VHOST = "/"

# initialize publisher and connect to the broker
client = mqtt.MQTTPublisher()
client.connect(MQTT_BROKER, MQTT_PORT, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST)

# create message and publish the message as UTF-8 encoded string
message = json.dumps({"value": [random.uniform(0, 5) for i in range(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 messages

# username and password required to connect to the broker
MQTT_USER = ""
MQTT_PASSWORD = ""

# address, port and virtual host of the broker to connect to 
MQTT_BROKER = "127.0.0.1"
MQTT_PORT = 1883
MQTT_VHOST = "/"

# initialize logger
logger = mqtt.Logger.get('Receiver') # change 'Receiver' to any string you like

# define callback which will be executed when a message is received
def print_mqtt_message(topic, message):
        logger.info("### {} ###\r\n{}\r\n".format(topic, message.decode("utf-8")))

# initialize subscriber and connect to the broker   
client = mqtt.MQTTSubscriber()
client.connect(MQTT_BROKER, MQTT_PORT, MQTT_USER, MQTT_PASSWORD, vhost=MQTT_VHOST)

# register the callback and subscribe topic
client.set_callback("PRINT", print_mqtt_message)
client.subscribe('#')

# start waiting loop to prevent program from exiting
while True:
    try:
        time.sleep(1)
    except KeyboardInterrupt:
        break

Slightly more detailled examples can be found in the sample directory.
If there are any questions contact Matthias Bodenbenner.
To obtain credentials for MQTT-Broker of WZL-MQ-MS contact Mark Sanders

Documentation

For more detailed explanation and full API documentation, view https://iot.wzl-mq.rwth-aachen.de/documentation/libs/mqtt/

Changelog

2.4.1

  • made the error message of code 1 more precise

2.4.0

  • added vhost parameter to connect function of client
  • changed default logging behaviour
    • now by default logging is only written to console
    • if logging should also create log-files, the logger must be explicitly initialized

2.3.0

  • a prefix can now be defined which is prepended to every published or subscribed topic

2.2.0

  • it is possible to connect with more than one client with an identical username to the broker

2.1.0

  • removed wzl-utilities dependency

2.0.0

  • renamed the MQTReceiver to MQTTSubscriber for the sake of convenience
  • added extensive documentation

1.3.0

  • added wzl-utilities dependency by sourcing out logging functionality