Skip to main content

Interface MQTT ASGI compatible with Django Channels 3.

Project description

MQTT-Django Bridge

Interface MQTT ASGI compatible with Django Channels 3.

Installation

pip install django-mqtt-bridge

Configuration

First you would to configure a valid channel_layer endpoint to consume and connect between django-mqtt-bridge and channels.

In your_channel_application/asgi.py:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_channel_application.settings")

import django
from channels.routing import get_default_application
from channels.layers import get_channel_layer

django.setup()

# Application
application = get_default_application()

# Layers
channel_layer = get_channel_layer()

In your_channel_application/consumers.py:

from channels.consumer import SyncConsumer

class MqttConsumer(SyncConsumer):
    def mqtt_sub(self, event):
        topic = event['text']['topic']
        payload = event['text']['payload']
        # do something with topic and payload
        print('{} - {}'.format(topic, payload))

    def mqtt_pub(self, event):
        pass

In your_channel_application/routing.py:

from django.core.asgi import get_asgi_application
from .consumers import MqttConsumer
from channels.routing import ProtocolTypeRouter, ChannelNameRouter, URLRouter
from channels.auth import AuthMiddlewareStack

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "channel": ChannelNameRouter(
        {
            "mqtt.pub": MqttConsumer.as_asgi()
        }
    )
})

Usage

django_mqtt_bridge -H iot.eclipse.org -p 1883 --topic=some_topic:2 your_channel_application.asgi:channel_layer

Options

django_mqtt_bridge -h

usage: django_mqtt_bridge [-h] [-H HOST] [-p PORT] [-v] [-U USERNAME] [-P PASSWORD]
                  [--topic TOPICS] [-n CHANNEL_NAME] [-s CHANNEL_SUB]
                  [-x CHANNEL_PUB]
                  channel_layer

Simple MQTT bridge for ASGI

positional arguments:
  channel_layer         The ASGI channel layer instance to use as
                        path.to.module:instance.path

optional arguments:
  -h, --help            show this help message and exit
  -H HOST, --host HOST  MQTT broker host
  -p PORT, --port PORT  MQTT broker port
  -v, --verbosity       Set verbosity
  -U USERNAME, --username USERNAME
                        MQTT username to authorised connection
  -P PASSWORD, --password PASSWORD
                        MQTT password to authorised connection
  --topic TOPICS        MQTT topics with qos to subscribe --topic TOPIC:QOS
                        --topic /office/sensor:0 --topic /home/sensor:1 If
                        empty (#, 2) is set as default
  -n CHANNEL_NAME, --channel-name CHANNEL_NAME
                        Name of Channels's channel to send and receive
                        messages
  -s CHANNEL_SUB, --channel-sub CHANNEL_SUB
                        Name of Channels's channel for MQTT Sub messages,
                        default is mqtt.pub
  -x CHANNEL_PUB, --channel-pub CHANNEL_PUB
                        Name of Channels's channel for MQTT Pub messages,
                        default is mqtt.sub

HOST: MQTT broker host

PORT: MQTT broker port, default 1883

USERNAME: MQTT username to authorised connection

PASSWORD: MQTT password to authorised connection

TOPICS: MQTT topics with qos to subscribe. This option expect a valid topic name and a QOS value splited by :

To subscribe to a list of topics use the same option --topic.

django-mqtt-bridge --topic some_topic:qos --topic another_topic:qos --topic home/kitchen_gas_sensor:2 --topic home/office_air_sensor:0

CHANNEL_NAME : Name of Channels's channel to send and receive messages, this channel_name must to exist in your channel's consumer, default is mqtt.

CHANNEL_SUB : Name of Channels's channel for MQTT Sub messages, default is mqtt.sub.

CHANNEL_PUB : Name of Channels's channel for MQTT Pub messages, default is mqtt.pub.

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

django_mqtt_bridge-0.4.0.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

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

django_mqtt_bridge-0.4.0-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

Details for the file django_mqtt_bridge-0.4.0.tar.gz.

File metadata

  • Download URL: django_mqtt_bridge-0.4.0.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.9

File hashes

Hashes for django_mqtt_bridge-0.4.0.tar.gz
Algorithm Hash digest
SHA256 108653384e657e289c824f4fa3a9c3c2cbca399f518bb66c9c25257a06724794
MD5 999deda550bea2cd0fd778f3f3cbfb63
BLAKE2b-256 6c7cd0b77717aa0ae4a214741614b49ff0d86627a9f2caa88e3231a8fdfebf64

See more details on using hashes here.

File details

Details for the file django_mqtt_bridge-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: django_mqtt_bridge-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 19.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.6.9

File hashes

Hashes for django_mqtt_bridge-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a7e3369682aeae48fb599c28a5d03e9379bd352f555855f66c5e555fedee21c8
MD5 736a4fcbdb3b732d4427d6bcae54a5bd
BLAKE2b-256 61ea16b76f224653e9362858030d171b69d007bf1a770eab028065970d6cd84c

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