【问题标题】:NodeMCU WiFi auto connectNodeMCU WiFi 自动连接
【发布时间】:2017-02-15 07:01:16
【问题描述】:

我正在尝试使用 Lua 语言解决 wifi 连接问题。我一直在梳理the api 以找到解决方案,但还没有确定的解决方案。我问了一个先前的问题, dynamically switch between wifi networks,答案确实以我提出的方式解决了这个问题,但它并没有达到我的预期。

基本上,我有来自两个不同提供商的两个不同网络。我想要 ESP8266 12e 做的就是检测当前网络何时或是否无法访问互联网并自动切换到下一个网络。它必须以 3 分钟的间隔不断尝试连接,直到成功,而不仅仅是放弃。

出于测试目的,我在下面尝试了此代码。计划是使用变量“effectiveRouter”,并编写一些逻辑来根据当前路由器进行切换。

effectiveRouter = nil
function wifiConnect(id,pw)
    counter = 0
    wifi.sta.config(id,pw)
    tmr.alarm(1, 1000, tmr.ALARM_SEMI, function()  
    counter = counter + 1
        if counter < 10 then  
            if wifi.sta.getip() == nil then
              print("NO IP yet! Trying on "..id)
              tmr.start(1)
            else
                print("Connected, IP is "..wifi.sta.getip())

            end
        end     
    end)
end
wifiConnect("myNetwork","myPassword")
print(effectiveRouter)

当我运行该代码时,我在控制台上将 effectiveRouter 设为 nil。这告诉我 print 语句在方法调用完成之前运行,print(effectiveRouter)。我对 lua 非常陌生,因为这是我第一次使用该语言。我确信这个样板代码之前一定已经完成了。有人可以指出我正确的方向吗?我愿意转移到 arduino IDE,因为我已经为 NodeMCU ESP8266 设置了它。由于我来自 java-OOP 背景,也许我可以更好地遵循逻辑。

【问题讨论】:

  • effectiveRouter 从未在提供的代码中分配任何值。它怎么会变成零呢?您启动一个计时器,该计时器将在 1000 毫秒后第一次关闭。然后你立即打印 effectiveRouter ,此时当然是 nil 。第一次连接尝试将在您打印 EffectiveRouter 之后发生。但是无论你的定时器回调发生了什么,它都不会影响 EffectiveRouter 的值
  • @Piglet 感谢您指出,我的错误。上面的代码是我玩过的众多变体之一。在函数的 else 部分中,我有一个空行,我从中删除了分配“effectiveRouter=dlink”

标签: lua wifi nodemcu


【解决方案1】:

我最终坐下来,根据之前的答案测试了我的草图。再加两行,我们就可以开始了...

我错过的是wifi.sta.config() 重置连接尝试如果auto connect == true(这是默认设置)。因此,如果您在连接到 X 的过程中调用它来连接到 AP X,它将从头开始 - 因此在再次调用之前通常不会获得 IP。

effectiveRouter = nil
counter = 0
wifi.sta.config("dlink", "password1")
tmr.alarm(1, 1000, tmr.ALARM_SEMI, function()
  counter = counter + 1
  if counter < 30 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 == 30 then
    wifi.sta.config("cisco", "password2")
    -- there should also be tmr.start(1) in here as suggested in the comment
  elseif counter < 60 then
    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)

【讨论】:

  • 谢谢你的澄清,我会检查代码
  • 只是一个快速添加,在您更改配置的“elseif”内,我不得不调用计时器启动函数来继续循环。我在上面的代码中进行了更改。再次感谢,你这个摇滚人!
  • 无赖,你当然是对的。测试时我没有注意到,因为我的设备在大约 1 秒内连接到 AP。谢谢,我在上面的代码中添加了注释。
【解决方案2】:

您最好迁移基于回调的架构,以确保您已成功连接。这是它的文档:

https://nodemcu.readthedocs.io/en/master/en/modules/wifi/#wifistaeventmonreg

你可以听

wifi.STA_GOTIP

并在其中进行自定义操作。不要忘记启动 eventmon。

附:我无法在相关函数中看到您的变量 EffectiveRouter。

【讨论】:

  • 这并没有以任何方式解决真正的问题。 IMO 它没有资格获得答案。使用计时器或回调具有相同的效果。回调只是稍微更现代的方法。
  • 你是对的马塞尔。我只是想减少当前代码的复杂性并让用户注意相关变量未定义。所以追踪起来会更容易。感谢您的评论。
猜你喜欢
  • 2016-05-11
  • 2016-09-26
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
相关资源
最近更新 更多