Skip to main content

mqttwrappr

pypi versions License: MIT

This library provides a simple interface for interacting with MQTT brokers using the Paho MQTT client. It includes classes for managing MQTT connections, publishing messages, and subscribing to topics with customizable message dispatching.

⚠️ Note: This package is currently in active development. It is not yet feature-complete. Users should expect changes and potential instability.

👥 Please feel free to test and contribute.

🚀 Quickstart Example

Creating an MQTT Client

To create an MQTT client, you can either instantiate it directly or use a YAML configuration file.

Creating client

from mqttwrappr import MqttClient

# Create an MQTT client instance
mqtt_client = MqttClient(
    broker='mqtt.example.com',
    port=1883,
    username='your_username',
    password='your_password'
)

Creating client from config file

(recommended way)

#config.yaml
broker: mqtt.example.com
port: 1883
username: your_username
password: your_password
from mqttwrappr import MqttClient

# Create an MQTT client instance from a YAML configuration file
mqtt_client = MqttClient.from_yaml('config.yaml')

Subscribing to a Topic

You can subscribe to a topic and handle incoming messages using a custom dispatch function. A custom dispatch function is a user-defined function that processes incoming MQTT messages. It serves as a flexible mechanism to handle messages received from subscribed topics, allowing developers to implement specific logic tailored to their application's needs. It is crucial that the custom dispatch function accepts exactly two parameters: topic and message. The function must be defined with precisely these two parameters—no more, no less—to ensure compatibility with the MQTT client's dispatch mechanism.

def custom_dispatch(topic, message):
    print(f"Custom Dispatch - Topic: {topic}, Message: {message}")

# Subscribe to a topic with a custom dispatch function
mqtt_client.subscribe(topic='test/topic', qos=1, dispatch_fnc=custom_dispatch)

Publishing Messages

You can publish messages to a topic. Messages can be strings or dictionaries (which are converted to JSON).

# Publish a simple string message
mqtt_client.publish(topic='test/topic', message='Hello, MQTT!')

# Publish a dictionary as a JSON string
mqtt_client.publish(topic='test/topic', message={'key': 'value'})

Interactive Terminal Publishing

You can also publish messages interactively from the terminal.

# Start interactive terminal input for publishing messages
mqtt_client.publish_on_terminal_input()

please also check out the examples provided

🔨 Installation

pip install mqttwrappr

if you want to run your own mqtt server check out setup-mosquitto-with-docker

👨‍💻 Contributors:

©️ License

This project is licensed under the MIT License.

Metadata

Release files for mqttwrappr 0.0.17

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mqttwrappr 0.0.17
File Size Uploaded
mqttwrappr-0.0.17.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mqttwrappr 0.0.17
File Interpreter ABI Platform
mqttwrappr-0.0.17-py3-none-any.whl Python 3 none any Details

Total release size: 16.7 kB

Release files / mqttwrappr-0.0.17.tar.gz

Download URL mqttwrappr-0.0.17.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
8b61ffccc2a977764c4925bb97efc8e96affd57a19878c6f6fbd686c46902b5b
BLAKE2b-256 checksum
How to use checksums
3eaeade2919cf3e5dcf50c1dd94298e63331232c83900417f8dca6b7fc67ad1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / mqttwrappr-0.0.17-py3-none-any.whl

Download URL mqttwrappr-0.0.17-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a2c4383a8cf299d9555dbb7478a440c58aa387ee3e8a62282aaac0601ccc67a
BLAKE2b-256 checksum
How to use checksums
1399cf207893efdcf9666bde68136ce164031774f883930eaf06ecce1f926e2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release history Release notifications | RSS feed

This release

0.0.17 This release

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page