Skip to main content

Paho's MQTT dev Toolkit

Project description

Paho's MQTT toolkit

Welcome to Paho's MQTT Toolkit.

For who? and why?

This toolkit if for those who have gone through the documentation of the Paho's MQTT library and its examples and, like me, have felt that the API is somehow off and complicated to onboard applications.

I, personally, like Paho's MQTT library and I don't wish to replace it in anyway. However, I want to write applications that in a way, are familiar to the frameworks I use, e.g., Flask or Sanic. And therefore, abstracting the underlying implementation by using a solid configuration base with a simplified API.

What's the base configuration?

The base configuration of this toolkit uses Paho's event loop implementation by using the start_loop and stop_loop methods and creating a single entrypoint method that accepts different configuration parameters, start_async.

Simple Usage

For more examples refer to the examples directory.

import time

from paho.mqtt.client import Client

from pahotoolkit import (
    init,
    start_async,
    stop_async,
    subscribe,
    json_message,
)

from my_settings import (
    HOST,
    PORT,
    USERNAME,
    PASSWORD,
    LOG_CONFIG
)


@subscribe('/temperature')
def handle_temperature(mqtt_client: Client, userdata, message):
    print(f'Got {message}')


@subscribe('/my/topic')
@json_message()  # by default, non-json messages in the topic are ignored
def listen_my_topic(payload: dict, *args, **kwargs):
    # todo: do something with the payload
    pass


@on_connect()
def client_connected(mqtt_client: Client, *args, **kwargs):
    """Publish something on_connect."""
    mqtt_client.publish('/clients', 'Paho\'s MQTT toolkit message!')


# log_config => https://docs.python.org/3/library/logging.config.html#logging.config.dictConfig
# the default MQTT Client uses the default's MQTT Client constructor
# https://github.com/eclipse/paho.mqtt.python#client-1
def main():
    client: Client = start_async(host=HOST, port=PORT,  # port default to 1883 if not given
                                 username=USERNAME, password=PASSWORD,  # optional fields
                                 initialize=False, log_config=LOG_CONFIG)  # optional fields

    init(client)  # only required if initialize=False, else by default is automatically called

    while True:
        time.sleep(1)  # or do something in this thread...


if __name__ == '__main__':
    try:
        main()
    finally:
        stop_async()

Contact

Arnulfo Solis

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

pahotoolkitfork-0.1.4.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

pahotoolkitfork-0.1.4-py2.py3-none-any.whl (12.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pahotoolkitfork-0.1.4.tar.gz.

File metadata

  • Download URL: pahotoolkitfork-0.1.4.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.28.0 CPython/2.7.15

File hashes

Hashes for pahotoolkitfork-0.1.4.tar.gz
Algorithm Hash digest
SHA256 cef244ca29397249353010cef86d064f40a38cd64454e15926c4eee9344b6829
MD5 1d5349efa11e2476bb48d37d37b3b968
BLAKE2b-256 7f27a4b16e7a1ebf204f62927314ce88dfbcff806f5787c0e8e1edcccf18e7ad

See more details on using hashes here.

File details

Details for the file pahotoolkitfork-0.1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: pahotoolkitfork-0.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.28.0 CPython/2.7.15

File hashes

Hashes for pahotoolkitfork-0.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 210c00b3736df77ef8038d6a39a9c73bde1b272bb7f76c45a21431eb93601a01
MD5 2bface5e77c0d5aef45a59aae308909b
BLAKE2b-256 38233a90396e02d58acd71d8a498ab236e967679739b6dc62afe7489b9f9e751

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