ESP8266 Lua Real Time Clock

I nearly have this running – I could do with a little help though!

The latest LUA has more than one timer – so you can spare one for a clock.

Here’s the basic idea – you can run this as is if you want…

tmr.alarm(1,1000, 1, function()
second=second+1
if second>=60 then
second=0 minute=minute+1
if minute>=60 then
minute=0 hour=hour+1
if hour>=24 then
hour=0 day=day+1
if day>=7 then day=0
end
end
end
end
print(string.format(“%02d:%02d:%02d Day=%01d”,hour,minute,second,day))
end)

There you go – THAT was easy !!
It’s also quite easy to get the time from a server. I made my own PHP page and put it on any old hosting – I’ll not make that public or my server will get stoned to death but it doesn’t take much of an imagination to go get the time. In this example I’ve missed out month and year but you get the idea.  What I would do it get the time every night maybe 2am from a real clock (and on power up of course)

In my case from my web code,  I get a string back which contains….” lots of stuff ;timestr=22:04:02 10-12-2014; more stuff”

So – I want to extract at least 3 and more likely all 6 numbers out of there – so I can punch them into my variables above.  I can do it one at a time but it would be NEAT if Lua could return 6 variables at once… like….

hour,minute,second,day,month,year=string.match(string.match(payload,”timestr=%d*”),”%d*”)

Except of course that the last bit of that is rubbish because I’m rubbish at regular expressions – I don’t suppose any of you Lua masters out there would know how to take that string above and convert it into the numbers I want?  I promise if someone provides a working solution, I’llrepay by publishing the source of my PHP page – which actually returns a lot more in the string.

Why not figure out how to access an NTP server? I find them slow – this is instant and I get more useful info back.

Thoughts?

Advertisements

2 thoughts on “ESP8266 Lua Real Time Clock

  1. — The following should pull out the numbers of interest. Lua doesn’t support full-blown regexp
    — but a subset which is typically “good enough”. This isn’t optimal but it works. Enjoy!

    function table.pack(…)
    return { n = select(“#”, …), … }
    end

    local data = “{time=1418272378;timestr=04:32:58 11-12-2014;dawn=07:38;dusk=16:24;}”

    local caps = table.pack(string.match(data,”(%d+);timestr=(%d+):(%d+):(%d+)%s+(%d+)-(%d+)-(%d+);dawn=(%d+):(%d+);dusk=(%d+):(%d+)”))

    — Note: all the numbers on the array are strings. You can use tonumber() to convert
    — these to numbers as needed.
    for k,v in pairs(caps) do
    print(k, tonumber(v))
    end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s