无法从 ESP8266 连接到 MQTT 代理
Cannot connect to MQTT broker from ESP8266
我在我的 windows 机器上安装了 Mosquitto,它是一个 MQTT v3.1 代理(最近从 mosquitto.org 下载)。
我正在尝试从 ESP8266 连接到代理,但到目前为止我还无法连接到代理。我可以连接到 public 代理,但不能连接到我机器上安装的连接到同一 wifi 网络的代理。
我使用 build-nodemcu 服务构建了固件并使用了 master b运行ch。我认为它有 MQTT v3.1.1.
我遇到了这个 question,我想我 运行 也遇到过同样的情况。虽然给出了问题的原因,但没有提到如何解决这个问题。
你能建议一下如何解决这个问题吗?
更新 [13-09-2016]
这是我正在使用的代码:
sensorID = "sen_001"
tgtHost = "192.168.8.101"
tgtPort = "1883"
mqttUserID = "admin"
mqttPass = "word"
mqttTimeOut = 120
topicQueue = "/security"
WIFI_SSID = "Lakmal 4G"
WIFI_PASSWORD = "TF18BNY3M"
WIFI_SIGNAL_MODE = wifi.PHYMODE_N
ESP8266_IP=""
ESP8266_NETMASK=""
ESP8266_GATEWAY=""
if wifi.sta.getip() == nil then
wifi.setmode(wifi.STATION)
wifi.setphymode(WIFI_SIGNAL_MODE)
wifi.sta.config(WIFI_SSID, WIFI_PASSWORD)
wifi.sta.connect()
end
if ESP8266_IP ~= "" then
wifi.sta.setip({ip=ESP8266_IP,netmask=ESP8266_NETMASK,gateway=ESP8266_GATEWAY})
end
print("IP: " .. wifi.sta.getip())
m = mqtt.Client(sensorID, mqttTimeOut, mqttUserID, mqttPass, 1)
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 then
print(data)
end
end)
m:connect(tgtHost, tgtPort, 0, function(client) print ("connected") end,
function(client, reason) print("failed reason: "..reason) end)
我总是收到“失败原因:-5”错误。
我用 MQTTLens chrome 扩展测试了代理,代理运行良好。
抱歉,评论太长了。
NodeMCU MQTT docs and the SO question you referenced 都明确表示您需要 MQTT 3.1。1,3.1 是不够的。
Mosquitto 网站指出
Eclipse Mosquitto™ is an open source (EPL/EDL licensed) message broker
that implements the MQTT protocol versions 3.1 and 3.1.1.
然而,您声称您的经纪人是 3.1,根据上述说法,这听起来不是全部事实。
如果您使用更多详细信息更新您的问题,我将更新此答案。您既没有给出代码也没有给出错误消息。不过,您的 NodeMCU/Lua 代码可以与 public 经纪人一起正常工作这一事实对您的本地经纪人来说是一个非常有力的案例。
我在我的 windows 机器上安装了 Mosquitto,它是一个 MQTT v3.1 代理(最近从 mosquitto.org 下载)。
我正在尝试从 ESP8266 连接到代理,但到目前为止我还无法连接到代理。我可以连接到 public 代理,但不能连接到我机器上安装的连接到同一 wifi 网络的代理。
我使用 build-nodemcu 服务构建了固件并使用了 master b运行ch。我认为它有 MQTT v3.1.1.
我遇到了这个 question,我想我 运行 也遇到过同样的情况。虽然给出了问题的原因,但没有提到如何解决这个问题。
你能建议一下如何解决这个问题吗?
更新 [13-09-2016]
这是我正在使用的代码:
sensorID = "sen_001"
tgtHost = "192.168.8.101"
tgtPort = "1883"
mqttUserID = "admin"
mqttPass = "word"
mqttTimeOut = 120
topicQueue = "/security"
WIFI_SSID = "Lakmal 4G"
WIFI_PASSWORD = "TF18BNY3M"
WIFI_SIGNAL_MODE = wifi.PHYMODE_N
ESP8266_IP=""
ESP8266_NETMASK=""
ESP8266_GATEWAY=""
if wifi.sta.getip() == nil then
wifi.setmode(wifi.STATION)
wifi.setphymode(WIFI_SIGNAL_MODE)
wifi.sta.config(WIFI_SSID, WIFI_PASSWORD)
wifi.sta.connect()
end
if ESP8266_IP ~= "" then
wifi.sta.setip({ip=ESP8266_IP,netmask=ESP8266_NETMASK,gateway=ESP8266_GATEWAY})
end
print("IP: " .. wifi.sta.getip())
m = mqtt.Client(sensorID, mqttTimeOut, mqttUserID, mqttPass, 1)
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 then
print(data)
end
end)
m:connect(tgtHost, tgtPort, 0, function(client) print ("connected") end,
function(client, reason) print("failed reason: "..reason) end)
我总是收到“失败原因:-5”错误。
我用 MQTTLens chrome 扩展测试了代理,代理运行良好。
抱歉,评论太长了。
NodeMCU MQTT docs and the SO question you referenced 都明确表示您需要 MQTT 3.1。1,3.1 是不够的。
Mosquitto 网站指出
Eclipse Mosquitto™ is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1.
然而,您声称您的经纪人是 3.1,根据上述说法,这听起来不是全部事实。
如果您使用更多详细信息更新您的问题,我将更新此答案。您既没有给出代码也没有给出错误消息。不过,您的 NodeMCU/Lua 代码可以与 public 经纪人一起正常工作这一事实对您的本地经纪人来说是一个非常有力的案例。