Package: django-mqtt-bridge
MQTT-Django BridgeInterface MQTT ASGI compatible with Django Channels 3.Installationpipinstalldjango-mqtt-bridgeConfigurationFirst you would to configure a validchannel_layerendpoint to consume and connect betweendjango-mqtt-bridgeand channels.Inyour_channel_application/asgi.py:importosos.environ.setdefault("DJANGO_SETTINGS_MODULE","your_channel_application.settings")importdjangofromchannels.routingimportget_default_applicationfromchannels.layersimportget_channel_layerdjango.setup()# Applicationapplication=get_default_application()# Layerschannel_layer=get_channel_layer()Inyour_channel_application/consumers.py:from channels.consumer import SyncConsumerclassMqttConsumer(SyncConsumer):defmqtt_sub(self,event):topic=event['text']['topic']payload=event['text']['payload']# do something with topic and payloadprint('{}-{}'.format(topic,payload))defmqtt_pub(self,event):passInyour_channel_application/routing.py:fromdjango.core.asgiimportget_asgi_applicationfrom.consumersimportMqttConsumerfromchannels.routingimportProtocolTypeRouter,ChannelNameRouter,URLRouterfromchannels.authimportAuthMiddlewareStackapplication=ProtocolTypeRouter({"http":get_asgi_application(),"channel":ChannelNameRouter({"mqtt.pub":MqttConsumer.as_asgi()})})Usagedjango_mqtt_bridge-Hiot.eclipse.org-p1883--topic=some_topic:2your_channel_application.asgi:channel_layerOptionsdjango_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.subHOST: MQTT broker hostPORT: MQTT broker port, default 1883USERNAME: MQTT username to authorised connectionPASSWORD: MQTT password to authorised connectionTOPICS: 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:0CHANNEL_NAME: Name of Channels's channel to send and receive messages, thischannel_namemust to exist in your channel's consumer, default ismqtt.CHANNEL_SUB: Name of Channels's channel for MQTT Sub messages, default ismqtt.sub.CHANNEL_PUB: Name of Channels's channel for MQTT Pub messages, default ismqtt.pub.