Post your best Lua script examples here

User avatar
By phatjoe
#31223 Quick question.... So with this bit of code can I input any GPIO Pin number? i.e. 5 for example. Or is it limited to only using 2, 8 and 9? If its limited to 2, 8 and 9 how can I open up its compatibility to any GPIO?

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)
User avatar
By forlotto
#31291
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)


If you look where it says:
Code: Select allfor v,i in pairs{2,8,9} do
   gpio.mode(i,gpio.OUTPUT)


For each step in that code, i gets an index which is specified I believe as (2,8,9)
And then set as an output .

In order to use GPIO we must first set it as an output or an input. So in this case only GPIO (2,8,9) is being used I believe.

I am not 100% sure on this as I am new but this is how I interpret the code so if you needed to use other GPIO's they would indeed have to be specified.

So if you wanted to use all GPIO's you would need to specify this in your index by adding the remaining GPIO's...

The next portion of the code----->
Code: Select allc:send("\ngpio("..i.."):"..gpio.read(i))

Sends a string like so \ngpio7:0 if it is off or \ngpio7:1 if it were off or it will read and send the status of gpio's within the index and display or send the information in this manner.

The next line of code--->
Code: Select all if string.find(pl,"gpio"..i.."=0") then gpio.write(i,0) end

Will check the string of data payload, for gpio and is in the index and a value of 0 then it will write to that index using gpio write the value of 0 or in english it will turn off.

The next line of code---->
Code: Select all if string.find(pl,"gpio"..i.."=1") then gpio.write(i,1) end

Will check the string of data payload, for gpio and is in the index and a value of 1 then it will write to that index using gpio write the value of 1 or in english it will turn on.

The next line of code----->
Code: Select allc:send("\nnew_gpio("..i.."):"..gpio.read(i))

Will send the new value of the GPIO like so \nnew_gpio7:0 if off or \nnew_gpio7:1 if on

(Keep in mind I am only using 7 as an example)

Code: Select allc:send("\nTMR:"..tmr.now().." MEM:"..node.heap())
This is executing the nodeheap command to clear memory I believe although I am not 100% on this still need to do reading on my own.

Code: Select all c:on("sent",function(c) c:close() end)
This I believe closes the connection.

I am in the same boat with you on trying to understand stuff but little by little I am starting to understand it.

BTW this is an excellent bit of code who ever wrote it a very powerful yet compact code to use as a handler I thank who ever wrote it!!!

This code is not good however if you are just trying to push a button and turn stuff off and on you are better off using the buff:buff to create an HTML page that allows you to use a simple on or off button.

If anything here posted is wrong please correct me I am attempting to help with a somewhat limited knowledge of what I am doing.

Wishing for the best here.