-->
Page 1 of 1

Trying to understand some Lua code...

PostPosted: Fri Dec 04, 2015 5:36 pm
by Trickuncle
First, apologies to the person that I borrowed this code from; I can't remember who it was to give them credit.
It turns on the red and/or blue leds on the ESP8266 board and that works fine. I added some code to display a slider and display the value the user sets it to. This seems to work fine.

There is a block commented out by me as a test (lines 14 to 25). Commenting this out does not seem to affect the operation so I'm guessing it does something else but cannot figure out just what it is there for.

Can anyone give me a clue on this? Thanks!

Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("xxx","zzz")

print(wifi.sta.getip())
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP,3600)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
--        local buf = "";

--[[     
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end
]]

        conn:send('<h1><center>ESP8266 Web Server </center></h1>')
        conn:send('<p><center>GPIO0 <a href=\"?pin=ON1\"><button style="font-size:300%;">ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button style="font-size:300%;">OFF</button></center></a></p>')
        conn:send('<p><center>GPIO2 <a href=\"?pin=ON2\"><button style="font-size:300%;">ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button style="font-size:300%;">OFF</button></center></a></p>')


--        local _on,_off = "",""
        if(_GET.pin == "OFF1")then
            gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "ON1")then
            gpio.write(led1, gpio.LOW);
        elseif(_GET.pin == "OFF2")then
            gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "ON2")then
            gpio.write(led2, gpio.LOW);
        end
       
        conn:send('<center><font color=red><b>OFF</b></font>')
        conn:send('<input style="width:550px; height:50px" type="range" name="cmd" id="cmd" value="" min=1 max=128 step=1 onchange="myFunction()"/>')
        conn:send('<font color=red><b>ON</b></font></center></form>')
      conn:send('<center><p id="demo" /p></center>')

      conn:send('<script>')
      conn:send('function myFunction() {')
      conn:send('    var x = document.getElementById("cmd").value;')
      conn:send('    document.getElementById("demo").innerHTML = "Slider value is : " + x + " units";')
      conn:send('}</script>')
      
        client:close();
        collectgarbage();
    end)
end)

Re: Trying to understand some Lua code...

PostPosted: Mon Dec 07, 2015 7:55 am
by gjbfl2020
The commented out lines parse the incoming string to the http server to show what parameters were included in the server query.

Re: Trying to understand some Lua code...

PostPosted: Mon Dec 07, 2015 12:16 pm
by Trickuncle
@gjbfl2020 - thanks! I should put it back in and learn how to use it. I have my program nearly working 100% except for some apparent client-server interactions that I don't understand in the slightest. But I'm here to learn.

The pushbuttons work on any browser but the slider action is different on every browser I have tried with the worst being Chrome. On Chrome, the slider data shows up in the uri but never seems to get to the ESP8266. Still diggin'...