Skip to content

Instantly share code, notes, and snippets.

@atrakeur atrakeur/ntp.lua
Created Apr 2, 2019

Embed
What would you like to do?
Lua ESP8266 convenience libraries
ntpOnSuccess = function () end
function ntpStart(onSuccess, onError)
ntpOnSuccess = onSuccess
sntp.sync(
nil,
function(sec, usec, server, info)
print("SNTP SYNCED")
rtctime.set(sec, usec)
-- Call user callback on first sync, then disable
ntpOnSuccess(sec, usec, server, info)
ntpOnSuccess = function() end
end,
function()
onError("SNTP FAILED")
end,
1 -- autosync true
)
end
function wifiConfig(cfg)
wifi.setmode(wifi.STATION)
wifi.sta.config(cfg)
wifi.sta.autoconnect(1)
end
globalOnSuccess = nil
globalOnError = nil
function wifiConnect(onSuccess, onError)
wifi.sta.connect()
globalOnError = onError
globalOnSuccess = onSuccess
tmr.create():alarm(250, tmr.ALARM_SINGLE, wifiCheck)
end
function wifiCheck()
onError = globalOnError
onSuccess = globalOnSuccess
if wifi.sta.status() == wifi.STA_FAIL then
onError("STAFAIL")
elseif wifi.sta.status() == wifi.STA_APNOTFOUND then
onError("APNOTFOUND")
elseif wifi.sta.status() == wifi.STA_WRONGPWD then
onError("WRONGPWD")
else
local ip = wifi.sta.getip()
if ip then
onSuccess(ip)
else
tmr.create():alarm(250, tmr.ALARM_SINGLE, wifiCheck)
end
end
end
function wifiDisconnect(onSuccess, onError)
-- TODO
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.