Post your best Lua script examples here
User avatar
By GeoNomad
#4567 A simple script to grab a time stamp from Google.

If you are behind the Great Chinese Firewall, you may have to change Google.com to Baidu.com, but in most places Google will have a close server with an accurate time in the header.

Code: Select all
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)
    print(string.sub(payload,string.find(payload,"Date: ")+6,string.find(payload,"Date: ")+35))
    conn:close()
    end)
conn:dns('google.com',function(conn,ip) ipaddr=ip end)
conn:connect(80,ipaddr)
heap = node.heap()
conn:send("HEAD / HTTP/1.1\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; ESP8266 NodeMcu Lua;)\r\n")
conn:send("\r\n")



It is not advisable to use this in a short loop or the google server will soon identify you as a DDoS threat.
User avatar
By Venkatesh
#28718 I use time.is website for local time in my time zone.
------------------
Code: Select allconn=net.createConnection(net.TCP, 0)
conn:on("connection",function(conn, payload)
            conn:send("HEAD / HTTP/1.1\r\n"..
                      "Host: time.is\r\n"..
                      "Accept: */*\r\n"..
                      "User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)"..
                      "\r\n\r\n")
            end)
           
conn:on("receive", function(conn, payload)
    -- rint(payload)
   local ctime ={}
    ctime = string.sub(payload,string.find(payload,"Expires: ")
     +9,string.find(payload,"Expires: ")+33)
        conn:close()
   print("time :("..ctime..")")
end)

conn:connect(80,"time.is")
User avatar
By forlotto
#31568 Ask Google for the Time

This is an example of how to connect to Google using an ESP8266 running NodeMCU Lua and retrieve just the HEAD information, which happens to include the current time.

It would be possible to do this with NTP, but that would require more coding. The results wouldn't be any more accurate. I am pretty sure Google keeps the time on their servers well synchronized with the actual time.
Code: Select all-- retrieve the current time from Google
-- tested on NodeMCU 0.9.5 build 20150108

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('\nRetrieved in '..((tmr.now()-t)/1000)..' milliseconds.')
    print('Google says it is '..string.sub(payload,string.find(payload,"Date: ")
           +6,string.find(payload,"Date: ")+35))
    conn:close()
    end)
t = tmr.now()   
conn:connect(80,'google.com')



The program can be saved to a file, google.lua. Then, dofile("google.lua") will retrieve and print the time. From this location, the normal elapsed time from requesting to printing was under one tenth of a second. Pretty good.
program output
How does it work?

A TCP type connection is created and the event driven functions are assigned to on "connection" and on "receive". conn:connect(80,'google.com') connects to Google. When the connection is made, the on "connection" event is fired. At this point, the "HEAD..." message is sent to the server, causing it to respond with a few lines of information. One of those lines contains the date and time. The string.sub() function extracts the date and throws away the rest.