Post your best Lua script examples here

User avatar
By brunovianna
#55588 Here is a simple script to get the time from a US govn't NIST server. I tried an NTP server but it is UDP only, and I never got the UDP client from nodemcu LUA to send out a message.

Also, the NIST servers send the time in a very nicely formatted string, which I converted to the thingspeak format :)

Code: Select allwifi.setmode(wifi.STATION);
wifi.sta.config("SSID" ,"passwd");

function get_time ()


    --print ("getting time")
    --print (wifi.sta.status())
   
    conn = nil
    conn = net.createConnection(net.TCP, 0)
   
    ntp_time = nil
       
   
   
    conn:on("receive", function(conn, payload)
        print (payload:sub(7,20))
        year = payload:sub(7,9)+2000
        ts_string = year..payload:sub(10,25)
        --print (year)
        print (ts_string)
    end)
   
    conn:connect(13,'time-a.timefreq.bldrdoc.gov')
    conn:close()

end

tmr.alarm(1, 3000, 1, function()
get_time()
end)