It is a django module that allow your:
- Mosquito Auth configured with mosquitto-auth-plug
- Automatic MQTT replay
- MQTT server
Edit settings.py
and add:
INSTALLED_APPS = (
...
'django.contrib.admin',
'django.contrib.auth',
...
'django_mqtt',
'django_mqtt.mosquitto.auth_plugin',
'django_mqtt.publisher',
'django_mqtt.server',
...
)
# Used for storage certs and keys if 'django_mqtt.publisher' is Installed
MQTT_CERTS_ROOT = /path/to/private/certs/storage
# Test Example: MQTT_CERTS_ROOT = os.path.join(BASE_DIR, 'private')
# Used for 'django_mqtt' if 'django_mqtt.server' or 'django_mqtt.mosquitto.auth_plugin' is Installed
# Optional MQTT_ACL_ALLOW indicated if must allow topic not asigned for the user
MQTT_ACL_ALLOW = False
# Optional MQTT_ACL_ALLOW_ANONIMOUS indicated if must allow topic not valid users
MQTT_ACL_ALLOW_ANONIMOUS = MQTT_ACL_ALLOW
Browser to your admin page and configure: broken servers, auth and client data.
You can add the following code for send Data model when new one are created:
from django.db.models.signals import post_save
from django_mqtt.publisher.models import Data as MQTTData
def update_mqtt_data(sender, **kwargs):
obj = kwargs["instance"]
if isinstance(obj, MQTTData):
if kwargs["created"]:
obj.update_remote()
post_save.connect(receiver=update_mqtt_data, sender=MQTTData, dispatch_uid='django_mqtt_update_signal')
Or you can auto-send with any change using:
from django.db.models.signals import post_save
from django.dispatch import receiver
from django_mqtt.publisher.models import Data as MQTTData
@receiver(post_save, sender=MQTTData)
def auto_update(sender, instance, **kwargs):
instance.update_remote()
You can also attach django Signals for monitoring publisher, connection and disconnection.
from django_mqtt.publisher.models import *
from django_mqtt.publisher.signals import *
def before_connect(sender, client):
if not isinstance(client, Client):
raise AttributeError('client must be Client object')
mqtt_connect.connect(receiver=before_connect, sender=Server, dispatch_uid='my_django_mqtt_before_connect')
def before_publish(sender, client, topic, payload, qos, retain):
if not isinstance(client, Client):
raise AttributeError('client must be Client object')
mqtt_pre_publish.connect(receiver=before_publish, sender=Data, dispatch_uid='my_django_mqtt_before_publish')
def then_publish(sender, client, userdata, mid):
if not isinstance(client, Client):
raise AttributeError('client must be Client object')
mqtt_publish.connect(receiver=then_publish, sender=Client, dispatch_uid='my_django_mqtt_then_publish')
def then_disconnect(sender, client, userdata, rc):
if not isinstance(client, MQTTClient):
raise AttributeError('client must be MQTTClient object')
mqtt_disconnect.connect(receiver=then_disconnect, sender=Server, dispatch_uid='my_django_mqtt_then_disconnect')
Thanks to mosquitto-auth-plug you can configure Mosquitto for connect with externals Auth systems.
For active Django Auth system edit your urls.py
and add:
urlpatterns = patterns(
...
url(r'^mqtt/', include('django_mqtt.mosquitto.auth_plug.urls')),
...
)
Run script install_mosquitto_auth_plugin.sh for install mosquitto server and run script compile_mosquitto_auth_plugin.sh and configure_mosquitto_auth_plugin.sh for configure it for use mosquitto-auth-plug with compiler configuration in config.mk and mosquitto configuration server with auth_plug.conf.
You can use the mosquitto test server test.mosquitto.org
.
See the mosquitto test server website for information about the broken configuration
Run scripts INSTALL.sh bash test_web/INSTALL.sh
and CONFIGURE.sh bash test_web/CONFIGURE.sh
.
This script will be install and configure mosquitto, mosquitto-auth-plug, gunicorn, supervisord, nginx and postgresql