Created
March 11, 2018 14:44
-
-
Save danielkucera/29d9fa865718fceecf99a1ef1161ae4d to your computer and use it in GitHub Desktop.
radio clock ntp sync
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
gpio.mode(1, gpio.OUTPUT) | |
gpio.mode(2, gpio.OUTPUT) | |
gpio.write(1, gpio.HIGH) | |
gpio.write(2, gpio.LOW) | |
synced=0 | |
timeset=0 | |
inchours=0 | |
timezone=1 -- range 1 - 25 | |
huptmr = tmr.create() | |
-- need to start clock first | |
rtctime.set(0, 1) | |
sntp.sync(nil, | |
function(sec, usec, server, info) | |
synced=1 | |
print('sync', sec, usec, server) | |
end, | |
function() | |
print('failed!') | |
end, | |
1 | |
) | |
function hup() | |
gpio.write(2, gpio.HIGH) | |
tmr.delay(100000) | |
gpio.write(2, gpio.LOW) | |
end | |
function hups(hours) | |
inchours = hours | |
huptmr:register(1000, tmr.ALARM_AUTO, function() | |
if inchours < 1 then | |
huptmr.unregister() | |
return | |
end | |
inchours = inchours - 1 | |
hup() | |
end ) | |
huptmr:start() | |
end | |
function reset(hours) | |
gpio.write(1, gpio.LOW) | |
rstt = tmr.create() | |
rstt:register(700, tmr.ALARM_SINGLE, function() | |
gpio.write(1, gpio.HIGH) | |
hups(hours) | |
end) | |
rstt:start() | |
end | |
cron.schedule("0 0 * * *", function(e) | |
if timeset == 1 and synced == 1 then | |
reset(timezone) | |
end | |
end) | |
cron.schedule("0 * * * *", function(e) | |
if timeset == 0 and synced == 1 then | |
tm = rtctime.epoch2cal(rtctime.get()) | |
timeset=1 | |
reset(timezone+tm["hour"]) | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment