Skip to content

Instantly share code, notes, and snippets.

@andrew-smith
Last active February 21, 2016 00:06
Show Gist options
  • Save andrew-smith/74ac15b71cc42a833f1a to your computer and use it in GitHub Desktop.
Save andrew-smith/74ac15b71cc42a833f1a to your computer and use it in GitHub Desktop.
nodemcu script to connect to wifi
function connect_to_wifi(ssid, password, callback)
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid, password)
wifi.sta.connect()
secondsWaited = 0
secondsToWait = 15
-- WIFI CONNECT HAS ALARM ID 0
tmr.alarm(0, 1000, 1, function()
-- test if connected yet
if wifi.sta.status() == 5 then
tmr.stop(0)
print("Connected to: " .. ssid)
callback(true)
else
secondsWaited = secondsWaited + 1
if secondsWaited > secondsToWait then
tmr.stop(0)
print("Could not connect to: " .. ssid)
print("Reason: " .. wifi.sta.status())
callback(false)
end
end
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment