getting time from a server
Posted: Fri Sep 23, 2016 5:45 pm
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
Also, the NIST servers send the time in a very nicely formatted string, which I converted to the thingspeak format
Code: Select all
wifi.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)