我是靠谱客的博主 现实麦片,这篇文章主要介绍NodeMCU--MQTT学习笔记(一),现在分享给大家,希望可以做个参考。

官方文档:https://nodemcu.readthedocs.io/en/master/en/modules/mqtt/

函数函数简介
mqtt.Client()创建一个MQTT客户端
mqtt.client:close()关闭与MQTT服务器的连接
mqtt.client:connect()连接到MQTT服务器并指定主机、端口和安全选项
mqtt.client:lwt()建立遗嘱(可选的)
mqtt.client:on()注册一个回调函数的事件
mqtt.client:publish()发布消息
mqtt.client:subscribe()订阅一个或多个主题
mqtt.client:unsubscribe()退订一个或几个主题

mqtt.Client()

语法:

复制代码
1
mqtt.Client(clientid, keepalive[, username, password, cleansession])

参数:

  • clientid client ID
  • keepalive keepalive seconds
  • username user name
  • password user password
  • cleansession 0/1 for false/true. Default is 1 (true).

mqtt.client:connect()

语法:

复制代码
1
2
mqtt:connect(host[, port[, secure[, autoreconnect]]][, function(client)[, function(client, reason)]])

参数:

  • host :主机、域名或者是IP地址(字符串)
  • port :MQTT服务器的端口号(数字),默认1883
  • secure 0/1 for false/true, default 0. Take note of constraints documented in the net module.
  • autoreconnect 0/1 for false/true, default 0. This option is deprecated.
  • function(client) callback function for when the connection was established
  • function(client, reason) callback function for when the connection could not be established. No further callbacks should be called.

mqtt.client:on()

语法:

复制代码
1
2
mqtt:on(event, function(client[, topic[, message]]))

参数:

  • event :注册的事件可以是”connect”、”message” 或者是”offline”
  • function(client[, topic[, message]]):回调函数。 第一个参数是客户端,如果注册的事件是“message”,那么第二个和第三个参数是得到的主题和消息(字符串)

首先一个简单的例子:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
client = mqtt.Client("NodeMCU_Client", 120) function handle_mqtt_error(client, reason) -- 定义mqtt错误处理函数 print("fail reason" .. reason) end function do_mqtt_connect(client) -- 定义连接上mqtt处理函数 print("connected") client:publish("nodemcu", "Hello World", 0, 0) end client:connect("www.liefyuan.top", do_mqtt_connect, handle_mqtt_error)

在这个基础上添加一个定时器,每10秒发送一个消息

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
client = mqtt.Client("NodeMCU_Client", 120) function handle_mqtt_error(client, reason) -- 定义mqtt错误处理函数 print("fail reason" .. reason) end function do_mqtt_connect(client) -- 定义连接上mqtt处理函数 print("connected") client:publish("nodemcu", "Hello World", 0, 0) end client:connect("www.liefyuan.top", do_mqtt_connect, handle_mqtt_error) function do_timer() -- 定义定时器处理函数 print("connected") client:publish("nodemcu", "hoho", 0, 0) end tmr.alarm(1, 10000, tmr.ALARM_AUTO, do_timer)

在上面的基础上增加一个subscribe功能:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
client = mqtt.Client("NodeMCU_Client", 120) function handle_mqtt_error(client, reason) -- 定义mqtt错误处理函数 print("fail reason" .. reason) end function do_mqtt_connect(client) -- 定义连接上mqtt处理函数 print("connected") client:publish("nodemcu", "Hello World", 0, 0) -- 推送一条消息 client:subscribe("led", 0, function(client) print("subscribe success") end) -- 订阅一个主题 end client:connect("www.liefyuan.top", do_mqtt_connect, handle_mqtt_error) function do_mqtt_message(client, topic, data) -- 定义一个订阅消息接收处理函数 print(topic .. ": ") if data ~= nil then print(data) end end client:on("message", do_mqtt_message) function do_timer() -- 定义定时器处理函数 print("connected") client:publish("nodemcu", "hoho", 0, 0) end tmr.alarm(1, 10000, tmr.ALARM_AUTO, do_timer)

增加:判断接收的数据,如果是1,则打开LED灯,否则熄灭LED灯

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
pin=0 -- 0就是D0 gpio.mode(pin, gpio.OUTPUT) client = mqtt.Client("NodeMCU_Client", 120) ------------------------------------------------- function handle_mqtt_error(client, reason) -- 定义mqtt错误处理函数 print("fail reason" .. reason) end function do_mqtt_connect(client) -- 定义连接上mqtt处理函数 print("connected") client:publish("nodemcu", "Hello World", 0, 0) -- 推送一条消息 client:subscribe("led", 0, function(client) print("subscribe success") end) -- 订阅一个主题 end client:connect("www.liefyuan.top", do_mqtt_connect, handle_mqtt_error) -------------------------------------------------------- function do_mqtt_message(client, topic, data) -- 定义一个订阅消息接收处理函数 print(topic .. ": ") if data ~= nil then if data == '1' then -- 如果接收的是字符'1' gpio.write(pin, gpio.HIGH) print(data) else gpio.write(pin, gpio.LOW) print(data) end end end client:on("message", do_mqtt_message) --------------------------------------------------------- function do_timer() -- 定义定时器处理函数 print("connected") client:publish("nodemcu", "hoho", 0, 0) end tmr.alarm(1, 10000, tmr.ALARM_AUTO, do_timer)

最后增加一个DHT11

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
led_pin = 0 -- led的引脚0就是D0 dht11_pin = 5 -- dht11的引脚5就是D5 gpio.mode(led_pin, gpio.OUTPUT) client = mqtt.Client("NodeMCU_Client", 120) ------------------------------------------------- function handle_mqtt_error(client, reason) -- 定义mqtt错误处理函数 print("fail reason" .. reason) end function do_mqtt_connect(client) -- 定义连接上mqtt处理函数 print("connected") client:publish("nodemcu", "Hello World", 0, 0) -- 推送一条消息 client:subscribe("led", 1, function(client) print("subscribe success") end) -- 订阅一个主题 end client:connect("www.liefyuan.top", do_mqtt_connect, handle_mqtt_error) -------------------------------------------------------- function do_mqtt_message(client, topic, data) -- 定义一个订阅消息接收处理函数 print(topic .. ": ") if data ~= nil then if data == '1' then -- 如果接收的是字符'1' gpio.write(led_pin, gpio.HIGH) print(data) else gpio.write(led_pin, gpio.LOW) print(data) end end end client:on("message", do_mqtt_message) --------------------------------------------------------- function do_timer() -- 定义定时器处理函数 print("connected") client:publish("nodemcu", "hoho", 0, 0) end tmr.alarm(1, 10000, tmr.ALARM_AUTO, do_timer) ----------------------------------------------------------- function do_timer_getdht11() -- 定义定时器处理函数 status, temp, humi, temp_dec, humi_dec = dht.read(dht11_pin) if status == dht.OK then -- Integer firmware using this example print(string.format("DHT Temperature:%d.%03d;Humidity:%d.%03drn", math.floor(temp), temp_dec, math.floor(humi), humi_dec )) -- Float firmware using this example --print("DHT Temperature:"..temp..";".."Humidity:"..humi) elseif status == dht.ERROR_CHECKSUM then print( "DHT Checksum error." ) elseif status == dht.ERROR_TIMEOUT then print( "DHT timed out." ) end end tmr.alarm(2, 1000, tmr.ALARM_AUTO, do_timer_getdht11) -----------------------------------------------------------

最后

以上就是现实麦片最近收集整理的关于NodeMCU--MQTT学习笔记(一)的全部内容,更多相关NodeMCU--MQTT学习笔记(一)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(96)

评论列表共有 0 条评论

立即
投稿
返回
顶部