Lua NodeMCU WiFi自动连接

Lua NodeMCU WiFi自动连接,lua,wifi,nodemcu,Lua,Wifi,Nodemcu,我正在尝试使用Lua语言解决wifi连接问题。我一直在仔细研究,想找到一个解决办法,但还没有找到切实可行的办法。我问了之前的一个问题,答案确实按照我的提问方式解决了这个问题,但没有达到我的预期 基本上,我有两个不同的网络从两个不同的供应商。我想要ESP8266 12e做的就是检测当前网络何时或是否没有互联网接入,并自动切换到下一个网络。它必须不断尝试以3分钟的间隔连接,直到成功,而不是放弃 出于测试目的,我尝试了下面的代码。计划是使用变量“effectiveverouter”并根据当前路由器编写

我正在尝试使用Lua语言解决wifi连接问题。我一直在仔细研究,想找到一个解决办法,但还没有找到切实可行的办法。我问了之前的一个问题,答案确实按照我的提问方式解决了这个问题,但没有达到我的预期

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

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

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)
effectiveverouter=nil
功能接线(id,pw)
计数器=0
wifi.sta.config(id,pw)
tmr.警报(1,1000,tmr.警报,函数()
计数器=计数器+1
如果计数器<10,则
如果wifi.sta.getip()==nil,则
打印(“还没有IP!正在尝试”。.id)
tmr.启动(1)
其他的
打印(“已连接,IP为”。.wifi.sta.getip())
终止
终止
(完)
终止
无线连接(“我的网络”、“我的密码”)
打印(外部有效)

当我运行该代码时,我在控制台上得到effectiveouter作为nil。这告诉我print语句在方法调用完成之前运行,
print(effectiveverouter)
。我对lua非常陌生,因为这是我第一次接触这种语言。我肯定这个锅炉铭牌代码以前一定做过。有人能给我指一下正确的方向吗?我愿意切换到arduino IDE,因为我已经为NodeMCU ESP8266设置了它。也许我可以更好地遵循逻辑,因为我来自JavaOOP背景

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

你可以倾听我的声音

wifi.STA_GOTIP

并在其中进行自定义操作。别忘了启动eventmon


另外,我无法在相关函数中看到您的变量effectiveRouter。

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

你可以倾听我的声音

wifi.STA_GOTIP

并在其中进行自定义操作。别忘了启动eventmon


另外,我无法在相关函数中看到您的变量effectiveOuter。

我最终坐下来测试了前面答案中的草图。另外两条线,我们可以走了

我错过的是,如果
自动连接==true
(这是默认设置),则重置连接尝试。因此,如果您在AP X连接到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)
effectiveverouter=nil
计数器=0
wifi.sta.config(“dlink”、“password1”)
tmr.警报(1,1000,tmr.警报,函数()
计数器=计数器+1
如果计数器<30,则
如果wifi.sta.getip()==nil,则
打印(“还没有IP!继续尝试连接到dlink”)
tmr.start(1)——重新启动
其他的
打印(“连接到dlink,IP为“.wifi.sta.getip())
effectiveRouter=“dlink”
--startProgram()
终止
elseif计数器==30然后
wifi.sta.config(“思科”、“密码2”)
--正如评论中所建议的,这里还应该有tmr.start(1)
elseif计数器<60则
如果wifi.sta.getip()==nil,则
打印(“还没有IP!继续尝试连接到cisco”)
tmr.start(1)——重新启动
其他的
打印(“连接到cisco,IP为“.wifi.sta.getip())
effectiveRouter=“cisco”
--startProgram()
终止
其他的
打印(“没有选择,放弃”)
终止
(完)

我最终坐下来,测试了前面答案中的草图。另外两条线,我们可以走了

我错过的是,如果
自动连接==true
(这是默认设置),则重置连接尝试。因此,如果您在AP X连接到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)
effectiveverouter=nil
计数器=0
wifi.sta.config(“dlink”、“password1”)
tmr.警报(1,1000,tmr.警报,函数()
计数器=计数器+1
如果计数器<30,则
如果wifi.sta.getip()==nil,则
打印(“还没有IP!继续尝试连接到dlink”)
tmr.start(1)——重新启动
其他的
打印(“连接到dlink,IP为“.wifi.sta.getip())
effectiveRouter=“dlink”
--startProgram()
终止
elseif计数器==30然后
wifi.sta.config(“思科”、“密码2”)
--正如评论中所建议的,这里还应该有tmr.start(1)
elseif计数器<60则
如果wifi.sta.getip()==nil,则
打印(“还没有IP!继续尝试连接到cisco”)
tmr.start(1)——重新启动
其他的
打印(“连接到cisco,IP为“.wifi.sta.getip())
effectiveRouter=“cisco”
--startProgram()
终止
其他的
打印(“没有选择,放弃”)
终止
(完)

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