Skip to main content

MQTT utility scripts made easy

Project description

A little glue package to make it simple to quickly put together scripts that bridge MQTT and other libraries. See examples below.

Installation

Install from PyPI:

$ pip install mqttwrapper

By default paho-mqtt will be used as the MQTT library, but you can use hbmqtt if you wish - see below. To install, use:

$ pip install mqttwrapper[hbmqtt]

Usage

mqttwrapper.run_script handles the setup/maintenance of the MQTT connection and subscriptions to topics, and calls a callback function when messages are received.

The simplest script looks something like this:

from mqttwrapper import run_script

def callback(topic, payload):
    print("Received payload {} on topic {}".format(payload, topic))

def main():
    run_script(callback, broker="mqtt://127.0.0.1", topics=["my/awesome/topic1", "another/awesome/topic2"])

Any extra keyword arguments passed to run_script are passed back to the callback function when it’s called:

from mqttwrapper import run_script

def callback(topic, payload, context, foo):
    assert context == "hello"
    assert foo == "bar"
    print("Received payload {} on topic {}".format(payload, topic))

def main():
    run_script(callback, broker="mqtt://127.0.0.1", topics=["my/awesome/topic1", "another/awesome/topic2"], context="hello", foo="bar")

Configuration

broker and topics can be omitted from the run_script call and environment variables used instead:

  • MQTT_BROKER: MQTT broker to connect to, e.g. mqtt://127.0.0.1/

  • MQTT_TOPICS: Comma-separated list of topics to subscribe to, e.g. my/topic1,my/topic2

asyncio/hbmqtt

The asyncio-powered hbmqtt MQTT library can be used instead, if you like:

from mqttwrapper.hbmqtt_backend import run_script

NB hbmqtt’s reconnection handling does not resubscribe to topics upon reconnection, and mqttwrapper does not yet work around this.

Examples

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

mqttwrapper-0.0.2.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

mqttwrapper-0.0.2-py2.py3-none-any.whl (5.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file mqttwrapper-0.0.2.tar.gz.

File metadata

  • Download URL: mqttwrapper-0.0.2.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mqttwrapper-0.0.2.tar.gz
Algorithm Hash digest
SHA256 75e6c219a3ed091436561cc93d46543828da17a0f0b49d1d883b8a55bb78c6c9
MD5 78765fd05a0f9af2e54f56815f6b9aa8
BLAKE2b-256 e64ff1547872badcf71566d147c8a791bf3f4b9fdc45b6c393e555e6ab779f9d

See more details on using hashes here.

File details

Details for the file mqttwrapper-0.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for mqttwrapper-0.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d6cf30dfb0e61f0075f0a15011b7d843644be2e7758569c37bfa61b8d9e1217a
MD5 e09b5f3f1c639ce311c2adba1f0510f5
BLAKE2b-256 81869e17e7d72a1162937a14f09529e64bca37f92ea946b2dca126c7f602e9ed

See more details on using hashes here.

Supported by

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