mqtt 參考:html
https://pypi.org/project/paho-mqtt/python
https://github.com/eclipse/paho.mqtt.pythongit
#服務端github
[root@localhost ~]# cat mqtt_server.py服務器
import paho.mqtt.client as mqtt import paho.mqtt.publish as publish idx = 0 #往paho/temperature 一直髮送內容 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
#客戶端app
[root@localhost ~]# cat mqtt_client.py eclipse
import paho.mqtt.client as mqtt # The callback for when the client receives a CONNACK response from the server. def on_connect(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_connect client.on_message = on_message client.connect("iot.eclipse.org", 1883, 60) #訂閱頻道 client.subscribe("paho/temperature") # Blocking call that processes network traffic, dispatches callbacks and # handles reconnecting. # Other loop*() functions are available that give a threaded interface and a # manual interface. client.loop_forever()
#啓動服務端,再啓動客戶端oop
#服務端一直髮送信息 [root@localhost ~]# python3 mqtt_server.py send success send success send success send success send success send success send success send success send success send success send success #客戶端接收信息 [root@localhost ~]# python3 mqtt_client.py Connected with result code 0 paho/temperature b'this is message:125' paho/temperature b'this is message:126' paho/temperature b'this is message:127' paho/temperature b'this is message:128' paho/temperature b'this is message:129' paho/temperature b'this is message:130'
備註:post
MQTT服務器不負責存儲數據,須要編寫額外的接收客戶端來接收數據、分析、入庫等。測試
MQTT服務器用的是iot.eclipse.org,若是碰巧兩我的在用同一個頻道,那可能收到別人的消息哦~
搭建mqtt服務器
參考:
http://emqtt.com/docs/v2/install.html
https://blog.csdn.net/qq_37258787/article/details/79776663
2、Mosquitto安裝和使用
https://lanseyujie.com/post/mosquitto-installation-and-usage.html
3、安裝mqttfx(用於測試mqtt-服務器)
至關於部署一個mqtt-server
軟件下載:
http://www.jensd.de/apps/mqttfx/1.7.0/
使用方法參考:
https://blog.csdn.net/nicholaszao/article/details/79211965