Post your best Lua script examples here

User avatar
By bax
#8733 This is a lightweight client to get the date/time from a NIST DayTime server. The DayTime protocol was designed for minimal processing on limited hardware. The LUA code is a modified version of the httpget.lua example from the LuaLoader Quick Start Guide. Field definitions in the returned string are enumerated in the code comments. The server used is in the last line of code,
conn:connect(13,'utcnist2.colorado.edu')
I use the University of Colorado servers because they are never busy. For other servers, http://tf.nist.gov/tf-cgi/servers.cgi

Output from LuaLoader:
======================
dofile("daytime.lua")
daytime.lua started
>
received
57054 15-02-01 20:40:40 00 0 0 832.3 UTC(NIST) *
extracted Date and Time
2015-02-01 20:40:40
Disconnected
End output
==========
Incidentally, I really like LuaLoader ... thanks for making it available.

Code: Select all-- File name: daytime.lua
-- tested on NodeMCU 0.9.5 build 20150126
-- adapted from httpget.lua (example from LuaLoader Quick Start guide)
-- Queries NIST DayTime server for a date time string
-- Fixed format returned:
-- JJJJJ YR-MO-DA HH:MM:SS TT L H msADV UTC(NIST) OTM.
-- Ex: 57053 15-01-31 20:14:43 00 0 0 703.0 UTC(NIST) *
--   JJJJJ - the modified Julian Date ( MJD is the Julian Date              minus 2400000.5)
--   YR-MO-DA - the Date
--   HH:MM:SS - the Time
--   TT -  USA is on Standard Time (ST) or (DST) : 00 (ST)
--   L - Leap second at the end of the month (0: no, 1: +1, 2: -1)
--   H - Health of the server (0: healthy, >0: errors)
--   msADV - time code advanced in ms for network delays
--   UTC(NIST) - the originator of this time code
--   OTM - on-time marker : * (time correct on arival)

print('DayTime.lua started')
conn = nil
conn=net.createConnection(net.TCP, 0)
-- show the server returned payload
conn:on("receive", function(conn, payload)
                       print('\nreceived')
                       print(payload)
                       print('extracted Date and Time')
                       print('20'..string.sub(payload,8,14)..
                              string.sub(payload,15,24))
                   end)
-- show disconnection
conn:on("disconnection", function(conn, payload) print('\nDisconnected') end)
--connect                                   
conn:connect(13,'utcnist2.colorado.edu')


User avatar
By alonewolfx2
#8792 its very easy and very good. i have one question. how can i manipulate time to gmt+2. i tried this and its crashing.
Code: Select allprint('20'..string.sub(payload,8,14)..
                              (string.sub(payload,15,18)+2))

or
Code: Select alltime1=string.sub(payload,15,18)
print(tonumber(time1)

but i am giving this error message.
PANIC: unprotected error in call to Lua API (daytime.lua:28: attempt to perform arithmetic on a string value)
PANIC: unprotected error in call to Lua API (attempt to concatenate a nil value)
PANIC: unprotected error in call to Lua API (attempt to call a string value)


Sorry this question maybe silly but i am new in lua :)
User avatar
By bax
#8794 @Byalonewolfx2,
Well, I am also a LUA beginner. What you need to do is

(1) extract the hours
(2) convert to a number
(3) add 2 modulo 24 (or if add 2 > 24, subtract 24)
(4) convert the modulo result back to a string
(5) insert the string back in back into the hour position

UTC is ok for my purposes. So, I did not bother with this. I think
you can use coercion for the string/number stuff,

http://lua-users.org/wiki/StringsTutorial