Created
April 2, 2019 18:19
-
-
Save atrakeur/e38e2d725c62b835980e652d3a0fed43 to your computer and use it in GitHub Desktop.
Lua ESP8266 convenience libraries
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
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 |
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
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