The wifi.sta module connects if a loop is running?

前端 未结 2 1236
我寻月下人不归
我寻月下人不归 2021-01-27 15:12

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         


        
2条回答
  •  误落风尘
    2021-01-27 15:50

    If you use a recent dev firmware you can do something really event based :

    wifi.setmode(wifi.STATION)
    wifi.sta.config(SSID, PASSWORD)
    
    function Success()
        tmr.stop(0)
        if (SERIAL_PRINT) then
            print("IP: " .. wifi.sta.getip())
        end
        wifi.sta.eventMonStop()
        wifi.sta.eventMonReg(wifi.STA_GOTIP, "unreg")
        dofile("mainProgram.lua")
    end
    
    function Failure()
        if (SERIAL_PRINT) then
            print("Unable to connect")
        end
        wifi.sta.eventMonStop()
        wifi.sta.eventMonReg(wifi.STA_GOTIP, "unreg")
        return 0
    end
    
    tmr.alarm(0,30000,0, function() Failure() end)
    wifi.sta.connect()
    wifi.sta.eventMonReg(wifi.STA_GOTIP, function() Success() end)
    wifi.sta.eventMonStart()
    

    EDIT: Please have a look to the documentation for a list of all events. If you wish to use this code, you'll have to handle the failure more cleanly.

提交回复
热议问题