Wait for IP on NodeMCU

So, now that the NodeMCU is trying to connect to the wifi, we know how to do a RESTful call, it’s time for the last step, waiting for the NodeMCU to connect and get a valid IP.

The code:

-- wait for an IP
cnt = 10
tmr.alarm(0,500,1,function()
  if wifi.sta.getip()==nil then
    cnt = cnt-1
    if cnt<=0 then

      -- Did not get an IP in time, so quitting
      tmr.stop(0)
      gpio.write(pin_error,1)
      print "Not connected to wifi."
    end
  else

    -- Connected to the wifi
    tmr.stop(0)
    print("\nStarting main")
    dofile("main.lua")
  end
end)

The code uses a timer to check every 500 microseconds (half a second) to see whether we have a valid IP address. If we do we can start our main program. If we don’t we continue to try, but at most 10 times (the cnt variable). If still fails after 10 attempts (5 seconds), we assume it will not connect. We turn on our error led and stop the timer.

Well, lets now put it all together into one beautiful init.lua which we can use on any of our NodeMCU’s.

------------------
--              --
--   init.lua   --
--              --
------------------

-- 1:the wifi credentials. Change to match your wifi
ssid = "YourSSID"
pass = "YourPass"
thisNode = 1

-- 2:Configure wifi
wifi.setmode(wifi.STATION)
print("\nMy MAC address: "..wifi.sta.getmac())
wifi.sta.config(ssid,pass)

-- 3:Prepare GPIOs
pin_interrupt = 1
gpio.mode(pin_interrupt,gpio.INPUT)
pin_error = 0
gpio.mode(pin_error,gpio.OUTPUT)

-- 4:Read and write GPIOs
gpio.write(pin_error,gpio.HIGH)
interruptit = gpio.read(pin_interrupt)

-- 5:Check whether to start the main program
if interruptit==1 then

  -- 6:Wait for an IP
  cnt = 10
  tmr.alarm(0,500,1,function()
    interruptit = gpio.read(pin_interrupt)
    if interruptit==0 then

      -- 7:The interrupt button was pressed while waiting for an IP
      tmr.stop(0)
      gpio.write(pin_error,gpio.LOW)
      print "GPIO1 low, so not starting main"
    else
      if wifi.sta.getip()==nil then
        cnt = cnt-1
        if cnt<=0 then

          -- 8:Did not get an IP in time, so quitting
          tmr.stop(0)
          gpio.write(pin_error,gpio.LOW)
          print "Not connected to wifi."
        end
      else

        -- 9:Connected to the wifi, send IFTTT trigger
        tmr.stop(0)
        ip, nm, gw = wifi.sta.getip()
        conn = net.createConnection(net.TCP, false) 
        conn:on("receive", function(conn, answer) print(answer) end)
        conn:on("connection", function(cn, answer)
          cn:send("GET /trigger/NodeMCUBoot/with/key/--YourKey--?"
          .."value1="..thisNode.."&value2="..ip.." HTTP/1.1\r\n"
          .."Host: maker.ifttt.com\r\n"
          .."Connection: keep-alive\r\n"
          .."Accept: */*\r\n\r\n")
        end)
        conn:connect(80,"maker.ifttt.com")

        -- 10:Start the main program
        print("\nStarting main")
        dofile("main.lua")
      end
    end
  end)

else

  -- 11:The interrupt button was pressed
  gpio.write(pin_error,gpio.LOW)
  print("\nGPIO1 low, so not starting main.")
end

That’s it, all combined.

So we connect to the wifi, we set and check the GPIOs, we wait for an IP (while still checking if the switch on GPIO1 is pressed) and once we have an IP, we send a trigger to IFTTT to show that we are up and running and start the main program.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s