Last active
February 21, 2016 00:06
-
-
Save andrew-smith/74ac15b71cc42a833f1a to your computer and use it in GitHub Desktop.
nodemcu script to connect to wifi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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