Skip to content

Instantly share code, notes, and snippets.

@Terrabits
Created January 11, 2019 07:21
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 Terrabits/ac914967864d57cc1b05a81cc21e7341 to your computer and use it in GitHub Desktop.
Save Terrabits/ac914967864d57cc1b05a81cc21e7341 to your computer and use it in GitHub Desktop.
NodeMCU firmware init with WiFi
-- tested on HiLetgo NodeMCU v1.0
-- requires modules: wifi, tmr
-- (and optionally gpio for blinky light)
local ssid = "ssid"
local password = "password"
local BLUE_LED = 4
function connect_to_wifi(ssid, password)
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid, password)
wifi.sta.connect()
end
function wait_for_ip(callback)
tmr.create():alarm(100, tmr.ALARM_AUTO,
function(t)
if wifi.sta.getip() ~= nil then
t:unregister()
callback()
end
end)
end
function continue_with_wifi()
gpio.write(BLUE_LED, gpio.LOW)
print('Connected to WiFi')
end
gpio.mode (BLUE_LED, gpio.OUTPUT)
gpio.write(BLUE_LED, gpio.HIGH)
connect_to_wifi(ssid, password)
wait_for_ip(continue_with_wifi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment