As the title says... Chat on...

User avatar
By tma
#46596 Greetings,

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 :D
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
User avatar
By tma
#46659 Thanks for your interest!

I personally added that function call line thinking that it might solve the problem but instead it caused an error so I commented it out. I don't know how to call the function to get it to execute and hope someone here can show me the way.

I have now tried taking the code out of the function and local restriction so that it simply executes inline. This way it does calculate the time and year OK but the month and date remain as nils thus are not being assigned.

I am used to being able to step through code with break points to track down problems and I miss that with NodeMCU.
User avatar
By grex
#46661 Hi,

Sorry for not getting the point.
But looking at the month and day problem I can see that the array you pass has to be filled with the sums of days so far, not the days of each month. So I would expect it to work in January but not later.

I do not understand the problem calling the function. Maybe I will run your code lauter on and see. No possibility right now.

Hth and have fun
Grex