概念:
MQTT(訊息遙測傳輸) 是ISO【國際標準化組織】標準下基于 **發布/訂閱** 范式的**訊息協議**,它是作業在 TCP/IP協議簇上的,是為**硬體性能低**下的**遠程設備**以及網路狀況糟糕的情況下而設計的 發布/訂閱型 訊息協議,為此,需要一個訊息中間件,
? MQTT是一個基于 客戶端-服務器 的訊息發布/訂閱的傳輸協議, 協議是輕量,簡單,開放和易于實作的,優點在于,可以以極少的代碼和有限的寬帶,為連接遠程設備提供實時可靠的訊息服務,
特性:
? 1、使用發布/訂閱訊息模式,提供一對多的訊息發布,解除應用程式耦合;
? 2、對負載內容屏蔽的訊息傳輸;
? 3、使用 TCP/IP 提供網路連接;
? 4、有三種訊息發布服務質量:
運行機制:
? 實作MQTT協議的通訊,需要服務器與客戶端來完成,而在實際程序中,MQTT協議中有三種身份:發布者(Publish)、代理(Broker)(服務器)、訂閱者(Subscribe),其中,訊息代理是服務器,訊息的發布者和訂閱者都是客戶端,并且訊息發布者可以同時是訂閱者,
MQTT傳輸的訊息分為: 主題(Topic)和負載(payload)? 兩部分:
? (1)Topic,主題,可以理解為訊息的型別,訂閱者訂閱(Subscribe)后,就會收到該主題的訊息內容(payload)
? (2)payload,負載,可以理解為訊息的內容,是指訂閱者具體要使用的內容
MQTT是一種簡單、穩定、開放、輕量級易于實作的訊息協議,在物聯網的應用下的資訊采集,工業控制,智能家居等方面具有廣泛的適用性,
MQTT主要應用領域
MQTT協議廣泛應用于物聯網、移動互聯網、智能硬體、車聯網、電力能源等領域,
如:物聯網M2M通信,物聯網大資料采集
? Android訊息推送,WEB訊息推送
? 移動即時訊息,例如Facebook Messenger
? 智能硬體、智能家具、智能電器
? 車聯網通信,電動車站樁采集
? 智慧城市、遠程醫療、遠程教育
? 電力、石油與能源等行業市場
例子:
新建一個 mqtt_public.py檔案 【發布者】
import paho.mqtt.client as mqtt
import json
import time
from main import Main
# 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)) #rc的值很重要,為0代表連接成功,
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("$SYS/#") #訂閱$SYS/下的所有主題
# 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("ADADWWRWFGWERWRWFGTERTER-PUB")
client.username_pw_set("WeightUpdate-PUB", password="1qaz#EDC")
client.on_connect = on_connect #連接broker時broker回應的回呼
client.on_message = on_message #接收到訂閱訊息時的回呼
client.connect("192.168.10.200", 1883, 190) #連接到broker
print(client.is_connected())
for i in range(1000):
# msg = Main().up_weight()
# print(msg,"msg")
client.publish(f"aicp/sound/soundDevice/new/report/", qos=0, payload=json.loads("kkkkkkkkkkkkk"))
time.sleep(1)
新建一個 mqtt_sub.py檔案 【訂閱者】
import paho.mqtt.client as mqtt
import json
class EstimationWeightUpdate(object):
def __init__(self):
self.init_mqtt()
# The callback for when the client receives a CONNACK response from the server.
def on_connect(self, client, userdata, flags, rc):
print("Connected with result code "+str(rc)) #rc的值很重要,為0代表連接成功,
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
self.client.subscribe("$SYS/#") #訂閱$SYS/下的所有主題
# The callback for when a PUBLISH message is received from the server.
def on_message(self, client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
def init_mqtt(self):
self.client = mqtt.Client("ADADWWRWFGWERWRWFGTERTER-SUB")
self.client.username_pw_set("WeightUpdate-SUB", password="1qaz#EDC")
self.client.on_connect = self.on_connect #連接broker時broker回應的回呼
self.client.on_message = self.on_message #接收到訂閱訊息時的回呼
def subscribe_update_topic(self):
self.client.connect("192.168.10.200", 1883, 60) #連接到broker
self.client.subscribe("aicp/sound/soundDevice/new/report/", 0)
self.client.loop_forever()
if __name__ == "__main__":
ew = EstimationWeightUpdate()
ew.subscribe_update_topic()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/295494.html
標籤:其他
下一篇:STM32CubeMX安裝