Post your best Lua script examples here

User avatar
By Amelsfort
#8807 That's because of the way the find-function works. According to the documentation:
"Find the first occurrence of the pattern in the string passed. If an instance of the pattern is found a pair of values representing the start and end of the string is returned. If the pattern cannot be found nil is returned."

So, 'kdesi' contains the start and end of the string 'pwmi=' in the payload. Since we need the value of 'pwmi' (and the fact that arrays are 1-based in Lua) we need to have the end. That's where we will find the value.
.
User avatar
By Dileep
#10589 Hello
I am using Esp-12 module It is given to the relays to control appliances(like bulb).when it is power on it is working. but if power is gone then when power comes the module is getting strucked. and then webpage is not showing. what is the problem can any one give me a reply.......
User avatar
By GengusKahn
#10802 Been messing with your code.....

Code: Select all-- Demo server v.20150101 for POST method - tested with NodeMcu 0.9.4 build 20141230 and
-- Nokia XPRESS 5, FireFox 34, IE 11, Chrome 39, ANDROID 4.2.2 default browser
-- 1. Flash NodeMcu 0.9.2 build 20141219 or later to ESP module.
-- 2. Load program POST_54.lua to ESP with LuaLoader
-- 3. HW reset module
-- 4. Login module to your AP - wifi.setmode(wifi.STATION),wifi.sta.config("yourSSID","yourPASSWORD")
-- 5. Run program POST_54.lua - dofile(POST_54.lua)
-- 6. Test IP address - wifi.sta.getip()
-- 7. Test it with your browser and true  IP address of module.
-- 8. Keep happy new year 2015!
   --------------------------------------------------------------------------------
    outpin=4 -- Select right IO index !! Here is settings for GPIO2 (Lua build 20141219 or later)
    -------------------------------------------------------------------------------
   setpower=0
   gpio.mode(outpin,gpio.OUTPUT)
   gpio.write(outpin,gpio.LOW)
   srv=net.createServer(net.TCP) srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload)
   -- print(payload) -- for debugging only
    function ctrlpower() -- Settings ON/OFF or PWM for GPIO
    pwm.close(outpin)
    gpio.mode(outpin,gpio.OUTPUT)
   value=string.sub(payload,strstr[2],#payload)
   if value=="ON"  then gpio.write(outpin,gpio.HIGH)setpower=100 return end
    if value=="OFF" then gpio.write(outpin,gpio.LOW)setpower=0 return end
    if value=="FLC" then pwm.setup(outpin,2,100)pwm.start(outpin)setpower=10 return end
   if value~=nil and tonumber(value)>0 and tonumber(value)<100 then setpower=tonumber(value) else return end
   pwm.setup(outpin,1000,setpower*10)
    pwm.start(outpin)
    end ----------------------------------------------------
    --parse a position of POST variable/value from header
   strstr={string.find(payload,"\r\n\r\npwmi=")}
   if(strstr[1]~=nil) then strstr[1]=strstr[1]+4 strstr[2]=strstr[1]+5 end
   if payload~=nil and string.sub(payload,1,5)=="pwmi=" then strstr[1]=1 strstr[2]=6 end
    --If POST value exist, set LED power
    if (strstr[2]~=nil)then ctrlpower()end

   -- generates HTML web site
    conn:send('HTTP/1.1 200 OK\r\n Content-type: text/html\r\n\r\n\
   <!DOCTYPE HTML>\
      <meta charset="utf-8"><meta name="apple-mobile-web-app-capable" content="yes" /><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta name="viewport" content="width=device-width, user-scalable=no">\
  <title>Network LED Lighting Controller</title>\
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" />\
  <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>\
  <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>\
  <style> body, .ui-page { background: black; } .ui-body { padding-bottom: 1.5em; } div.ui-slider { width: 88%; } #pwmi, { display: block; margin: 10px; } #pwmi { background: #fff; } </style>\
</head>\
<body>\
  <div data-role="header" data-position="inline"><h1>WiFi LED</h1></div>\
    <div class="ui-body ui-body-a">\
     <form action="" method="post">\
      <input type="range" name="slider" id="pwmi" value="0" min="0" max="100"  />\
   </div>\
  <div data-role="header" data-position="inline"><h1>12v LED STRIP</h1></div>\
   </font><font color="black"><br></font><br>\
   <input type="submit" name="pwmi" value="FLC"> HW blinker</form><br><br>\
   <P ALIGN="CENTER">&nbsp;<IMG SRC="https://raw.githubusercontent.com/genguskahn/ESP8266-For-DUMMIES/master/SoC/DimmerDocs/organicw.gif" WIDTH="250" HEIGHT="151" BORDER="1"><P>\
</body></html>')
    conn:on("sent",function(conn) conn:close() end)
    end)
end)



Have been trying to embed YouTube Video, and use some jazzier look....
For info on Ajax, slider, form etc...start here
http://jquery.malsup.com/form/
The line for YouTube but this is why I gave up......to distracted....
<P ALIGN="CENTER">&nbsp;<iframe width="320" height="180" src="https://www.youtube.com/embed/sOhLD8HLfg4?feature=player_embedded" frameborder="0" allowfullscreen></iframe><P>\

The binding for the Ajax needs put back in, I lost a partition with all my bits, so see if anyone in this thread would like any of the bits that are left, it will load the webpage from ESP until you load the YouTube line there is something wrong with it.......