Post your best Lua script examples here

User avatar
By Erni
#3859 Ok,
Here is the example with dns lookup.
Thanks to gerardwr and scargill


Code: Select allfile.remove("client.lua")
file.remove("client.lua")
file.open("client.lua","w+")
host="www.ernstc.dk"
ipnr=0
file.writeline([[tmr.alarm(30000, 1, function()]])
file.writeline([[sk=net.createConnection(net.TCP, 0)]])
file.writeline([[sk:dns(host,function(conn,ip) ]])
file.writeline([[ipnr=ip]])
file.writeline([[end)]])
file.writeline([[conn=net.createConnection(net.TCP, 0) ]])
file.writeline([[    conn:on("receive", function(conn, payload) print(payload) end )]])
file.writeline([[    conn:connect(80,ipnr)]])
file.writeline([[    conn:send("GET /esp.php?tekst="..adc.read(0) .."&heap="..node.heap().." HTTP/1.1\r\nHost: "..host.."\r\n"]])
file.writeline([[        .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")]])
file.writeline([[end)]])
file.close()
User avatar
By gerardwr
#3866
Erni wrote:Ok,
Here is the example with dns lookup.


Nice work!

May I suggest to consider changing the title on your opening post into something that better describes the Example. That will help me find it later when I need it ;)

Code: Select allExample of a webclient with DNS lookup
User avatar
By Erni
#3873 Yes good idea with a more descriptive title ;)

Just tried it with pushingbox (sending a email.), works fine
The only thing that needs change for others is devid=vxxxxxxx

Code: Select allfile.remove("pushing.lua")
file.open("pushing.lua","w+")
host="api.pushingbox.com"
ipnr=0
file.writeline([[tmr.alarm(30000, 1, function()]])
file.writeline([[sk=net.createConnection(net.TCP, 0)]])
file.writeline([[sk:dns(host,function(conn,ip) ]])
file.writeline([[ipnr=ip]])
file.writeline([[end)]])
file.writeline([[conn=net.createConnection(net.TCP, 0) ]])
file.writeline([[    conn:on("receive", function(conn, payload) print(payload) end )]])
file.writeline([[    conn:connect(80,ipnr)]])
file.writeline([[    conn:send("GET /pushingbox?devid=vxxxxxxx HTTP/1.1\r\nHost: "..host.."\r\n"]])
file.writeline([[        .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")]])
file.writeline([[end)]])
file.close()