在wifi网络之间动态切换 [英] dynamically switch between wifi networks

查看:129
本文介绍了在wifi网络之间动态切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我家里有两个WiFi网络,我想使用我的 NodeMCU ESP8266 V1 在世界各地通过Web远程控制多个中继.为此,我正在考虑测试WiFi连接,如果我在一分钟内没有获得IP,请尝试其他网络,直到获得IP.这是 tmr的API文档,我在下面的代码.

I have two WiFi networks at home where I want to use my NodeMCU ESP8266 V1 to control several relays remotely over the web from anywhere in the world.To accomplish this I was thinking to test for WiFi connectivity and if I don't get an IP within 1-minute try the other network until I get an IP. Here is the API docs for tmr which I followed in the code below.

有没有一种方法可以使用Lua以编程方式在两个或多个wifi网络之间切换?我使用的是Lua语言,但是如果需要,我可以转到arduino IDE.

Is there a way to switch between two or more wifi networks programatically using Lua? I am using the Lua language, however I can move to arduino IDE, if required.

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 "变量.

What I am looking for is a callback which fires whenever the timer "tmr" expires. This way I can change the variable to myRouter="cisco". Notice in the code above i was unable to change the "myRouter" variable.

我考虑过使用软件看门狗来监视始终保持连接性,因此当WiFi掉落在一个网络上时,它将通过运行上面的代码来触发重新连接.我不确定如何执行此操作或通常如何执行此操作,因为我对lua非常陌生.请给我建议或指向可以在这方面有所帮助的资源.谢谢大家.

I considered using a software watchdog to monitor the conectivity all the time so if or when the WiFi drops on one network, it will trigger a reconnect by running the code above. I am not sure how to do this or how its usually done, since I am very new to lua. Please advise or point me to a resource which can help in this regard. Thanks guys.

推荐答案

这是未经测试的快速代码段.

This is an untested piece of code quickly put together.

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'60s,然后再连接到'cisco'60s,如果这两次尝试均未成功,则最终将放弃.它使用半自动计时器,只有在以下情况下才会重新启动还没有IP.

It'll first try to connect to 'dlink' for 60s, then to 'cisco' for another 60s, and will eventually give up after that if neither attempts was successful. It uses a semi-automatic timer which is only restarted if there's no IP yet.

这篇关于在wifi网络之间动态切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆