Post your best Lua script examples here

User avatar
By forlotto
#30622 This code was originally submitted by Zeroday to give him the proper credits...

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)


It is quoted that it will work by using Get like so...

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


Now if I wanted to:
Connect to wireless as well so I could control the node remotely as well
add a password in order for the get request to be processed
add the ability to read all GPIO pin status
add the ability to read 1 GPIO pin status

What would be the most effective way to do this without causing memory faults and keeping the connection alive at all times. I would have to allow for all nodemcu pin control except gpio 0,2,15,16 I believe are the pins that may cause issue however I would like to still address the remaining pins. 1 3 4 5 9 10 12 13 14 which should leave me 9 pins that I could switch on and off safely without causing any conflict.

I do have a question as to why the user who made this code addresses gpio8 when according to the table both old and new GPIO8 does not even exist?

Code: Select all cfg =
  {
    ip="192.168.0.111",
    netmask="255.255.255.0",
    gateway="192.168.0.1"
  }
  wifi.sta.setip(cfg)
wifi.sta.config("myssid", "password")
--I believe the above should handle connecting to my access point when in range ... ?
p=mypassword //<----- is this the proper place and way to declare a global variable?
s=net.createServer(net.TCP)
 s:listen(80,function(c)
 c:on("receive",function(c,pl)   --I assume c in (c,pl) is
  for v,i in pairs{1,3,4,5,9,10,12,13,14} do  --I think this means for variable i in pairs
   gpio.mode(i,gpio.OUTPUT)  --I think this sets all of the above gpio's as outputs
   c:send("\ngpio("..i.."):"..gpio.read(i))   --Not sure what this does connects and sends gpio status maybe?
   if string.find(pl,"gpio"..i.."=0") then gpio.write(i,0) end  --This string finds the gpio(i) and writes a 0 or off for that gpio (i) if the =0
   if string.find(pl,"gpio"..i.."=1") then gpio.write(i,1) end  --This string finds the gpio(i) and writes a 1 or on for that gpio (i) if the =1
   if string.find(pl,"gpio"..i.."=?") then gpio.read(i)  end  --would this reading function be needed being that it reads below this is something I added as to how I think we would read pin status?
   c:send("\nnew_gpio("..i.."):"..gpio.read(i))  -- Does this send the value of pins read?
  end
  c:send("\nTMR:"..tmr.now().." MEM:"..node.heap())  --dunno what the remaining code does exactly...??
 c:on("sent",function(c) c:close() end)


I will note the main objective is easy control from a remote website for all via http request to eliminate the need for any third party stuff like brokers etc... Ease of use while being fairly secure.

Yes I know very beginner but I am trying to understand and comment as much as I can to attempt to get a better grasp on how to accomplish this task and in doing so maybe I will help a few others as well along the way while getting help possibly or by making these notes to myself and figuring things out as time provides.

Thank you for taking the time to read this and respond in advance I will continue to read and see what I can come up with as spare time provides.
User avatar
By forlotto
#30737 Deleted post as it may cause confusion.