ehooo/django_mqtt

facing problem with django(2.1.5) while integrating MQTT

Closed this issue · 1 comments

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.

ehooo commented

Could you upload the traceback of the error?
I don't have any test on Django 2.

Have you try with the commands: mqtt_publisher, mqtt_subscriber or mqtt_updater

You must create the mqtt config on DB and them you could use the commands https://github.com/ehooo/django_mqtt#how-to-update-data-from-remote-mqtt

Please let me know any question you have.