我无法使用nodemcu连接到mqtt代理,直到DNS重试2次

时间:2017-02-11 11:25:12

标签: lua dns nodemcu

我无法连接到mqtt broker" hivemq"直到DNS重试2次。 问题是Dns在代码全部执行后重试,所以尽管它连接到代理,我仍然无法发布任何消息。 我试过设置DNS服务器,但它没有解决任何问题。 我在mac上使用ESPlorer ide。 感谢您的帮助,Luca

ssid = "my ssid"
pswd = "my pswd"

wifi.setmode(wifi.STATION)
print("connecting to wifi")


wifi.sta.config(ssid,pswd) 
tmr.delay(5000000)
print(wifi.sta.getip())


net.dns.setdnsserver("8.8.8.8", 0)--sets google first dns server--
net.dns.setdnsserver("8.8.8.8")  --equal to above
net.dns.setdnsserver("8.8.4.4", 1)  --sets google second dns server
--net.dns.setdnsserver("208.67.222.222", 0)--sets opendns first dns server
--net.dns.setdnsserver("208.67.222.222")  --equal to 
--net.dns.setdnsserver("208.67.220.220", 1) --sets opendns second dns server

--net.dns.getdnsserver(0) --gets first dns server
--net.dns.getdnsserver() --equal to above
--net.dns.getdnsserver(1)  --gets second dns server
net.dns.resolve("www.google.com", function (socket,ip)

if ip == nil then print("error: DNS fail!") else print(ip) end
end )

tmr.delay(5000000)



m=mqtt.Client("nodemcu", 1200, "username", "password")
print("hey")
connected = false



   m:connect("broker.hivemq.com", function(client) print("connected to broker") connected = true end, function(client,reason) print("reason:") print(reason) end)
   tmr.delay(5000000)

  if connected==true then print("connected is true") else print("connected is false") end


tmr.delay(10000000)

m:publish("/topic", "hello", 0, function(client) print("message published")end)

这是我从nodemcu开发工具包1.0获得的:



192.168.1.64	255.255.255.0	192.168.1.254
hey
connected is false
init.lua:47: not connected
stack traceback:
	[C]: in function 'publish'
	init.lua:47: in main chunk
	[C]: in function 'dofile'
	stdin:1: in main chunk
> DNS retry 1!
DNS retry 2!
connected to broker




0 个答案:

没有答案