Post your best Lua script examples here

User avatar
By FritzFritz
#64207 In case someone ist interested in a Quick+Dirty script for writing log-records to a log-file or reading from a file residing on a FTP-server.
When an esp8266 is powered by battery it will be the most time in deep sleep. An FTP-server (e.g. a FritzBox) can be used as a DropBox where the esp8266 can post to or check for config changes (e.g. changing deepsleep cycletime,...). This script writes a log-record to a file or read a line from a file at server's side.
The script is very basic and can be enhanced ad libidum :) (e.g. writing/reading files )


if ftp_cont ~= nil then ftp_cont = nil end
if ftp_data ~= nil then ftp_data = nil end
ftp_cont=net.createConnection(net.TCP,0)
ftp_data=net.createConnection(net.TCP,0)

dict = {
[220] = function(c) ftp_cont:send("USER "..ftp_user.."\r\n") end,
[331] = function(c) ftp_cont:send("PASS "..ftp_psw.."\r\n") end,
[230] = function(c) ftp_cont:send("CWD "..ftp_dir.."\r\n") end,
[250] = function(c) ftp_cont:send("PASV\r\n") end,
[227] = function(c) str = string.sub(c, string.find(c,"%(")+1 , string.find(c,"%)")-1)
t = {}
for k, v in string.gmatch(str, "(%d+)") do table.insert(t,k) end
port = t[5]*2^8 + t[6]
ftp_data:connect(port,ftp_ip)
t=nil
str=nil
port=nil
end,
[200] = function(c) wr = "RETR "
if (ftp_wr == 1) then wr = "APPE " end
ftp_cont:send(wr .. ftp_file .. "\r\n")
wr = nil
end,
[150] = function(c) ftp_data:send(log_record) end,
[226] = function(c) ftp_cont:send("QUIT\r\n") end,
[221] = function(c) end,
["error"] = function(c) ftp_data = nil
if ftp_cont:getpeer() ~= nil then
ftp_cont:send("QUIT\r\n")
ftp_cont:close()
end
ftp_cont = nil
end,
["finished"] = function(c) if ftp_cont:getpeer() ~= nil then ftp_cont:close() end
ftp_data = nil
ftp_cont = nil
end,
}

-- a lot of callbacks -debug version- remove all 'print' when its running ok

ftp_cont:on("receive", function(sck1, c) print(c)
t = tonumber(string.sub(c,1,3))
if dict[t] then
dict[t](c)
else
dict["error"](c)
end end )

ftp_cont:on("connection", function(sck, c) print("received control connection message, error: ", c)
if c ~= nil then dict["error"](c) end end)

ftp_cont:on("sent", function(sck,c) print("received control sent message, error: ", c)
if c ~= nil then dict["error"](c) end end)

ftp_cont:on("disconnection", function(sck,c) print("received disconnection message, error: " , c)
if c ~= 0 then
dict["error"](c)
else
dict["finished"](c)
end end)

ftp_data:on("connection", function(sck,c) print("received data connection message, error: " , c)
if c ~= nil then
dict["error"](c)
else
ftp_cont:send("TYPE I\r\n")
end end)

ftp_data:on("sent", function(sck,c) print("received data sent message, error: " , c)
if c ~= nil then
dict["error"](c)
else
ftp_data:close()
end end)

ftp_data:on("receive", function(sck , c ) print("received on data: " , c)
read_line = c
end )

-- required credentials and other stuff

ftp_ip = "192.168.188.1" --> FTP-Server
ftp_port = 21 --> FTP-Server Port
ftp_user = "xxxxxxx" --> Account on this server
ftp_psw = "xxxxxxxx" --> pswd for this account
ftp_dir = "/memory-USB2-0-01/access" --> directory on the FTP-Server
ftp_file = "test.csv" --> log-file on FTP-Server
ftp_wr = 0 --> 0 = read , 1 = write
log_record = "hello world\r\n" --> log_record
--in case of writing the read line will be in variable read_line

-- triggering transfer
ftp_cont:connect(ftp_port,ftp_ip)