Skip to main content

mqtty is a flexible Python package that bridges MQTT communication with serial devices. It provides an agnostic approach to MQTT client libraries, supporting both paho-mqtt and aiomqtt through helper functions, while allowing developers to manage multiple serial devices seamlessly.

Project description

mqtty

mqtty is a flexible Python package that bridges MQTT communication with serial devices. It provides an agnostic approach to MQTT client libraries, supporting both paho-mqtt and aiomqtt through helper functions, while allowing developers to manage multiple serial devices seamlessly.

Key Features

  • Serial Device Integration: Easily connect serial devices using serial_device_factory and register them under unique MQTT topics.
  • Execution Mode Flexibility: Choose between synchronous (threaded) or asynchronous modes to suit your application's requirements.
  • MQTT Client Agnostic: Use your preferred MQTT library, with helper functions like connect_paho_mqtt and connect_aio_mqtt.
  • Multi-Device Management: Use the manager to register and manage multiple serial devices, routing messages between MQTT topics and serial ports effortlessly.

Installation

Install mqtty via pip:

pip install ki2-mqtty

Quick Start

Here are examples of how to use mqtty in synchronous and asynchronous modes.

Synchronous Example

import paho.mqtt.client as mqtt
from paho.mqtt.enums import CallbackAPIVersion

from mqtty import serial_device_factory, manager_setup, connect_paho_mqtt

def main():
    mqttclient = mqtt.Client(CallbackAPIVersion.VERSION2)

    device = serial_device_factory("/dev/ttyACM0")
    manager = manager_setup("threaded").register("device-topic", device)

    connect_paho_mqtt(mqttclient, manager=manager)

    manager.start()
    mqttclient.connect("localhost", 1883, 60)
    mqttclient.loop_forever()

if __name__ == "__main__":
    main()

Asynchronous Example

from datetime import datetime
import asyncio

from aiomqtt import Client as AioMqttClient

from ki2_python_utils import run_parallel
from mqtty import serial_device_factory, manager_setup, connect_aio_mqtt

async def main():
    mqttclient = AioMqttClient("localhost", 1883)

    device = serial_device_factory("/dev/ttyACM0")
    manager = manager_setup("async").register("device-topic", device)

    mqtt_loop = connect_aio_mqtt(mqttclient, manager=manager)

    await run_parallel(
        manager.loop,
        mqtt_loop,
    )

if __name__ == "__main__":
    asyncio.run(main())

How It Works

  1. Device Registration: Use serial_device_factory to create a serial device and register it with the manager. Each device is associated with an MQTT topic for message routing.
  2. Manager Setup: Initialize the manager in either "threaded" or "async" mode, depending on your application's requirements.
  3. MQTT Integration: Use the provided helper functions to connect the manager to your MQTT client (paho-mqtt or aiomqtt).
  4. Run Your Application: Start the manager and MQTT client loop to enable communication.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

ki2_mqtty-1.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

ki2_mqtty-1.1.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file ki2_mqtty-1.1.0.tar.gz.

File metadata

  • Download URL: ki2_mqtty-1.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.10.12 Linux/5.15.0-130-generic

File hashes

Hashes for ki2_mqtty-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a5317ed4e9699fc7c950c960e2d8ede4d5b358abc5a96beeeea462a35cf7390f
MD5 99c0ce5901922a37253bdd96b2f1397b
BLAKE2b-256 9a5743ef5c26acb299f04272b42d72a7df8b033384306fd45c5c7a66825d85e8

See more details on using hashes here.

File details

Details for the file ki2_mqtty-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: ki2_mqtty-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.10.12 Linux/5.15.0-130-generic

File hashes

Hashes for ki2_mqtty-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70be57c1da774b1ddd601c7fab50fd1846bb735dca37751ef9fb28f8de3b04f7
MD5 3adcf99d8e5e7dac1f96ad3edd20b2f4
BLAKE2b-256 a4b2758f7abf0bb2e37e64bf71bd3337c5ea560039d6acd244d5c7bb79eb2a7a

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