2016-04-12 18:07:25 +02:00
|
|
|
dofile("wlancfg.lua")
|
2016-04-12 19:42:35 +02:00
|
|
|
dofile("timecore.lua")
|
2016-04-19 18:25:41 +02:00
|
|
|
dofile("wordclock.lua")
|
2016-04-12 18:07:25 +02:00
|
|
|
print("Initialize Wordclock")
|
|
|
|
|
2016-04-12 18:12:26 +02:00
|
|
|
function startTimeupdate()
|
|
|
|
-- Found: http://thearduinoguy.org/using-an-esp8266-as-a-time-source-part-2/
|
|
|
|
-- retrieve the current time from Google
|
|
|
|
conn=net.createConnection(net.TCP, 0)
|
2016-04-12 19:16:47 +02:00
|
|
|
-- Send the HTTP request
|
2016-04-12 18:12:26 +02:00
|
|
|
conn:on("connection",function(conn, payload)
|
2016-04-12 19:16:47 +02:00
|
|
|
conn:send("HEAD / HTTP/1.1\r\n"..
|
2016-04-12 18:12:26 +02:00
|
|
|
"Host: google.com\r\n"..
|
|
|
|
"Accept: */*\r\n"..
|
|
|
|
"User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
|
|
|
|
"\r\n\r\n")
|
|
|
|
end)
|
2016-04-12 19:16:47 +02:00
|
|
|
-- Extract the time from the answer
|
2016-04-12 18:12:26 +02:00
|
|
|
conn:on("receive", function(conn, payload)
|
2016-04-12 19:16:47 +02:00
|
|
|
local timestr = string.sub(payload,string.find(payload,"Date: ")
|
|
|
|
+6,string.find(payload,"Date: ")+35)
|
|
|
|
-- time looks like: Tue, 12 Apr 2016 16:26:31 GMT
|
|
|
|
print('Time: '.. timestr)
|
|
|
|
local firstComma=string.find(timestr, ",")
|
|
|
|
dow=string.sub(timestr, 0, firstComma-1)
|
|
|
|
nextParts=string.sub(timestr, firstComma+1)
|
|
|
|
i=0
|
|
|
|
hourminsectxt=nil
|
|
|
|
-- Split the text at each space
|
|
|
|
for str in string.gmatch(nextParts, "([^ ]+)") do
|
|
|
|
if (i==0) then
|
|
|
|
day=str
|
|
|
|
elseif (i==1) then
|
|
|
|
month=convertMonth(str)
|
|
|
|
elseif (i==2) then
|
|
|
|
year=str
|
|
|
|
elseif (i==3) then
|
|
|
|
hourminsectxt=str
|
|
|
|
end
|
|
|
|
i=i+1
|
|
|
|
end
|
|
|
|
-- Extract the time
|
|
|
|
if (hourminsectxt ~= nil) then
|
|
|
|
i=0
|
|
|
|
-- split the text at each :
|
|
|
|
for str in string.gmatch(hourminsectxt, "([^:]+)") do
|
|
|
|
if (i==0) then
|
|
|
|
hour=str
|
|
|
|
elseif (i==1) then
|
|
|
|
minutes=str
|
|
|
|
elseif (i==2) then
|
|
|
|
seconds=str
|
|
|
|
end
|
|
|
|
i=i+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
print("---------------")
|
|
|
|
year, month, day, hour, minutes, seconds = getLocalTime(year, month, day, hour, minutes, seconds,dow)
|
|
|
|
print(year)
|
|
|
|
print(month)
|
|
|
|
print(day)
|
|
|
|
print(hour)
|
|
|
|
print(minutes)
|
|
|
|
print(seconds)
|
2016-04-19 18:25:41 +02:00
|
|
|
-- Manually set something
|
|
|
|
leds=display_timestat(hour,minutes)
|
|
|
|
for k,v in pairs(leds) do
|
|
|
|
if (v == 1) then
|
|
|
|
print(k)
|
|
|
|
end
|
|
|
|
end
|
2016-04-12 19:16:47 +02:00
|
|
|
print("---------------")
|
2016-04-12 18:12:26 +02:00
|
|
|
conn:close()
|
2016-04-12 19:16:47 +02:00
|
|
|
end)
|
2016-04-12 18:12:26 +02:00
|
|
|
conn:connect(80,'google.com')
|
|
|
|
end
|
2016-04-12 18:07:25 +02:00
|
|
|
|
2016-04-12 18:12:26 +02:00
|
|
|
-- 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)
|
|
|
|
print('IP: ',wifi.sta.getip())
|
2016-04-12 18:16:16 +02:00
|
|
|
-- Initaly set the time
|
2016-04-12 18:12:26 +02:00
|
|
|
startTimeupdate()
|
2016-04-12 18:16:16 +02:00
|
|
|
|
|
|
|
-- Update the time each minute
|
|
|
|
tmr.alarm(1, 60000, 1, function()
|
|
|
|
startTimeupdate()
|
|
|
|
end)
|
2016-04-12 18:12:26 +02:00
|
|
|
end
|
2016-04-19 18:25:41 +02:00
|
|
|
end)
|