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 protocol to connect OneNet in Python

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about how to use the MQTT protocol to connect to OneNet in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

A brief introduction to MQTT

MQTT (Message Queuing Telemetry Transport, message queuing Telemetry Transmission) is an instant messaging protocol developed by IBM, which may become an important part of the Internet of things. The protocol supports all platforms, connects almost all connected objects to the outside, and is used as a communication protocol for sensors and brakes, such as connecting houses through Twitter.

II. Characteristics of MQTT

MQTT protocol is designed for the communication of a large number of remote sensors and control devices with limited computing power and working in low-bandwidth, unreliable networks. It has the following main characteristics:

1. Use the publish / subscribe message model to provide one-to-many message publishing and uncouple the application.

2. Message transmission that shields the load content

3. Use TCP/IP to provide network connection

4. There are three kinds of message publishing service quality:

"at most once," the release of messages depends entirely on the underlying TCP/IP network. Messages can be lost or duplicated. This level can be used in the case of environmental sensor data, it doesn't matter if you lose a read record, because there will be a second transmission soon.

"at least once" to ensure that the message arrives, but message repetition may occur.

"only once", make sure the message arrives once. This level can be used in situations where duplicate or lost messages can lead to incorrect results in a billing system.

5. Small transmission with low overhead (fixed length header is 2 bytes). Protocol switching is minimized to reduce network traffic.

6. The mechanism for notifying the parties concerned of abnormal client interruptions using Last Will and Testament features

3. OneNet platform MQTT access instructions: click to view

4. Complete example of python code: (upload initialization value, receive server value change in real time)

# prepare in advance, register the OneNet Internet of things platform, create a project, and create a data flow from _ _ future__ import print_functionimport paho.mqtt.client as mqttimport structimport json# CONNECT: # client_id: DEV_ID# username: PRO_ID# password: AUTHINFO (authentication information) # you can connect to the device cloud CONNECT and CONNACK handshake succeeded # temperature: a data stream has been created # more please refer to the OneNet official mqtt documentation and paho-mqtt development documentation # modify it to your own DEV_ID = "29455901" # device IDPRO_ID = "12555524" # Product IDAUTH_INFO = "JHFDJF55586jhk" # APIKEYTYPE_JSON = 0x01TYPE_FLOAT = 0x17# defines the json format of uploaded data which is modified according to the format specified by oneNET But body = {"datastreams": [{"id": "wendu" # data flow name "datapoints" corresponding to OneNet: [{"at": "2016-08-15T14:47:00", # data submission time You can obtain the real-time time "value": 55 # data value}]}]} def build_payload (type, payload): datatype = type packet = bytearray () packet.extend (struct.pack ("! B") Datatype) if isinstance (payload, str): udata = payload.encode ('utf-8') length = len (udata) packet.extend (struct.pack ("! h" + str (length) + "s", length, udata)) return packet# callback when the client receives a CONNACK response from the server. That is, to apply for a connection, whether the result returned by the server is successful, such as def on_connect (client, userdata, flags, rc): print ("connection result:" + mqtt.connack_string (rc)) # upload data json_body = json.dumps (body) packet = build_payload (TYPE_JSON, json_body) client.publish ("$dp", packet, qos=1) # qos represents the callback when receiving and publishing messages from the server. Def on_message (client, userdata, msg): print ("temperature:" + str (msg.payload,'utf-8') + "°C") # when the message has been sent to the middleman The on_publish () callback will be triggered by def on_publish (client, userdata, mid): print ("mid:" + str (mid)) def main (): client = mqtt.Client (client_id=DEV_ID, protocol=mqtt.MQTTv311) client.on_connect = on_connect client.on_publish = on_publish client.on_message = on_message client.username_pw_set (username=PRO_ID, password=AUTH_INFO) client.connect ('183.230.40.39), port=6002 Keepalive=120) client.loop_forever () if _ _ name__ = ='_ _ main__': main () finish reading the above Do you have any further understanding of how to connect to OneNet using MQTT protocol in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report