0
votes

I have two WiFi networks at home where I want to use my NodeMCU ESP8266 V1 to control several relays remotely over the web from anywhere in the world.To accomplish this I was thinking to test for WiFi connectivity and if I don't get an IP within 1-minute try the other network until I get an IP. Here is the API docs for tmr which I followed in the code below.

Is there a way to switch between two or more wifi networks programatically using Lua? I am using the Lua language, however I can move to arduino IDE, if required.

wifi.setmode(wifi.STATION)
myRouter = "dlink"
tmr.alarm(1, 60000, tmr.ALARM_SINGLE, function()
      if myRouter=="dlink" then
        print("Dlink selected")
        wifi.sta.config("dlink","password1")
        wifi.sta.connect()  
             if wifi.sta.getip() == nil then
                 print("NO IP yet! ,Connecting...")
             else
                 tmr.stop(1)
                 print("Connected, IP is "..wifi.sta.getip())
             end           
      elseif myRouter=="cisco" then
        print("Cisco selected")
        wifi.sta.config("cisco","passoword2")
        wifi.sta.connect()  
             if wifi.sta.getip() == nil then
                 print("NO IP yet! ,Connecting...")
             else
                 tmr.stop(1)
                 print("Connected, IP is "..wifi.sta.getip())
             end
      else
         print("No network is giving an ip")            
      end            
end)

What I am looking for is a callback which fires whenever the timer "tmr" expires. This way I can change the variable to myRouter="cisco". Notice in the code above i was unable to change the "myRouter" variable.

I considered using a software watchdog to monitor the conectivity all the time so if or when the WiFi drops on one network, it will trigger a reconnect by running the code above. I am not sure how to do this or how its usually done, since I am very new to lua. Please advise or point me to a resource which can help in this regard. Thanks guys.

1
you can change the value of myRouter at any time, everywhere in the code. its a global variable. at least in the code you provided. your code will try to connect to a network after 1 minute. if you succeed you stop (you have a single shot timer, no need to stop it), if you don't succeed you print "NO IP yet! ,Connecting..." and then your program is done. why don't you restart your timer if you cannot connect? Please read the tmr documentation again. I think you did not get what it does. - Piglet
Thank you for the quick reply, I understand its a global variable and I can change it at any time. The question is when do I change it. Is there a function that is called before the timer restarts automatically where I can update the variable and then jump back into the timer code? That is, assuming no connection was made. I will look over the documentation. - Faiyet
you should change it in the timer callback. in your implementation the timer will not restart at all as you initialized a single shot timer (tmr.ALARM_SINGLE). it will execute your code once after the 60000ms have expired. checkout auto and semiauto timers. try to connect, start the timer, when it expires (after 1 min), check if you were successful, if not change the router and restart the timer (so it will check again in 1 min), if yes, you don't have to restart the timer and you're done. - Piglet

1 Answers

3
votes

This is an untested piece of code quickly put together.

effectiveRouter = nil
counter = 0
wifi.sta.config("dlink", "password1")
tmr.alarm(1, 1000, tmr.ALARM_SEMI, function()
  counter = counter + 1
  if counter < 60 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 < 120 then
    wifi.sta.config("cisco", "password2")
    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)

It'll first try to connect to 'dlink' for 60s, then to 'cisco' for another 60s, and will eventually give up after that if neither attempts was successful. It uses a semi-automatic timer which is only restarted if there's no IP yet.