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