Coder Social home page Coder Social logo

django_mqtt's Introduction

Django-MQTT Build Status

It is a django module that allow your:

Install

Install the latest version through pip

pip install -e git+https://github.com/ehooo/django_mqtt.git#egg=django_mqtt

Edit settings.py and add:

INSTALLED_APPS = (
  ...
  'django.contrib.admin',
  'django.contrib.auth',
  ...
  'django_mqtt',
  'django_mqtt.mosquitto.auth_plugin',
  'django_mqtt.publisher',
  ...
)

# 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.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

Also need to run migrations once

python manage.py migrate

Setting Up

Browser to your admin page and configure: Broker 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()

Attach signals

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')

Configure Mosquitto for use Django Auth

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_plugin.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.

How Mosquitto Auth works ?

You could create the follow settings to set the default auth flow:

MQTT_ACL_ALLOW = False  # For allow auth users to any topic, False by defauld
MQTT_ACL_ALLOW_ANONIMOUS = False # For allow anonimous users, False by defauld

The auth mechanism works based con ACL class.

This mean that you could "bypass" the setting creating a wildcard topic (#). For example, if you want that all auth user could subscribe but not publish:

from django_mqtt.models import Topic, ACL
from django_mqtt.models import PROTO_MQTT_ACC_SUS, PROTO_MQTT_ACC_PUB
topic = Topic.objects.create(name='#')
acl = ACL.objects.create(acc=PROTO_MQTT_ACC_PUB, topic=topic, allow=False)

If you want that only one user or group could publish to any topic, you could add it:

acl.allow=True
acl.users.add(User.objects.get(username='admin')
acl.groups.add(Group.objects.get(username='mqtt')
acl.save()

How user for publish data con MQTT server ?

All this steps could be done by shell or by admin page

  1. Create a MQTT Server
from django_mqtt.publisher.models import Server
mqtt_server = Server.objects.create(host='test.mosquitto.org')
  1. Create a MQTT client
from django_mqtt.publisher.models import Client
mqtt_client = Client.objects.create(server=mqtt_server)
  1. Create a MQTT Topic
from django_mqtt.models import Topic
mqtt_topic = Topic.objects.create(name='/django/MQTT')
  1. Create a MQTT Data object
from django_mqtt.publisher.models import Data
mqtt_data = Data.objects.create(client=mqtt_client, topic=mqtt_topic, payload='initial data')
mqtt_data.update_remote()  # Send/update data to MQTT server

How to update data from remote MQTT?

  1. Create a MQTT Server
from django_mqtt.publisher.models import Server
mqtt_server = Server.objects.create(host='test.mosquitto.org')
  1. Create a MQTT client
from django_mqtt.publisher.models import Client
mqtt_client = Client.objects.create(server=mqtt_server)
  1. Run command mqtt_updater
python manage.py mqtt_updater /topic/#

MQTT Test Brokers

You can use the mosquitto test server test.mosquitto.org. See the mosquitto test server website for information about the broker configuration

Setup your own MQTT for test

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

django_mqtt's People

Contributors

arush15june avatar daadu avatar ehooo avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

django_mqtt's Issues

newbie getting setup

Hi,

Could anyone give me a tip on getting setup? Do I need Linux to run this or is Windows 10 Ok?

I would like to try this but very new to MQTT and mostly newb to Django but I understand some of the steps and work flow.

How user for publish data con MQTT server ?
Is this all on the same machine? To run the MQTT broker through Django and also a client application?

Thank you for any help this seems like a cool project.

[Feature Request] usage documentation

It would be great if you could provide a few clearer examples of how you use the MQTT connection in a Django app.

I'm specifically wanting to do the following two tasks but am unsure how:

  • subscribe to a specific MQTT topic and keep a text field or many text fields in my HTML page up to date when new data comes in.
  • publish data to a topic/various topics upon saving a form

Many thanks in advance.

facing problem with django(2.1.5) while integrating MQTT

I am facing some difficulty to integrate MQTT in Django ORM it showing error, I don't know how to solve the problem, So please help me. I am giving you the snippet of my code.

  • mqtt.py
import paho.mqtt.client as mqtt
import json
import time
from django.conf import settings
#from .models import Emp

#from MQTT.views import get_detail

MQTT_DEFAULT_USER = '50000025'
MQTT_DEFAULT_PASSWORD = 'Test@1234'



def on_connect(client, userdata, flags, rc):
 print("on_connect")
 client.subscribe('sh/k',qos=1)
 

def on_message(client, userdata, message):
 print("This is on_message function !!!!")
 data=message.payload.decode()
 print(data)
 data=json.loads(data)
 print('-----------------------------')
 ret=get_detail(data)
 if message.payload:
 client.publish("s/us",ret) 
 print("...done...")

def on_log(client, userdata, level, buf): # this is call back function
 print("Logs :"+buf)

client = mqtt.Client('rahul')
client.username_pw_set(MQTT_DEFAULT_USER, MQTT_DEFAULT_PASSWORD )
client.on_message = on_message
client.on_connect = on_connect
client.on_log = on_log


client.connect('192.168.02.125', port=1883)
  • view.py
from django.shortcuts import render
from .models import Emp
import json
 
def get_detail(data):
 
 data=Emp.objects.filter(name=data.name)[0]
 data=json.dumps(data)
 return data
  • models.py
from django.db import models

class Emp(models.Model):
 name=models.CharField(max_length=50)
 address=models.CharField(max_length=50)
 salary=models.DecimalField(max_digits=10,decimal_places=2)

 class Meta:
 db_table='emp'

These are the file which is containing my Django project, I want to do the operation on that data which is provided an MQTT publisher client and send the data means publish one topic with the response data. how to do.

GPL Licence question

I see this popular project uses the GPL license. Often in Python land I've learned this isn't always intentional, and sometimes a mistake.

Is your intention that any project that uses this library should be open sourced as GPL?
Basically, an commercial usage of this library in a private project is now forbidden and risks lawsuit.

If that wasn't your intention, could you please change the license to LGPL? (or BSD). Using the LGPL would still enforce authors to contribute any changes to this library, but no longer enforces any project that uses this library to become GPL too.

Commands for windows

Thanks for your reference @ehooo but I facing issues for commands in windows because you only given UNIX commands here but I needed Windows Commands

Secure confs ADD

I can't add the certificates. I encountered two errors

Select a valid choice. VerifyMode.CERT_REQUIRED is not one of the available choices.
Select a valid choice. _SSLMethod.PROTOCOL_TLSv1 is not one of the available choices.

TypeError: __init__() missing 1 required positional argument: 'on_delete'

Hi Team,
I'm using Django 2.2.6 and Python 3.7
I'm facing this issue please resolve it.
Thanks.

class ACL(models.Model):
File "/home/enigma/Desktop/project_iot/backend/venv/src/django-mqtt/django_mqtt/models.py", line 202, in ACL
topic = models.ForeignKey(Topic) # There is many of acc options by topic
TypeError: init() missing 1 required positional argument: 'on_delete'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.