Post your best Lua script examples here

User avatar
By scargill
#4015 I saw Pushingbox - looks great - but I noted when attempting to register, this message.

Important notice: OpenID2 for Google accounts is going away on April 20, 2015. Learn more.

Is it work using given the above?

Pete.




Erni wrote: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()
User avatar
By scargill
#4017 Noo... Thank YOU!! Whatever I did wrong, you didn't - and I can now access my server.. which means I can get the time from it... all we need now is an RTC inside the ESP8266 that accepts the normal Linux-type time - and I'm all set!!!!

I made a little PHP page to simplify getting the time and lightup times etc rather than the overhead of getting it from an NTP server.. and for the first time using a variation of your code I got the results back!!!

Content-Type: text/html

44
{time=1417740275;timestr=00:44:35 05-12-2014;dawn=07:31;dusk=16:26;}
0

HOWEVER - most of the time it works. But if I sit there and every few seconds call this - even with the addition of nil'ing out the variables after use, eventually it will start to fall apart. Commands will be duplicated, or the board will simply reset. I thought it might be my terminal software so I tried Coolterm - same result. So I can call a web page that I know returns results every single time - and the code will return the results maybe 90% of the time... and eventually die. I can't put my finger on it but I still think there is something wrong in there.





Erni wrote: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 Erni
#4073
and the code will return the results maybe 90% of the time... and eventually die


I haven't noticed any instabaility, but just checking at the moment with one call pr. minut: I will let it run for some time to see if there should be any strange things happening.
http://www.ernstc.dk/sensor.php

I am using the lua firmwareversion from 2. dec.
One thing that I have noticed.
If I try to run the script without the looping, it won't find a IP number in the first try, but first in the second.
My first thought was taht the ip in the function

Code: Select allsk:dns(host,function(conn,ip)


was local to the function, therefore the strange code

Code: Select allipnr=ip


But that didn't help either, so it may be because I am a noob regarding lua.
User avatar
By GeoNomad
#4122
Erni wrote:it won't find a IP number in the first try, but first in the second.


I have the exact same problem. Thought it was local to my router or something...

The IP is never returned the first time, but is consistently returned the second time. May be a clue for zeroday.

I am resorting to caching the IP address and only checking it once in a while. It also makes the gets a slight bit faster (a lot faster on the first call).