Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use MQTT method in Django

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of how to use the MQTT method in Django, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value, I believe you will have something to gain after reading this Django article on how to use the MQTT method, let's take a look at it.

MQTT is the abbreviation of message queue telemetry transmission. It is a message protocol based on publish / subscribe paradigm. It is widely used in a variety of low-performance network devices or bad network conditions for message transmission. Install M on Django

Install MQTT

It is also very easy to install MQTT in Python environment. What you need to pay attention to is not to type the wrong command.

The command to install Python MQTT in a Python3 environment is:

Pip3 install paho-mqtt uses Python MQTT

Write Python MQTT scripts

The code is as follows:

# in order to call the Django ORM model in an external script, you must configure the script environment variable and register the script to import os in the environment variable of Django. The first parameter of sysimport django# is fixed The second parameter is the project name .settingsos.sett.setdefault ('DJANGO_SETTING_MODULE',' my_django.settings') django.setup () # introducing the mqtt package import paho.mqtt.client as mqtt# uses a separate thread to run the from threading import Threadfrom app name import modelsimport timeimport json# to establish a mqtt connection def on_connect (client, userdata, flag, rc): print ("Connect with the result code" + str (rc) client.subscribe ('test/#') Qos=2) # receive and process mqtt messages def on_message (client, userdata) Msg): out = str (msg.payload.decode ('utf-8')) print (msg.topic) print (out) out = json.loads (out) # execute the task after receiving the message if msg.topic = =' test/newdata': print (out) # mqtt client startup function def mqttfunction (): global client # use loop_start to avoid blocking Django processes Using loop_forever () may block the system process # client.loop_start () # client.loop_forever () has dropped reconnection function client.loop_forever (retry_first_connection=True) client = mqtt.Client (client_id= "test") Clean_session=False) # Startup function def mqtt_run (): client.on_connect = on_connect client.on_message = on_message # bind MQTT server address broker = '192.168.1.88' # MQTT server port number client.connect (broker, 1883, 62) client.username_pw_set ('user',' user') client.reconnect_delay_set (min_delay=1 Max_delay=2000) # launch mqttthread = Thread (target=mqttfunction) mqttthread.start () # launch MQTT# mqtt_run () if _ _ name__ = = "_ _ main__": mqtt_run () starts the script in the Django project

Under Django2.1, you only need to import the script file to be executed in wsgi.py, and then execute the startup function.

Such as:

From stores the MQTT script module import script name # if the script name is: mqtt_functionsmqtt_functions.mqtt_run () on "how to use the MQTT method in Django" this article is introduced here, thank you for reading! I believe you all have a certain understanding of "how to use the MQTT method in Django". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report