Skip to main content

Small library containing an MQTT publisher and receiver.

Project description

WZL-MQTT

Current stable version: 2.5.2

Documentation

For the full API documentation, view https://iot.wzl-mq.rwth-aachen.de/documentation/libs/mqtt/.

Installation

Requires at least Python 3.6

  1. Install the WZL-MQTT package via pip
pip install wzl-mqtt

Usage

Publish messages

from wzl import mqtt

# 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_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, port=MQTT_PORT)

# 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

from wzl import mqtt

# 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_USER, MQTT_PASSWORD, vhost=MQTT_VHOST, port=MQTT_PORT)

# 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

Changelog

2.5.2 - 2023-03-29

  • client handles slash in at the end of prefix and the beginning of topic to avoid multiple consecutive slashes correctly now

2.5.1

  • increased verbosity in case of errors by including the full stack trace

2.5.0

  • changed signature of connect method
    • specifying the port is optional now, if not specified the port automatically is derived from the "websocket" and "ssl" flags

2.4.2

  • fixed a bug of websocket connection

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wzl-mqtt-2.5.2.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wzl_mqtt-2.5.2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file wzl-mqtt-2.5.2.tar.gz.

File metadata

  • Download URL: wzl-mqtt-2.5.2.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for wzl-mqtt-2.5.2.tar.gz
Algorithm Hash digest
SHA256 bdddb74e946f75723d1681bab09ca89f8aad84ccace9cb4f47e523399006f128
MD5 9979826876f25a551a6c00794dc5f858
BLAKE2b-256 6294d8bfde719c546e17a61c32b730067b8f5ba038d8ae7e40fe69c82aab8739

See more details on using hashes here.

File details

Details for the file wzl_mqtt-2.5.2-py3-none-any.whl.

File metadata

  • Download URL: wzl_mqtt-2.5.2-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for wzl_mqtt-2.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a932d4925c0c152215f04e35df44c8f94bd66fa22150462e7681e373f4ed39fe
MD5 90b3b48c3e601918843f2ce49828e858
BLAKE2b-256 b78303f05a62ca370dfa31e2610d2350441342f535e614e8a517a1e23073fb07

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page