"Developer Notes" MQTT python test notes

Source: Internet
Author: User

MQTT is a subscription/publish-based IoT protocol.

The Python test requires a sending and receiving process, a sending client and a receiving client, and if the two clients are working under the same topic, then the message can be exchanged.

Server with "iot.eclipse.org" is good, to avoid building a server, and then the process can also run through.

Send client code:

Import paho.mqtt.client as Mqttimport paho.mqtt.publish as Publishidx = 0
#往paho/temperature always send content while True: print ("Send Success") Publish.single ("Paho/temperature", payload = "This is message:%s"%idx, hostname= "iot.eclipse.org", client_id= "Lora1", # QoS = 0, # tls=tls, port=1883, Protocol=mqtt. MQTTv311) idx + = 1

Receive client code:

Import Paho.mqtt.client as mqtt# the callback for when the client receives a CONNACK response from the Server.def On_conne  CT (client, UserData, flags, RC):    print ("Connected with result code" +STR (RC)) # The callback for when a PUBLISH message is received from the Server.def on_message (client, UserData, msg):    #在这里处理业务逻辑    print (msg.topic+ "" +str ( Msg.payload)) client = Mqtt. Client () Client.on_connect = On_connectclient.on_message = On_messageclient.connect ("iot.eclipse.org", 1883, 60)
#订阅频道client. Subscribe ("Paho/temperature") # Blocking call, Processes network traffic, dispatches callbacks and# Handles reconnecting.# other loop* () functions is available that give a threaded interface and a# manual interface.client . Loop_forever ()

Then run two clients and you will receive a message at the receiving end.

The MQTT server is not responsible for storing data, it needs to write additional receiving clients to receive data, analysis, storage, etc.

The MQTT server uses iot.eclipse.org, and if it happens that two people are using the same channel, they may receive messages from others.

If you want to build your own MQTT server, then go back.

Play on the good, do not add too much burden to the server yo ~

Resources:

Paho-qtt Description Document

  

"Developer Notes" MQTT python test notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.