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()
语法:
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()
语法:
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()
语法:
mqtt:on(event, function(client[, topic[, message]]))

参数:
  • event :注册的事件可以是”connect”、”message” 或者是”offline”
  • function(client[, topic[, message]]):回调函数。 第一个参数是客户端,如果注册的事件是“message”,那么第二个和第三个参数是得到的主题和消息(字符串)
首先一个简单的例子:
client = mqtt.Client("NodeMCU_Client", 120)function handle_mqtt_error(client, reason) -- 定义mqtt错误处理函数 print("fail reason" .. reason) endfunction do_mqtt_connect(client)-- 定义连接上mqtt处理函数 print("connected") client:publish("nodemcu", "Hello World", 0, 0) endclient:connect("www.liefyuan.top", do_mqtt_connect, handle_mqtt_error)

在这个基础上添加一个定时器,每10秒发送一个消息
client = mqtt.Client("NodeMCU_Client", 120)function handle_mqtt_error(client, reason) -- 定义mqtt错误处理函数 print("fail reason" .. reason) endfunction do_mqtt_connect(client)-- 定义连接上mqtt处理函数 print("connected") client:publish("nodemcu", "Hello World", 0, 0) endclient: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功能:
client = mqtt.Client("NodeMCU_Client", 120)function handle_mqtt_error(client, reason) -- 定义mqtt错误处理函数 print("fail reason" .. reason) endfunction do_mqtt_connect(client)-- 定义连接上mqtt处理函数 print("connected") client:publish("nodemcu", "Hello World", 0, 0)-- 推送一条消息 client:subscribe("led", 0, function(client) print("subscribe success") end)-- 订阅一个主题 endclient: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 endclient: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灯
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) endfunction do_mqtt_connect(client)-- 定义连接上mqtt处理函数 print("connected") client:publish("nodemcu", "Hello World", 0, 0)-- 推送一条消息 client:subscribe("led", 0, function(client) print("subscribe success") end)-- 订阅一个主题 endclient: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 =https://www.it610.com/article/='1' then-- 如果接收的是字符'1' gpio.write(pin, gpio.HIGH) print(data) else gpio.write(pin, gpio.LOW) print(data) endend endclient: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)

【NodeMCU--MQTT学习笔记(一)】最后增加一个DHT11
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) endfunction do_mqtt_connect(client)-- 定义连接上mqtt处理函数 print("connected") client:publish("nodemcu", "Hello World", 0, 0)-- 推送一条消息 client:subscribe("led", 1, function(client) print("subscribe success") end)-- 订阅一个主题 endclient: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 =https://www.it610.com/article/='1' then-- 如果接收的是字符'1' gpio.write(led_pin, gpio.HIGH) print(data) else gpio.write(led_pin, gpio.LOW) print(data) endend endclient: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.%03d\r\n", 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)-----------------------------------------------------------

    推荐阅读