NodeMCU文档中文翻译 6 MQTT模块
【本文出处:http://blog.csdn.net/leytton/article/details/72454287】
一、译文
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])
参数
clientid
客户端idkeepalive
心跳秒数username
用户名password
密码cleansession
清除session 0/1 表示否
/是
返回
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 thenprint(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()
host
主机名, 域名或 IP (字符串)port
服务器端口(数字), 默认1883secure
0/1 表示false
/true
, 默认 0. 详情查看文档 net module.autoreconnect
0/1表示false
/true
,默认 0 (断线是否重连)function(client)
连接服务器成功时的回调函数function(client, reason)
连接服务器失败时的回调函数
常量 | 值 | 描述 |
---|---|---|
mqtt.CONN_FAIL_SERVER_NOT_FOUND
|
-5 | 未找到服务器(或没开端口) |
mqtt.CONN_FAIL_NOT_A_CONNACK_MSG
|
-4 | 返回消息回应不是Mqtt协议 |
mqtt.CONN_FAIL_DNS
|
-3 | DNS错误 |
mqtt.CONN_FAIL_TIMEOUT_RECEIVING
|
-2 | 等待响应超时 |
mqtt.CONN_FAIL_TIMEOUT_SENDING
|
-1 | 尝试发送连接消息超时 |
mqtt.CONNACK_ACCEPTED
|
0 | 没有错误. 注意: 这不会触发错误回调函数. |
mqtt.CONNACK_REFUSED_PROTOCOL_VER
|
1 | 服务器MQTT版本不是3.1.1. |
mqtt.CONNACK_REFUSED_ID_REJECTED
|
2 |
ClientID被服务器拒绝. (查看mqtt.Client() )
|
mqtt.CONNACK_REFUSED_SERVER_UNAVAILABLE
|
3 | 服务不可用. |
mqtt.CONNACK_REFUSED_BAD_USER_OR_PASS
|
4 | 用户名或密码错误. |
mqtt.CONNACK_REFUSED_NOT_AUTHORIZED
|
5 | 用户名没经过认证. |
4、mqtt.client:lwt()
创建遗嘱(可选),服务器将会发送一条 qos = 0, retain = 0, 内容为"offline"的消息到"/lwt"主题,如果没收到客户端发送的心跳数据包(掉线)。
topic
将要发送遗嘱消息的主题 (字符串)message
遗嘱消息, (缓存 或 字符串)qos
消息质量, 默认 0retain
保留标志, 默认 0
返回 空
5、mqtt.client:on()
event
可以是 "connect", "message" 或者 "offline"function(client[, topic[, message]])
回调函数。第一个参数为client、如果事件为 "message",那么第二个、第三个参数分别是主题和接收到的消息
6、mqtt.client:publish()
topic
要发送消息的主题 (字符串)message
消息, (缓存 或 字符串)qos
消息质量retain
消息保留标志function(client)
可选的回调函数。当消息发送成功(服务器传来PUBACK响应)则触发该事件。注意:当多次调用publish() 函数,所有的发送指令将会调用最后定义的回调函数。
成功true,失败false
7、mqtt.client:subscribe()
订阅一个或多个主题.
mqtt:subscribe(topic, qos[, function(client)]) 或
mqtt:subscribe(table[, function(client)])
topic
主题 (字符串)qos
订阅的消息质量, 默认 0table
要订阅的'topic, qos'数组对function(client)
订阅成功时的可选回调函数.注意:当多次调用subscribe() 函数,所有的订阅指令将会调用最后定义的回调函数。
成功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
主题 (字符串)table
要取消订阅的'topic, qos'数组对function(client)
取消订阅成功时的可选回调函数.注意:当多次调用unsubscribe() 函数,所有的取消订阅指令将会调用最后定义的回调函数。
成功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】
二、原文
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
clientid
client IDkeepalive
keepalive secondsusername
user namepassword
user passwordcleansession
0/1 forfalse
/true
Returns
MQTT client
Example
-- init mqtt client without logins, keepalive timer 120s
m = mqtt.Client("clientid", 120)-- init mqtt client with logins, keepalive timer 120sec
m = mqtt.Client("clientid", 120, "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", 0, 0)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 thenprint(data)end
end)-- for 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)-- 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",0, function(client) print("subscribe success") end)
-- publish a message with data = hello, QoS = 0, retain = 0
m:publish("/topic","hello",0,0, 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
host, domain or IP (string)port
broker port (number), default 1883secure
0/1 forfalse
/true
, default 0. Take note of constraints documented in the net module.autoreconnect
0/1 forfalse
/true
, default 0function(client)
callback function for when the connection was establishedfunction(client, reason)
callback function for when the connection could not be established
Returns
true
on success, false
otherwise
Connection failure callback reason codes:
Constant | Value | Description |
---|---|---|
mqtt.CONN_FAIL_SERVER_NOT_FOUND
|
-5 | There is no broker listening at the specified IP Address and Port |
mqtt.CONN_FAIL_NOT_A_CONNACK_MSG
|
-4 | The response from the broker was not a CONNACK as required by the protocol |
mqtt.CONN_FAIL_DNS
|
-3 | DNS Lookup failed |
mqtt.CONN_FAIL_TIMEOUT_RECEIVING
|
-2 | Timeout waiting for a CONNACK from the broker |
mqtt.CONN_FAIL_TIMEOUT_SENDING
|
-1 | Timeout trying to send the Connect message |
mqtt.CONNACK_ACCEPTED
|
0 | No errors. Note: This will not trigger a failure callback. |
mqtt.CONNACK_REFUSED_PROTOCOL_VER
|
1 | The broker is not a 3.1.1 MQTT broker. |
mqtt.CONNACK_REFUSED_ID_REJECTED
|
2 |
The specified ClientID was rejected by the broker. (Seemqtt.Client() )
|
mqtt.CONNACK_REFUSED_SERVER_UNAVAILABLE
|
3 | The server is unavailable. |
mqtt.CONNACK_REFUSED_BAD_USER_OR_PASS
|
4 | The broker refused the specified username or password. |
mqtt.CONNACK_REFUSED_NOT_AUTHORIZED
|
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
topic
the topic to publish to (string)message
the message to publish, (buffer or string)qos
QoS level, default 0retain
retain flag, default 0
Returns
nil
mqtt.client:on()
Registers a callback function for an event.
Syntax
mqtt:on(event, function(client[, topic[, message]]))
Parameters
event
can be "connect", "message" or "offline"function(client[, topic[, message]])
callback function. The first parameter is the client. If event is "message", the 2nd and 3rd param are received topic and message (strings).
Returns
nil
mqtt.client:publish()
Publishes a message.
Syntax
mqtt:publish(topic, payload, qos, retain[, function(client)])
Parameters
topic
the topic to publish to (topic string)message
the message to publish, (buffer or string)qos
QoS levelretain
retain flagfunction(client)
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.
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
topic
a topic stringqos
QoS subscription level, default 0table
array of 'topic, qos' pairs to subscribe tofunction(client)
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.
Returns
true
on success, false
otherwise
Example
-- subscribe topic with qos = 0
m:subscribe("/topic",0, 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"]=0,["topic/1"]=1,topic2=2}, 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
topic
a topic stringtable
array of 'topic, anything' pairs to unsubscribe fromfunction(client)
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.
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"]=0,["topic/1"]=0,topic2="anything"}, function(conn) print("unsubscribe success") end)
NodeMCU文档中文翻译 6 MQTT模块相关推荐
- NodeMCU文档中文翻译 7 DHT温湿度传感器模块
[转载请注明出处:http://blog.csdn.net/leytton/article/details/76178661] 一.序言 DHT11 温湿度模块传感器 资料下载:http://pan. ...
- NodeMCU文档中文翻译 4 烧写固件
一.译文 Adafruit 提供了非常好的固件烧写教程 .下面你会发现两个流行的基本工具esptool和NodeMCU Flasher. 注意:记住ESP8266在烧写新固件前需要放入烧写模块. 1. ...
- NodeMCU文档中文翻译 5 上传代码
一.译文 与烧写一样,这里有好几种方法来从计算机上传代码到设备当中. 注意NodeMCU串口接口在启动时使用115200波特率,为了在启动后改变速率,可以使用uart.setup(0,9600,8,0 ...
- ReactiveX文档中文翻译
ReactiveX/RxJava文档中文版 项目地址:https://github.com/mcxiaoke/RxDocs,欢迎Star和帮忙改进. 有任何意见或建议,到这里提出 Create New ...
- Pushy入门文档中文翻译
本文为博主原创,允许转载,但请声明原文地址:http://www.coselding.cn/article/2016/12/01/Pushy入门文档中文翻译/ pushy 这是我自己的翻译版本,原文地 ...
- Word文档中文翻译成英文的方法
将Word文档在线翻译,有的时候我们在一些文档中放一些重要的内容,将这些内容放在文档中,然后通过在浏览器上进行搜索进入到在线翻译中,下面就让小编给大家简单介绍一下. 步骤一:首先我们需要在浏览器上进行 ...
- axios 文档中文翻译
axios中文文档(完全中文翻译) 由于使用网上一些翻译文档时发现,内容多被广告隔离成小块.用起来有点不方便同时夹杂部分英文.于是索性把 axios 自己翻译了一遍,贡献给大家便查. 原文档地址-- ...
- Next.js v4.1.4 文档中文翻译
最近想稍稍看下 React的 SSR框架 Next.js,因为不想看二手资料, 所以自己跑到 Github上看,Next.js的文档是英文的,看倒是大概也能看得懂, 但有些地方不太确定,而且英文看着毕 ...
- RFC文档(中文翻译版本)
RFC文档官方在线阅读地址:https://tools.ietf.org/rfc/index 以下是部分中文翻译的文档连接 RFC文档目录 RFC1 主机软件 RFC2 主机软件 RFC3 文档规范 ...
- Surround360 README文档——中文翻译
该文档位于Surround360项目根目录下 翻译正文: #Surround360 系统 Surround360是用于捕获和渲染3D(立体声)360视频和照片的硬件和软件系统,适合在VR中浏览.我们将 ...
最新文章
- JPA /休眠刷新策略初学者指南
- 与大家分享一下2010我的找工作历程!真累啊!不过都已经结束了!
- 20155222 卢梓杰 myod
- 字符串,字典,元祖,列表
- 1970年代宇航员在月球上生活,如何实现电力供应
- win pe备份linux,Windows10操作系统如何使用微PE实现备份与恢复
- C# 客户端PDF文件打印方法大全
- 华为培训中华为数通HCIE考试流程-ielab
- SpringBoot的yml配置文件(三)
- VBAProject调用mysql出错_VBA代码调用Access数据库系统压缩和修复数据库功能
- 刑事案件的构成要素 zt
- 小程序 侧边栏(导航)滑动
- 英语发音之音标4---长元音法()
- 鲸探发布点评:8月19日发售《小窗白云与凿山骨》数字藏品
- 电子招标采购系统源码功能清单
- 小红书竞品分析_App竞品分析报告:小红书VS洋码头
- java中的while和do while循环语句
- vue集成海康h5player实现播放
- 太吾绘卷第一世攻略_太吾绘卷第一世无修改通关7剑冢攻略 太吾绘卷怎么通关剑冢...
- JAVA综合练习-动物乐园