Skip to main content

A user friendly wrappr for easy mqtt operations

Project description

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.

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

mqttwrappr-0.0.16.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

mqttwrappr-0.0.16-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file mqttwrappr-0.0.16.tar.gz.

File metadata

  • Download URL: mqttwrappr-0.0.16.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for mqttwrappr-0.0.16.tar.gz
Algorithm Hash digest
SHA256 12f7a842ab575351bdb4b22a027ce13e6a80ebacf1d0310442b356e1f03d1207
MD5 4feae9c8e4795142c851d6097358242f
BLAKE2b-256 a3c6d5788587d2bf6b8eda06143f19d960f768eafd6a987f9245519fa62e58f3

See more details on using hashes here.

File details

Details for the file mqttwrappr-0.0.16-py3-none-any.whl.

File metadata

  • Download URL: mqttwrappr-0.0.16-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for mqttwrappr-0.0.16-py3-none-any.whl
Algorithm Hash digest
SHA256 5aef03db0e07e5f87422025e0dd61c69b8b5c54fcbe08044e2aefa17c8839aae
MD5 c3752e7a0e9507d5db87df58a95bb377
BLAKE2b-256 5fdddc38abf7f395470d08ad4f01caa43bb78379ba226177a7a52474a53323a0

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