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_factoryand 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_mqttandconnect_aio_mqtt. - Multi-Device Management: Use the
managerto 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
- Device Registration: Use
serial_device_factoryto create a serial device and register it with themanager. Each device is associated with an MQTT topic for message routing. - Manager Setup: Initialize the
managerin either "threaded" or "async" mode, depending on your application's requirements. - MQTT Integration: Use the provided helper functions to connect the
managerto your MQTT client (paho-mqttoraiomqtt). - Run Your Application: Start the
managerand MQTT client loop to enable communication.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ki2_mqtty-1.1.2.tar.gz.
File metadata
- Download URL: ki2_mqtty-1.1.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8bf5eaa65d604e680e3b5a67f96f0cf03b3acd8880a0579bf14c12697f64e0c
|
|
| MD5 |
356836eb8dd3c4464a2ff60a3a822350
|
|
| BLAKE2b-256 |
7781eb68d41de0aece20e12dab098a9f2ded0290cdbcfa969b46a172a4a0c9c5
|
File details
Details for the file ki2_mqtty-1.1.2-py3-none-any.whl.
File metadata
- Download URL: ki2_mqtty-1.1.2-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a904773174eed05a0b170d87c9b729484b59a0fb0786e5455d595651a102376
|
|
| MD5 |
3266430b071b584d5a40a3fd6244723f
|
|
| BLAKE2b-256 |
2032e35e419f1242e9e14b59bcf64301518b00b9e066471a093b7118c371cdb9
|