Skip to content

Instantly share code, notes, and snippets.

@confile
Created December 20, 2015 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save confile/1ccf0783a2a25a585f72 to your computer and use it in GitHub Desktop.
Save confile/1ccf0783a2a25a585f72 to your computer and use it in GitHub Desktop.
Connect to local wifi
-- 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