diff --git a/nodemcu-master-bit,enduser_setup,file,gpio,node,rtcfifo,rtcmem,rtctime,sntp,spi,tmr,uart,wifi,ws2812-integer.bin b/nodemcu-master-bit,enduser_setup,file,gpio,node,rtcfifo,rtcmem,rtctime,sntp,spi,tmr,uart,wifi,ws2812-integer.bin new file mode 100644 index 0000000..c2d7814 Binary files /dev/null and b/nodemcu-master-bit,enduser_setup,file,gpio,node,rtcfifo,rtcmem,rtctime,sntp,spi,tmr,uart,wifi,ws2812-integer.bin differ diff --git a/ntpTest.lua b/ntpTest.lua new file mode 100644 index 0000000..3bbb648 --- /dev/null +++ b/ntpTest.lua @@ -0,0 +1,50 @@ +dofile("wlancfg.lua") + +-- Wait to be connect to the WiFi access point. +tmr.alarm(0, 100, 1, function() + if wifi.sta.status() ~= 5 then + print("Connecting to AP...") + else + tmr.stop(0) + -- Switch of the booting lamp + gpio.write(5, gpio.LOW) + print('IP: ',wifi.sta.getip()) + + --ptbtime1.ptb.de + sntp.sync('192.53.103.108', + function(sec,usec,server) + print('sync', sec, usec, server) + end, + function() + print('failed!') + end + ) + + end +end) + + +EPOCH_YR=1970 +--SECS_DAY=(24L * 60L * 60L) +SECS_DAY=86400 + +gettime = function(unixtimestmp) + local year = EPOCH_YR + local dayclock = math.floor(unixtimestmp % SECS_DAY) + local dayno = math.floor(unixtimestmp / SECS_DAY) + + local sec = dayclock % 60 + local min = math.floor( (dayclock % 3600) / 60) + local hour = math.floor(dayclock / 3600) + local wday = math.floor( (dayno + 4) % 7) -- Day 0 was a thursday + + return hour, min, sec +end + +tmr.alarm(1, 1000, 1 ,function() + sec, usec = rtctime.get() + print("Time : " , sec) + hour, min, sec = gettime(sec) + print(hour, " ", min, " ", sec) +end) + diff --git a/unit/unix2time.lua b/unit/unix2time.lua new file mode 100644 index 0000000..3cb18ce --- /dev/null +++ b/unit/unix2time.lua @@ -0,0 +1,62 @@ +--- +-- Source: +-- http://www.jbox.dk/sanos/source/lib/time.c.html + +YEAR0=1900 + +EPOCH_YR=1970 +--SECS_DAY=(24L * 60L * 60L) +SECS_DAY=86400 + +ytab = {} +ytab[0] = {} +ytab[0][0] = 31 +--31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} +ytab[1] = {} +--{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} + + +leapyear = function(year) + return ( not ((year) % 4) and (((year) % 100) or not ((year) % 400))) +end + +yearsize = function(year) + if leapyear(year) then + return 366 + else + return 365 + end +end + +gettime = function(unixtimestmp) + local year = EPOCH_YR + local dayclock = math.floor(unixtimestmp % SECS_DAY) + local dayno = math.floor(unixtimestmp / SECS_DAY) + + local sec = dayclock % 60 + local min = math.floor( (dayclock % 3600) / 60) + local hour = math.floor(dayclock / 3600) + local wday = math.floor( (dayno + 4) % 7) -- Day 0 was a thursday + + while (dayno >= yearsize(year)) + do + dayno = dayno - yearsize(year); + year=year + 1 + end + local yday = dayno + local mon = 0 + + --isleap=0 if leapyear(year) then isleap=1 end + --while (dayno >= ytab[isleap][mon]) + --do + -- dayno = dayno - ytab[isleap][mon]; + -- mon = mon + 1 + -- isleap=0 if leapyear(year) then isleap=1 end + -- end + -- mday = dayno + 1 + + return year, hour, min, sec +end + +print( gettime(1462391501) ) +