Created
December 20, 2015 15:37
-
-
Save confile/1ccf0783a2a25a585f72 to your computer and use it in GitHub Desktop.
Connect to local 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
-- init.lua -- | |
WIFI_SSID = "ssid" | |
WIFI_PASS = "1232134123" | |
wifiReady = 0 | |
WIFI_LED = 0 | |
WIFI_ALARM_ID = 0 | |
WIFI_LED_BLINK_ALARM_ID = 1 | |
gpio.write(0, gpio.LOW) | |
function configureWiFi() | |
print("Startup up wifi mode") | |
wifi.setmode(wifi.STATION) | |
wifi.sta.config(WIFI_SSID, WIFI_PASS) | |
wifi.sta.autoconnect(1) | |
wifi.sta.connect() | |
tmr.alarm(WIFI_ALARM_ID, 2000, 1, wifi_watch) | |
end | |
function run() | |
print("run") | |
print(wifi.sta.getip()) | |
-- Run the main file | |
--dofile("main.lua") | |
end | |
-- while NOT connected to WiFi you blink a LED, see below | |
function wifi_watch() | |
-- 0: STATION_IDLE, | |
-- 1: STATION_CONNECTING, | |
-- 2: STATION_WRONG_PASSWORD, | |
-- 3: STATION_NO_AP_FOUND, | |
-- 4: STATION_CONNECT_FAIL, | |
-- 5: STATION_GOT_IP. | |
status = wifi.sta.status() | |
if status == 5 then | |
print("WiFi: connected") | |
print(wifi.sta.getip()) | |
turnWiFiLedOff() | |
if (wifiReady == 0) then | |
wifiReady = 1 | |
run() | |
end | |
else | |
print("WiFi: (re-)connecting") | |
wifiReady = 0 | |
turnWiFiLedOnOff() | |
wifi.sta.connect() | |
end | |
end | |
function turnWiFiLedOnOff() | |
turnWiFiLedOn() | |
tmr.alarm(WIFI_LED_BLINK_ALARM_ID, 500, 0, function() | |
turnWiFiLedOff() | |
end) | |
end | |
function turnWiFiLedOn() | |
gpio.write(WIFI_LED, gpio.LOW) | |
end | |
function turnWiFiLedOff() | |
gpio.write(WIFI_LED, gpio.HIGH) | |
end | |
configureWiFi() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment