diff options
author | Aki <please@ignore.pl> | 2024-01-15 00:33:32 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2024-01-15 00:33:32 +0100 |
commit | ba663a7dab9c132695da3ff949cbedc516817a13 (patch) | |
tree | 75909941afbb927e7b1c70076a2bb2ec9b01bbfc /wakeup.lua | |
parent | 8e04a77a96eccb86f775f7aa2bbc14ad587c313a (diff) | |
download | nodemcu-wakeup-ba663a7dab9c132695da3ff949cbedc516817a13.zip nodemcu-wakeup-ba663a7dab9c132695da3ff949cbedc516817a13.tar.gz nodemcu-wakeup-ba663a7dab9c132695da3ff949cbedc516817a13.tar.bz2 |
Implemented naive scheduling
Diffstat (limited to 'wakeup.lua')
-rw-r--r-- | wakeup.lua | 46 |
1 files changed, 40 insertions, 6 deletions
@@ -61,18 +61,52 @@ function wakeup:sendout (interval, times, after) end +function wakeup:now () + local time = rtctime.epoch2cal(rtctime.get()) + time.year = nil + time.sec = nil + return time +end + + +local +function compare (time, pattern) + for key, value in pairs(pattern) do + if time[key] ~= value then + return false + end + end + return true +end + + function wakeup:init () gpio.write(4, gpio.HIGH) gpio.mode(4, gpio.OUTPUT) self.data = magic_packet(self.mac) wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function () - gpio.write(4, gpio.LOW) - - self:sendout(500, 5, function () - wifi.setmode(wifi.NULLMODE, false) - gpio.write(4, gpio.HIGH) - end) + sntp.sync( + self.sntp, + function () + if compare(self:now(), self.when) then + gpio.write(4, gpio.LOW) + self:sendout(500, 5, function () + wifi.setmode(wifi.NULLMODE, false) + gpio.write(4, gpio.HIGH) + end) + else + print("Not yet time") + wifi.setmode(wifi.NULLMODE, false) + tmr.create():alarm(25000, tmr.ALARM_SINGLE, function () + self:run() + end) + end + end, + function (code, err) + print("SNTP sync failed", code, err) + node.restart() + end) end) end |