在 wifi 网络之间动态切换
dynamically switch between wifi networks
我家里有两个 WiFi 网络,我想使用我的 NodeMCU ESP8266 V1 从 world.To 中的任何地方通过网络远程控制多个继电器来完成这个我正在考虑测试 WiFi 连接,如果我在 1 分钟内没有获得 IP,请尝试其他网络,直到获得 IP。这是我在下面的代码中遵循的 API docs for tmr。
有没有办法使用 Lua 以编程方式在两个或多个 wifi 网络之间切换?我正在使用 Lua 语言,但是如果需要,我可以转到 arduino IDE。
wifi.setmode(wifi.STATION)
myRouter = "dlink"
tmr.alarm(1, 60000, tmr.ALARM_SINGLE, function()
if myRouter=="dlink" then
print("Dlink selected")
wifi.sta.config("dlink","password1")
wifi.sta.connect()
if wifi.sta.getip() == nil then
print("NO IP yet! ,Connecting...")
else
tmr.stop(1)
print("Connected, IP is "..wifi.sta.getip())
end
elseif myRouter=="cisco" then
print("Cisco selected")
wifi.sta.config("cisco","passoword2")
wifi.sta.connect()
if wifi.sta.getip() == nil then
print("NO IP yet! ,Connecting...")
else
tmr.stop(1)
print("Connected, IP is "..wifi.sta.getip())
end
else
print("No network is giving an ip")
end
end)
我正在寻找的是一个回调,它会在计时器 "tmr" 到期时触发。这样我就可以将变量更改为 myRouter="cisco"。请注意,在上面的代码中,我无法更改“myRouter”变量。
我考虑过使用 software watchdog 来始终监控连接性,因此如果或当 WiFi 在一个网络上掉线时,它将通过上面的代码 运行 触发重新连接。我不确定该怎么做,也不知道它通常是怎么做的,因为我对 lua 还很陌生。请建议或指出可以在这方面提供帮助的资源。谢谢大家。
这是一段未经测试的代码,很快就组合在一起了。
effectiveRouter = nil
counter = 0
wifi.sta.config("dlink", "password1")
tmr.alarm(1, 1000, tmr.ALARM_SEMI, function()
counter = counter + 1
if counter < 60 then
if wifi.sta.getip() == nil then
print("NO IP yet! Keep trying to connect to dlink")
tmr.start(1) -- restart
else
print("Connected to dlink, IP is "..wifi.sta.getip())
effectiveRouter = "dlink"
startProgram()
end
elseif counter < 120 then
wifi.sta.config("cisco", "password2")
if wifi.sta.getip() == nil then
print("NO IP yet! Keep trying to connect to cisco")
tmr.start(1) -- restart
else
print("Connected to cisco, IP is "..wifi.sta.getip())
effectiveRouter = "cisco"
startProgram()
end
else
print("Out of options, giving up.")
end
end)
它将首先尝试连接到 'dlink' 60 秒,然后再连接到 'cisco' 60 秒,如果两次尝试都不成功,最终将放弃。它使用 semi-automatic timer,只有在没有 IP 时才会重新启动。
我家里有两个 WiFi 网络,我想使用我的 NodeMCU ESP8266 V1 从 world.To 中的任何地方通过网络远程控制多个继电器来完成这个我正在考虑测试 WiFi 连接,如果我在 1 分钟内没有获得 IP,请尝试其他网络,直到获得 IP。这是我在下面的代码中遵循的 API docs for tmr。
有没有办法使用 Lua 以编程方式在两个或多个 wifi 网络之间切换?我正在使用 Lua 语言,但是如果需要,我可以转到 arduino IDE。
wifi.setmode(wifi.STATION)
myRouter = "dlink"
tmr.alarm(1, 60000, tmr.ALARM_SINGLE, function()
if myRouter=="dlink" then
print("Dlink selected")
wifi.sta.config("dlink","password1")
wifi.sta.connect()
if wifi.sta.getip() == nil then
print("NO IP yet! ,Connecting...")
else
tmr.stop(1)
print("Connected, IP is "..wifi.sta.getip())
end
elseif myRouter=="cisco" then
print("Cisco selected")
wifi.sta.config("cisco","passoword2")
wifi.sta.connect()
if wifi.sta.getip() == nil then
print("NO IP yet! ,Connecting...")
else
tmr.stop(1)
print("Connected, IP is "..wifi.sta.getip())
end
else
print("No network is giving an ip")
end
end)
我正在寻找的是一个回调,它会在计时器 "tmr" 到期时触发。这样我就可以将变量更改为 myRouter="cisco"。请注意,在上面的代码中,我无法更改“myRouter”变量。
我考虑过使用 software watchdog 来始终监控连接性,因此如果或当 WiFi 在一个网络上掉线时,它将通过上面的代码 运行 触发重新连接。我不确定该怎么做,也不知道它通常是怎么做的,因为我对 lua 还很陌生。请建议或指出可以在这方面提供帮助的资源。谢谢大家。
这是一段未经测试的代码,很快就组合在一起了。
effectiveRouter = nil
counter = 0
wifi.sta.config("dlink", "password1")
tmr.alarm(1, 1000, tmr.ALARM_SEMI, function()
counter = counter + 1
if counter < 60 then
if wifi.sta.getip() == nil then
print("NO IP yet! Keep trying to connect to dlink")
tmr.start(1) -- restart
else
print("Connected to dlink, IP is "..wifi.sta.getip())
effectiveRouter = "dlink"
startProgram()
end
elseif counter < 120 then
wifi.sta.config("cisco", "password2")
if wifi.sta.getip() == nil then
print("NO IP yet! Keep trying to connect to cisco")
tmr.start(1) -- restart
else
print("Connected to cisco, IP is "..wifi.sta.getip())
effectiveRouter = "cisco"
startProgram()
end
else
print("Out of options, giving up.")
end
end)
它将首先尝试连接到 'dlink' 60 秒,然后再连接到 'cisco' 60 秒,如果两次尝试都不成功,最终将放弃。它使用 semi-automatic timer,只有在没有 IP 时才会重新启动。