如果循环正在运行,wifi.sta模块会连接吗? [英] The wifi.sta module connects if a loop is running?

查看:48
本文介绍了如果循环正在运行,wifi.sta模块会连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检测该模块何时实际连接到我的wifi AP,因为.connect没有回调IM,它会执行以下简单操作:

Im trying to detect when the module actually connects to my wifi AP, since .connect does not have a callback im doing something simple like this:

wifi.sta.config("SSID","password")
wifi.sta.connect()
tmr.delay(1000000)
i = 0
while(wifi.sta.status() ~= 5 and i < 10) do
  print("Waiting")
  print(wifi.sta.status())
  i = i + 1
  tmr.delay(1000000) 
end

但是.sta.status()的输出在循环内始终为1.完成后,如果我从IDE手动发送= wifi.sta.status()命令,它会告诉我5.为什么?

But the output of .sta.status() is always 1 inside the loop. When it finish, if I send the command =wifi.sta.status() manually from the IDE it tells me 5. Why?

推荐答案

使用tmr.delay不允许运行事件循环,您应该使用计时器回调.

Using tmr.delay doesnot let run the event loop, you should use a timer callback.

然后,代码可能类似于:

Then the code could be something like :

wifi.sta.config("SSID","password")
wifi.sta.connect()

i=0
tmr.alarm(1, 1000, 1, function()
    if (wifi.sta.status() ~= 5 and i < 10) then
       print("Status:"..wifi.sta.status())
       i = i + 1
    else
       tmr.stop(1)
       if (wifi.sta.status() == 5) then
          print("IP:"..wifi.sta.getip())
       else
          print("Status:"..wifi.sta.status())
       end
    end
end)

这篇关于如果循环正在运行,wifi.sta模块会连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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