Lua MQTT 发布不适用于脚本,但适用于 ESP8266 上的终端
Lua MQTT publish doesn't work from script but works from terminal on ESP8266
我正在尝试从基于 ESP8266 的 Geekcreit 开发板构建一个小型 MQTT 客户端。
当我 运行 使用 PuTTY 通过串行连接发出命令时,它一切正常并成功发布消息,被代理 运行ning 在 Raspberry Pi 上接收。
我正在尝试将其添加到 Lua 脚本,而不是通过 init.lua 添加到 运行,并且当连接回调触发时没有发布。
--test.lua
print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("ap","pass")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
-- initiate the mqtt client and set keepalive timer to 120sec
m = mqtt.Client("myNodeName", 120, "", "") -- blank user and password
m:on("connect", function() print("connected") end )
m:connect("*.*.*.*") -- my local broker's IP
m:publish("topic/test", "7.2", 0, 0) -- no QoS, not retained
end
end)
我正在使用 Esplorer 上传和 运行 安装该脚本,正如我所说,我成功地看到了 'connected' 消息,但没有消息到达代理。
如果我选择
m:publish("topic/test", "7.2", 0, 0) -- no QoS, not retained
并从 Esplorer 的 "Send" 命令栏启动它,代理收到消息。
我有点不知所措。感谢任何帮助。
与 NodeMCU 中的许多其他功能一样 API mqtt.client:connect()
是异步的,即它不会阻塞。您只能在成功建立连接后发布。 mqtt.client:connect()
.
中有一个回调函数
您可以使用本地 mqtt_connected
标志,在回调中设置它(或 m:on("connect")
)并在计时器中等待直到连接或直接从回调中发布。
m:connect("192.168.11.118", 1883, 0, function(client)
// now publish through client
end)
我正在尝试从基于 ESP8266 的 Geekcreit 开发板构建一个小型 MQTT 客户端。
当我 运行 使用 PuTTY 通过串行连接发出命令时,它一切正常并成功发布消息,被代理 运行ning 在 Raspberry Pi 上接收。
我正在尝试将其添加到 Lua 脚本,而不是通过 init.lua 添加到 运行,并且当连接回调触发时没有发布。
--test.lua
print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("ap","pass")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
-- initiate the mqtt client and set keepalive timer to 120sec
m = mqtt.Client("myNodeName", 120, "", "") -- blank user and password
m:on("connect", function() print("connected") end )
m:connect("*.*.*.*") -- my local broker's IP
m:publish("topic/test", "7.2", 0, 0) -- no QoS, not retained
end
end)
我正在使用 Esplorer 上传和 运行 安装该脚本,正如我所说,我成功地看到了 'connected' 消息,但没有消息到达代理。
如果我选择
m:publish("topic/test", "7.2", 0, 0) -- no QoS, not retained
并从 Esplorer 的 "Send" 命令栏启动它,代理收到消息。
我有点不知所措。感谢任何帮助。
与 NodeMCU 中的许多其他功能一样 API mqtt.client:connect()
是异步的,即它不会阻塞。您只能在成功建立连接后发布。 mqtt.client:connect()
.
您可以使用本地 mqtt_connected
标志,在回调中设置它(或 m:on("connect")
)并在计时器中等待直到连接或直接从回调中发布。
m:connect("192.168.11.118", 1883, 0, function(client)
// now publish through client
end)