Skip to content

Instantly share code, notes, and snippets.

@marcelstoer
Created June 3, 2018 19:00
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 marcelstoer/7ae3e19c7be2847530fa63433637de60 to your computer and use it in GitHub Desktop.
Save marcelstoer/7ae3e19c7be2847530fa63433637de60 to your computer and use it in GitHub Desktop.
NodeMCU Lua initializer with DNS & internet connectivity testing
WIFI_SSID = "--your-value-here--"
WIFI_PASSWORD = "--your-value-here--"
wifi_got_ip_event = function(T)
local site = "wikipedia.org"
-- Note: Having an IP address does not mean there is internet access!
-- Internet connectivity can be determined with net.dns.resolve().
print("WiFi connection is established! IP address is: " .. T.IP)
print("DNS server 1: " .. net.dns.getdnsserver(0))
print("DNS server 2: " .. net.dns.getdnsserver(1))
net.dns.resolve(site, function(sk, ip)
if (ip == nil) then
print("DNS fail!")
else
print("Connected to the internet")
print(site .. " IP: " .. ip)
end
end)
end
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, wifi_got_ip_event)
print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config({ ssid = WIFI_SSID, pwd = WIFI_PASSWORD })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment