diff --git a/test.lua b/test.lua index 836f5f3..69c354c 100644 --- a/test.lua +++ b/test.lua @@ -1,4 +1,36 @@ dofile("wlancfg.lua") print("Initialize Wordclock") +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) + + conn:on("connection",function(conn, payload) + conn:send("HEAD / HTTP/1.1\r\n".. + "Host: google.com\r\n".. + "Accept: */*\r\n".. + "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)".. + "\r\n\r\n") + end) + + conn:on("receive", function(conn, payload) + print('Time: '..string.sub(payload,string.find(payload,"Date: ") + +6,string.find(payload,"Date: ")+35)) + conn:close() + end) + t = tmr.now() + conn:connect(80,'google.com') +end +-- 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()) + + startTimeupdate() + end +end) \ No newline at end of file