【本文出處:http://blog.csdn.net/leytton/article/details/72454287】
一、譯文
MQTT用戶端支援Mqtt3.1.1協定,要確定你的MQTT伺服器配置正确并支援3.1.1版本。此Mqtt子產品并不相容3.1版本之前的MQTT伺服器。
mqtt.Client() | 建立一個MQTT用戶端. |
mqtt.client:close() | 關閉與伺服器之間的連接配接. |
mqtt.client:connect() | 根據主機名、端口号和安全配置連接配接伺服器. |
mqtt.client:lwt() | 建立遺囑 (可選). |
mqtt.client:on() | 為事件建立回調函數. |
mqtt.client:publish() | 發送一條消息. |
mqtt.client:subscribe() | 訂閱一個或多個主題. |
mqtt.client:unsubscribe() | 取消訂閱一個或多個主題. |
1、mqtt.Client()
建立一個MQTT用戶端。
文法(PS:中括号内為可選參數)
mqtt.Client(clientid, keepalive[, username, password, cleansession])
參數
-
用戶端idclientid
-
心跳秒數keepalive
-
使用者名username
-
密碼password
-
清除session 0/1 表示cleansession
/否
是
傳回
Mqtt用戶端
示例代碼
-- 初始化無需登陸的用戶端, 心跳時間 120秒
m = mqtt.Client("clientid", 120)
-- 初始化需要登陸的用戶端, 心跳時間 120秒
m = mqtt.Client("clientid", 120, "user", "password")
-- 建立遺囑(可選)
-- 伺服器将會發送一條 qos = 0, retain = 0, 内容為"offline"的消息到"/lwt"主題,如果沒收到用戶端發送的心跳資料包(掉線)
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(client) print ("connected") end) --連接配接到伺服器觸發事件
m:on("offline", function(client) print ("offline") end) --掉線觸發事件
-- 收到消息時觸發事件
m:on("message", function(client, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
-- 對于 TLS: m:connect("192.168.11.118", secure-port, 1)
m:connect("192.168.11.118", 1883, 0, function(client) print("connected") end,
function(client, reason) print("failed reason: "..reason) end)
-- 確定subscribe/publish方法在連接配接上伺服器後再調用,在實際應用中是把他們放在connect回調函數裡或者确定連接配接成功
-- 訂閱/topic主題、服務品質為0
m:subscribe("/topic",0, function(client) print("subscribe success") end)
-- 發送一條資訊 data = hello, QoS = 0, retain = 0
m:publish("/topic","hello",0,0, function(client) print("sent") end)
m:close();
-- 你可以再次調用 m:connect 連接配接函數
2、mqtt.client:close()
關閉與伺服器的連接配接
文法 mqtt:close()
參數 無
傳回 成功true,失敗false
3、mqtt.client:connect()
根據主機名、端口号和安全配置連接配接伺服器。
文法 mqtt:connect(host[, port[, secure[, autoreconnect]]][, function(client)[, function(client, reason)]]) 參數
-
主機名, 域名或 IP (字元串)host
-
伺服器端口(數字), 預設1883port
-
0/1 表示secure
/false
, 預設 0. 詳情檢視文檔 net module.true
-
0/1表示autoreconnect
/false
,預設 0 (斷線是否重連)true
-
連接配接伺服器成功時的回調函數function(client)
-
連接配接伺服器失敗時的回調函數function(client, reason)
傳回 成功 true,失敗 false
連接配接失敗時的錯誤代碼
常量 | 值 | 描述 |
---|---|---|
| -5 | 未找到伺服器(或沒開端口) |
| -4 | 傳回消息回應不是Mqtt協定 |
| -3 | DNS錯誤 |
| -2 | 等待響應逾時 |
| -1 | 嘗試發送連接配接消息逾時 |
| 沒有錯誤. 注意: 這不會觸發錯誤回調函數. | |
| 1 | 伺服器MQTT版本不是3.1.1. |
| 2 | ClientID被伺服器拒絕. (檢視 ) |
| 3 | 服務不可用. |
| 4 | 使用者名或密碼錯誤. |
| 5 | 使用者名沒經過認證. |
4、mqtt.client:lwt()
建立遺囑(可選),伺服器将會發送一條 qos = 0, retain = 0, 内容為"offline"的消息到"/lwt"主題,如果沒收到用戶端發送的心跳資料包(掉線)。
文法 mqtt:lwt(topic, message[, qos[, retain]])
參數
-
将要發送遺囑消息的主題 (字元串)topic
-
遺囑消息, (緩存 或 字元串)message
-
消息品質, 預設 0qos
-
保留标志, 預設 0retain
傳回 空
5、mqtt.client:on()
為事件建立回調函數.
文法 mqtt:on(event, function(client[, topic[, message]]))
參數
-
可以是 "connect", "message" 或者 "offline"event
-
回調函數。第一個參數為client、如果事件為 "message",那麼第二個、第三個參數分别是主題和接收到的消息function(client[, topic[, message]])
傳回 空
6、mqtt.client:publish()
發送一條消息.
文法 mqtt:publish(topic, payload, qos, retain[, function(client)])
參數
-
要發送消息的主題 (字元串)topic
-
消息, (緩存 或 字元串)message
-
消息品質qos
-
消息保留标志retain
-
可選的回調函數。當消息發送成功(伺服器傳來PUBACK響應)則觸發該事件。注意:當多次調用publish() 函數,所有的發送指令将會調用最後定義的回調函數。function(client)
傳回 成功 true,失敗 false
7、mqtt.client:subscribe()
訂閱一個或多個主題. 文法
mqtt:subscribe(topic, qos[, function(client)]) 或
mqtt:subscribe(table[, function(client)])
參數
-
主題 (字元串)topic
-
訂閱的消息品質, 預設 0qos
-
要訂閱的'topic, qos'數組對table
-
訂閱成功時的可選回調函數.注意:當多次調用subscribe() 函數,所有的訂閱指令将會調用最後定義的回調函數。function(client)
傳回 成功 true,失敗 false
示例代碼
-- 訂閱/topic主題、服務品質為0
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
-- 或者訂閱多個主題 (topic/0, qos = 0; topic/1, qos = 1; topic2 , qos = 2)
m:subscribe({["topic/0"]=0,["topic/1"]=1,topic2=2}, function(conn) print("subscribe success") end)
8、mqtt.client:unsubscribe()
取消訂閱一個或多個主題. 文法
mqtt:unsubscribe(topic[, function(client)]) 或
mqtt:unsubscribe(table[, function(client)])
參數
-
主題 (字元串)topic
-
要取消訂閱的'topic, qos'數組對table
-
取消訂閱成功時的可選回調函數.注意:當多次調用unsubscribe() 函數,所有的取消訂閱指令将會調用最後定義的回調函數。function(client)
傳回 成功 true,失敗 false
示例代碼
-- 取消訂閱主題
m:unsubscribe("/topic", function(conn) print("unsubscribe success") end)
-- 或者取消訂閱多個主題 (topic/0; topic/1; topic2)
m:unsubscribe({["topic/0"]=0,["topic/1"]=0,topic2="anything"}, function(conn) print("unsubscribe success") end)
【轉載請注明出處:http://blog.csdn.net/leytton/article/details/72454287】
二、原文
摘自 https://nodemcu.readthedocs.io/en/master/en/modules/mqtt/
MQTT Module
Since | Origin / Contributor | Maintainer | Source |
---|---|---|---|
2015-01-23 | Stephen Robinson, Tuan PM | Vowstar | mqtt.c |
The client adheres to version 3.1.1 of the MQTT protocol. Make sure that your broker supports and is correctly configured for version 3.1.1. The client is backwards incompatible with brokers running MQTT 3.1.
mqtt.Client() | Creates a MQTT client. |
mqtt.client:close() | Closes connection to the broker. |
mqtt.client:connect() | Connects to the broker specified by the given host, port, and secure options. |
mqtt.client:lwt() | Setup Last Will and Testament (optional). |
mqtt.client:on() | Registers a callback function for an event. |
mqtt.client:publish() | Publishes a message. |
mqtt.client:subscribe() | Subscribes to one or several topics. |
mqtt.client:unsubscribe() | Unsubscribes from one or several topics. |
mqtt.Client()
Creates a MQTT client.
Syntax
mqtt.Client(clientid, keepalive[, username, password, cleansession])
Parameters
-
client IDclientid
-
keepalive secondskeepalive
-
user nameusername
-
user passwordpassword
-
0/1 forcleansession
/false
true
Returns
MQTT client
Example
-- init mqtt client without logins, keepalive timer 120s
m = mqtt.Client("clientid", )
-- init mqtt client with logins, keepalive timer 120sec
m = mqtt.Client("clientid", , "user", "password")
-- setup Last Will and Testament (optional)
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
-- to topic "/lwt" if client don't send keepalive packet
m:lwt("/lwt", "offline", , )
m:on("connect", function(client) print ("connected") end)
m:on("offline", function(client) print ("offline") end)
-- on publish message receive event
m:on("message", function(client, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
-- for TLS: m:connect("192.168.11.118", secure-port, 1)
m:connect("192.168.11.118", , , function(client) print("connected") end,
function(client, reason) print("failed reason: "..reason) end)
-- Calling subscribe/publish only makes sense once the connection
-- was successfully established. In a real-world application you want
-- move those into the 'connect' callback or make otherwise sure the
-- connection was established.
-- subscribe topic with qos = 0
m:subscribe("/topic",, function(client) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
m:publish("/topic","hello",,, function(client) print("sent") end)
m:close();
-- you can call m:connect again
MQTT Client
mqtt.client:close()
Closes connection to the broker.
Syntax
mqtt:close()
Parameters
none
Returns
true
on success,
false
otherwise
mqtt.client:connect()
Connects to the broker specified by the given host, port, and secure options.
Syntax
mqtt:connect(host[, port[, secure[, autoreconnect]]][, function(client)[, function(client, reason)]])
Parameters
-
host, domain or IP (string)host
-
broker port (number), default 1883port
-
0/1 forsecure
/false
, default 0. Take note of constraints documented in the net module.true
-
0/1 forautoreconnect
/false
, default 0true
-
callback function for when the connection was establishedfunction(client)
-
callback function for when the connection could not be establishedfunction(client, reason)
Returns
true
on success,
false
otherwise
Connection failure callback reason codes:
Constant | Value | Description |
---|---|---|
| -5 | There is no broker listening at the specified IP Address and Port |
| -4 | The response from the broker was not a CONNACK as required by the protocol |
| -3 | DNS Lookup failed |
| -2 | Timeout waiting for a CONNACK from the broker |
| -1 | Timeout trying to send the Connect message |
| No errors. Note: This will not trigger a failure callback. | |
| 1 | The broker is not a 3.1.1 MQTT broker. |
| 2 | The specified ClientID was rejected by the broker. (See ) |
| 3 | The server is unavailable. |
| 4 | The broker refused the specified username or password. |
| 5 | The username is not authorized. |
mqtt.client:lwt()
Setup Last Will and Testament (optional). A broker will publish a message with qos = 0, retain = 0, data = "offline" to topic "/lwt" if client does not send keepalive packet.
Syntax
mqtt:lwt(topic, message[, qos[, retain]])
Parameters
-
the topic to publish to (string)topic
-
the message to publish, (buffer or string)message
-
QoS level, default 0qos
-
retain flag, default 0retain
Returns
nil
mqtt.client:on()
Registers a callback function for an event.
Syntax
mqtt:on(event, function(client[, topic[, message]]))
Parameters
-
can be "connect", "message" or "offline"event
-
callback function. The first parameter is the client. If event is "message", the 2nd and 3rd param are received topic and message (strings).function(client[, topic[, message]])
Returns
nil
mqtt.client:publish()
Publishes a message.
Syntax
mqtt:publish(topic, payload, qos, retain[, function(client)])
Parameters
-
the topic to publish to (topic string)topic
-
the message to publish, (buffer or string)message
-
QoS levelqos
-
retain flagretain
-
optional callback fired when PUBACK received. NOTE: When calling publish() more than once, the last callback function defined will be called for ALL publish commands.function(client)
Returns
true
on success,
false
otherwise
mqtt.client:subscribe()
Subscribes to one or several topics.
Syntax
mqtt:subscribe(topic, qos[, function(client)])
mqtt:subscribe(table[, function(client)])
Parameters
-
a topic stringtopic
-
QoS subscription level, default 0qos
-
array of 'topic, qos' pairs to subscribe totable
-
optional callback fired when subscription(s) succeeded. NOTE: When calling subscribe() more than once, the last callback function defined will be called for ALL subscribe commands.function(client)
Returns
true
on success,
false
otherwise
Example
-- subscribe topic with qos = 0
m:subscribe("/topic",, function(conn) print("subscribe success") end)
-- or subscribe multiple topic (topic/0, qos = 0; topic/1, qos = 1; topic2 , qos = 2)
m:subscribe({["topic/0"]=,["topic/1"]=,topic2=}, function(conn) print("subscribe success") end)
mqtt.client:unsubscribe()
Unsubscribes from one or several topics.
Syntax
mqtt:unsubscribe(topic[, function(client)])
mqtt:unsubscribe(table[, function(client)])
Parameters
-
a topic stringtopic
-
array of 'topic, anything' pairs to unsubscribe fromtable
-
optional callback fired when unsubscription(s) succeeded. NOTE: When calling unsubscribe() more than once, the last callback function defined will be called for ALL unsubscribe commands.function(client)
Returns
true
on success,
false
otherwise
Example
-- unsubscribe topic
m:unsubscribe("/topic", function(conn) print("unsubscribe success") end)
-- or unsubscribe multiple topic (topic/0; topic/1; topic2)
m:unsubscribe({["topic/0"]=,["topic/1"]=,topic2="anything"}, function(conn) print("unsubscribe success") end)
轉載于:https://www.cnblogs.com/leytton/p/8253271.html