Post your best Lua script examples here

User avatar
By Amelsfort
#7898 First of all thanks for the example! It was my first program to run on the ESP8266.

The issue with the duplicate page appeared to be caused by the browser making two separate posts (one without the form data and one with data) or a single posts which was split on the ESP8266, I'm not sure. So the code handling the requests was executed twice and therefore the page was sent twice as well.

I have updated the code (and split the functions a bit) to solve this:
Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("<SSID>","<code>")
outpin=4 -- Select right IO index !! Here is settings for GPIO2 (Lua build 20141219)

gpio.mode(outpin,gpio.OUTPUT)
gpio.write(outpin,gpio.LOW)
status = 'OFF'

function ctrlpower(kdesi,payload)
   pwm.close(outpin)
   gpio.mode(outpin,gpio.OUTPUT)
   dotaz=string.sub(payload,kdesi[2]+1,#payload)
   status = dotaz
   if dotaz=="ON"  then gpio.write(outpin,gpio.HIGH) return end
   if dotaz=="OFF" then gpio.write(outpin,gpio.LOW) return end
   if dotaz=="FLC" then pwm.setup(outpin,2,512)pwm.start(outpin) return end
   pwm.setup(outpin,1000,dotaz*10)
   pwm.start(outpin)
end

function sendPage(conn)
   conn:send('HTTP/1.1 200 OK\n\n')
   conn:send('<!DOCTYPE HTML>')
   conn:send('<html>')
   conn:send('<head><meta content="text/html; charset=utf-8"><style>input{width: 100px; height: 100px;}</style>')
   conn:send('<title>ESP8266</title></head>')
   conn:send('<body><h1>LED Controller</h1>')
   conn:send('Status: <b>')
   if (status == "ON") then      conn:send('ON')
   elseif (status == "OFF") then    conn:send('OFF')
   elseif (status == "FLC") then    conn:send('Flickering')
   else                      
      conn:send(status)
      conn:send('%')
   end
   conn:send('</b><br /><br />')
   conn:send('<form action="/" method="POST">')
   conn:send('<input type="submit" name="pwmi" value="OFF"/>')
   conn:send('<input type="submit" name="pwmi" value="ON"/><br /><br />')
   conn:send('<input type="submit" name="pwmi" value="10"/>')
   conn:send('<input type="submit" name="pwmi" value="20"/>')
   conn:send('<input type="submit" name="pwmi" value="30"/>')
   conn:send('<input type="submit" name="pwmi" value="40"/>')
   conn:send('<input type="submit" name="pwmi" value="50"/>')
   conn:send('<input type="submit" name="pwmi" value="60"/>')
   conn:send('<input type="submit" name="pwmi" value="70"/>')
   conn:send('<input type="submit" name="pwmi" value="80"/>')
   conn:send('<input type="submit" name="pwmi" value="90"/> % of power<br /><br />')
   conn:send('<input type="submit" name="pwmi" value="FLC"/> HW blinker</form>')
   conn:send('</body></html>')
end

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
   conn:on("receive", function(conn,payload)
      --next row is for debugging output only
      --print(payload)
      if (string.find(payload, "GET / HTTP/1.1") ~= nil) then
         print("GET received")
         sendPage(conn)
      else
         kdesi={string.find(payload,"pwmi=")}
         --If POST value exist, set LED power
         if kdesi[2]~=nil then
            print("Command received: " .. payload)
            ctrlpower(kdesi,payload)
            sendPage(conn)
         end
      end
   end)
   conn:on("sent", function(conn)
      conn:close()
      print("Connection closed")
   end)
end)


This code works fine now and also shows the current status.
User avatar
By matt6ft9
#8127 In the above example, I'm having a little hard time with how the payload is handled. Near the bottom, we have this:
Code: Select all             kdesi={string.find(payload,"pwmi=")}   --find the location of "pwmi= in the payload

This parts makes sense. Two lines down from that, we have this:
Code: Select all                if kdesi[2]~=nil then      --??

Why is this needed? I see that it is checking to see something is in kdesi, but why the second element?

Also, if I print "kdesi" in the function "ctrlpower", I get something printed like "table: 3fff7280" why?
BTW, this thread has been a huge help.