I am a newbe to the NodeMCU platform which I am running on a stand alone esp8266 201 dev board.
I would like to test the script listed below but don't know how to run it within ESPlorer. I have saved this code in in a file called "ntpTime.lua". When I run the file with dofile("ntpTime.lua") it skips over the "getDate" function. I have inserted some print statements to show the flow and as shown below the getDate2 print statement which is within the getDate function entry point is not printed.
Console print out:
==============================
> dofile("ntpTime.lua")
1462081810
1462081810
getDate1
>
=======================================
Thanks for any help to get me on the right track!
tma
--getDate;
local tabIndexOverflow = function(seed, table)
-- This subtracts values from the table from seed until an overflow
-- This can be used for probability
for i = 1, #table do
if seed - table[i] <= 0 then
return i, seed
end
end
end
ntpSecs, usec = rtctime.get()
print(ntpSecs)
unix = ntpSecs
--print(getDate(ntpSecs))
print(unix)
print("getDate1")
local getDate = function(unix)
print("getDate2")
-- Given unix date, return string date
assert(unix == nil or type(unix) == "number" or unix:find("/Date%((%d+)"), "Please input a valid number to \"getDate\"")
local unix = (type(unix) == "string" and unix:match("/Date%((%d+)") / 1000 or unix or os.time()) -- This is for a certain JSON compatability. It works the same even if you don't need it
local dayCount, year, days, month = function(yr) return (yr % 4 == 0 and (yr % 100 ~= 0 or yr % 400 == 0)) and 366 or 365 end, 1970, math.ceil(unix/86400)
while days >= dayCount(year) do days = days - dayCount(year) year = year + 1 end -- Calculate year and days into that year
month, days = tabIndexOverflow(days, {31,(dayCount(year) == 366 and 29 or 28),31,30,31,30,31,31,30,31,30,31}) -- Subtract from days to find current month and leftover days
-- hours = hours > 12 and hours - 12 or hours == 0 and 12 or hours -- Change to proper am or pm time
-- local period = hours > 12 and "pm" or "am"
-- Formats for you!
print(string.format("%d/%d/%04d", month, days, year))
-- string.format("%02d:%02d:%02d %s", hours, minutes, seconds, period)
return {Month = month, day = days, year = year, hours = math.floor(unix / 3600 % 24), minutes = math.floor(unix / 60 % 60), seconds = math.floor(unix % 60)}
end