这是我的lua脚本,在带有nodemcu固件的esp8266上运行,我保留一个函数check_wifi()来检查我的esp模块是否已连接,然后我正在发布主题" on" ,并为" connect"设置两个事件处理程序。和#34; message",但是我的处理程序
function check_wifi()
local ip = wifi.sta.getip()
if(ip==nil) then
print("Connecting...")
else
tmr.stop(0)
print("Connected to AP!")
print(ip)
m:connect("test.mosquitto.org", 1883, 0, function(conn) print("connected") end)
m:publish("on","1",0,0,0)
m:on("connect",function(m)
m:subscribe("on",0,function(m) print(m);print("subcribed") end)
end )
m:on("message", function(conn, topic, data)
print("in on")
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
end
end
local SSID = "Crystalrock2"
local SSID_PASSWORD = "nest2abc123"
gpio.mode(4,gpio.OUTPUT)
gpio.write(4,gpio.LOW)
-- configure ESP as a station
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,SSID_PASSWORD)
wifi.sta.connect()
m = mqtt.Client("dee15", 120, "test", "password")
m:on("message", function(conn, topic, data)
print("in on")
print(topic .. ":" )
if data ~= nil then
print(data)
end
end)
m:on("connect",function(m)
m:subscribe("on",0,function(m) print(m);print("subcribed") end)
end )
m:connect("test.mosquitto.org", 1883, 0, function(conn) print("connected") end)
tmr.alarm(0,2000,1,check_wifi)
这是我得到的: 输出:连接... 连接... 连接到AP! 192.168.0.100 userdata:3fff8678 订阅频道
为什么m:on(" message" ..)未触发并打印消息