-->
Page 1 of 3

[NodeMcu] Lua script examples

PostPosted: Sat Nov 22, 2014 12:35 pm
by zeroday
I will collect examples from the community and post them here.
Any examples code are welcomed.

Re: [NodeMcu] Lua script examples

PostPosted: Sat Nov 22, 2014 1:01 pm
by zeroday
A http rest api for GPIO -- from @gorec2005
gorec2005 wrote:Hi!

one imho - if you use windows for download lua to the esp8266 - try YAT(from sf.net) it can insert delay between send string (500ms enought for me)
and second - this work for me:

Code: Select alls=net.createServer(net.TCP)
 s:listen(80,function(c)
 c:on("receive",function(c,pl)
  for v,i in pairs{2,8,9} do
   gpio.mode(i,gpio.OUTPUT)
   c:send("\ngpio("..i.."):"..gpio.read(i))
   if string.find(pl,"gpio"..i.."=0") then gpio.write(i,0) end
   if string.find(pl,"gpio"..i.."=1") then gpio.write(i,1) end
   c:send("\nnew_gpio("..i.."):"..gpio.read(i))
  end
  c:send("\nTMR:"..tmr.now().." MEM:"..node.heap())
 c:on("sent",function(c) c:close() end)
 end)
end)


with this you can post GET query like this:

http://192.168.x.x/gpio8=1
or
http://192.168.x.x/gpio2=1&gpio8=0
or
http://192.168.x.x/gpio9=1&gpio2=1
or
http://192.168.x.x/gpio9=1&gpio2=1&gpio8=1

Re: [NodeMcu] Lua script examples

PostPosted: Sat Nov 22, 2014 1:05 pm
by zeroday
A interface with an Arduino -- from @Erni
Erni wrote:First a thank to zeroday for this lua implementation

I have the webserver running without problems.
I am planning to put some sensor reading ,and I found this easy way
to interface with an Arduino, or actually a ATtiny85.

The t85 simply serially prints the value of t, which update the value on the webpage:
Maybe others will find it useful, or there is a better way to do this


Code: Select allif(t==nil) then
t=12
end
srv=net.createServer(net.TCP) srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload) print(payload)
    conn:send("HTTP/1.1 200 OK\n\n")
    conn:send("<html><body>")
    conn:send("<h1>Erni's ESP8266</h1><BR>")
    conn:send("TEMP : " .. t .. "<BR>")
    conn:send("NODE.HEAP : " .. node.heap() .. "<BR>")
    conn:send("TMR.NOW : " .. tmr.now() .. "<BR>")
    conn:send("</html></body>")
    conn:on("sent",function(conn) conn:close() end)
  end)
end)


Re: [NodeMcu] Lua script examples

PostPosted: Sat Nov 22, 2014 1:08 pm
by zeroday
A counter up http server -- from @scargill
scargill wrote:Here is the revised code with the heap display coming up on the remote browser... the recovery makes this unusable - typical application might be for a mobile phone to poll the status of something - would not last a minute..... I hope you can fix because this is SO NEAR to being useful.

Code: Select allmycounter=0
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
   if string.find(payload,"?myarg=") then
   mycounter=mycounter+1
    m="<br/>Value= " .. string.sub(payload,string.find(payload,"?myarg=")+7,string.find(payload,"HTTP")-2)
  else
    m=""
 end
 conn:send("<h1> Hello, this is Pete's web page.</h1>How are you today.<br/> Count=" .. mycounter .. m .. "Heap=".. node.heap())
 end)
conn:on("sent",function(conn) conn:close() end)
end)