Faiyet
Faiyet

Reputation: 5471

NodeMCU WiFi auto connect

I am trying to resolve wifi connectivity using Lua language. I have been combing through the api to find a solution but nothing solid yet. I asked a previous question, dynamically switch between wifi networks and the answer did address the question in the way I asked it, but it didn't accomplish what I expected.

Basically, I have two different networks from two different providers. All I want the ESP8266 12e to do is detect when or if the current network has no internet access and automatically switch to the next network. It must continuously try to connect at say a 3 minute interval until it is successful and not just give up.

For testing purposes I tried this code below. The plan is to use the variable "effectiveRouter" and write some logic to switch based on the current router.

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)

When I run that code, I get effectiveRouter as nil on the console. This tells me that the print statement ran before the method call was complete, print(effectiveRouter). I am very very new to lua as this is my first time with the language. I am certain this boiler plate code must have been done before. Can someone please point me in the right direction? I am open to shifting to the arduino IDE as I already have it set up for the NodeMCU ESP8266. May be I can follow the logic better as I come from a java-OOP background.

Upvotes: 1

Views: 2478

Answers (2)

Marcel St&#246;r
Marcel St&#246;r

Reputation: 23535

I eventually sat down and tested my sketch from the previous answer. Two additional lines and we're good to go...

What I missed is that wifi.sta.config() resets the connection attempts if auto connect == true (which is the default). So, if you call it to connect to AP X while it's in the process of connecting to X it will start from scratch - and thus usually not get an IP before it's called again.

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)

Upvotes: 3

cagdas
cagdas

Reputation: 1644

You better to migrate a callback based architecture to be sure that you have successfully connected. Here is doc for it :

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

You can listen for

wifi.STA_GOTIP

And make your custom operations in it. Do not forget to start eventmon.

P.s. I am not able to see your variable effectiveRouter in related function.

Upvotes: 2

Related Questions