-->
Page 1 of 5

Verifying connectivity

PostPosted: Sat Jan 17, 2015 2:07 pm
by wouter011
So I'm writing a LUA script using NodeMCU and I'm wondering wat is the correct way to verify that the ESP8266 is connected to an AP.

Code: Select all-- CONFIG --
SSID =   "bbox2-1bb0"
PWD  =    "password"
------------

-- Connect to Wi-Fi network
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID, PWD)

-- Wait for connection
repeat   
   tmr.delay(1000) -- 1000ms
     until wifi.sta.status() == 5 -- STATION_GOT_IP

print("Connected to network " .. SSID)
print("IP: " .. wifi.sta.getip())


OR

Code: Select all-- CONFIG --
SSID =   "bbox2-1bb0"
PWD  =    "password"
------------

-- Connect to Wi-Fi network
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID, PWD)

-- Wait for connection
repeat   
   tmr.delay(1000) -- 1000ms
     until wifi.sta.getip() ~= nil

print("Connected to network " .. SSID)
print("IP: " .. wifi.sta.getip())


What code should I go with and why?

Re: Verifying connectivity

PostPosted: Sat Jan 17, 2015 3:44 pm
by alonewolfx2
i am using this code and its working fine. first timer for check ip second timer for doing temp things.
Code: Select all--Check ip and do something.
gpio.mode(3,gpio.OUTPUT)
tmr.alarm(0,1000, 1, function()
   if wifi.sta.getip()==nil then
      print("connecting to AP...")
      gpio.write(3,gpio.HIGH)
   else
      print('ip: ',wifi.sta.getip())
      gpio.write(3,gpio.LOW)
           tmr.alarm(1,60000, 1, function()
                 dofile("gettemp.lua")
           end)
      tmr.stop(0)
   end
end)


Re: Verifying connectivity

PostPosted: Sun Jan 18, 2015 4:08 am
by wouter011
alonewolfx2 wrote:i am using this code and its working fine.

Thanks!

I think I might go for this.
Code: Select all--[[--------------------------
   LUA script for configuring an ESP8266 to connect to a Wi-Fi network
   Author: wvanvlaenderen
--------------------------]]--

----------- CONFIG -----------
SSID      = "bbox2-1bb0"
PASSWORD   = "password"
TIMEOUT    = 30000000 -- 30s
MAIN       = "main.lua"
------------------------------

-------- Station modes -------
STAMODE = {
STATION_IDLE             = 0,
STATION_CONNECTING       = 1,
STATION_WRONG_PASSWORD   = 2,
STATION_NO_AP_FOUND      = 3,
STATION_CONNECT_FAIL     = 4,
STATION_GOT_IP           = 5
}
------------------------------

--[[ Function connect: ------
      connects to a predefined access point
      params:
         timeout int    : timeout in us
--------------------------]]--
function connect(timeout)
   local time = tmr.now()
   wifi.sta.connect()

   -- Wait for IP address; check each 1000ms; timeout
   tmr.alarm(1, 1000, 1,
      function()
         if wifi.sta.status() == STAMODE.STATION_GOT_IP then
               tmr.stop(1)
                  print("Station: connected! IP: " .. wifi.sta.getip())
                 -- dofile(MAIN)
         else
                     if tmr.now() - time > timeout then
                        tmr.stop(1)
                        print("Timeout!")
                        if wifi.sta.status() == STAMODE.STATION_IDLE          then print("Station: idling") end
                        if wifi.sta.status() == STAMODE.STATION_CONNECTING       then print("Station: connecting") end
                        if wifi.sta.status() == STAMODE.STATION_WRONG_PASSWORD    then print("Station: wrong password") end
                        if wifi.sta.status() == STAMODE.STATION_NO_AP_FOUND    then print("Station: AP not found") end
                        if wifi.sta.status() == STAMODE.STATION_CONNECT_FAIL    then print("Station: connection failed") end
                  end
         end
      end
   )
end

--[[ Main loop ]]--
print("Setting up Wi-Fi connection..")
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID, PASSWORD)
connect(TIMEOUT)


Re: Verifying connectivity

PostPosted: Wed Oct 07, 2015 2:05 pm
by lakkimsetty
The wifi.sta.status function never returns wrong password even if we give one. Did you guys notice?