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

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

问题描述

我试图检测模块何时真正连接到我的 wifi AP,因为 .connect 没有回调,我正在做这样的简单事情:

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天全站免登陆