As the title says... Chat on...

User avatar
By Paw
#18084 Newbie question :
Hello all !
I'm just amazed what great option have esp but... can someone help with small project:
I wish to make some extension control panel for me it is web page 6/8 buttons and is able to read some variable from uart and print it out on page.
So i take my eye on simple example web control 2 led... and example that is able to read data from usart http://www.esp8266.com/viewtopic.php?f=19&t=1975 but i can't understand
How this code send data to html code ?
Code: Select allconn: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
        buf = buf.."<h1> GPIO Control </h1>";
        buf = buf.."<p>GPIO0 <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        buf = buf.."<h1> foo</h1>";
        local _on,_off = "",""
        if(_GET.pin == "ON1")then
              print ("LED1_ON")
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "OFF1")then
              print ("LED1_OFF")
              gpio.write(led1, gpio.LOW);
        elseif(_GET.pin == "ON2")then
              print ("LED2_ON")
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "OFF2")then
             print ("LED2_OFF")
              gpio.write(led2, gpio.LOW);
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)


All html is treated as variable called buff ? Because i can't see it.
I think i have to only put those buttons to code somewhere between
Code: Select allconn:on("receive",function(conn,payload)
   --print(payload) -- for debugging only
   strstr={string.find(payload,"GET / HTTP/1.1")}
   if(strstr[1]~=nil)then

and
Code: Select all   --generate the measurement string from arr list of measurements
   --create an empty string
   values_string="Measurements:"
   for i,v in ipairs(arr) do
      --concatenate measurements into a string
      values_string=values_string .. v .. " "
   end
   --generates HTML web site, autorefreshes at 1s interval, prints measurements
   conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\
   <!DOCTYPE HTML>\
   <html><head><meta content="text/html;charset=utf-8"><title>ESP8266 Arduino analog measurements</title><meta http-equiv="refresh" content="1" /></head>\
   <body bgcolor="#ffe4c4"><h2>'..values_string..'</h2>') end
   conn:on("sent",function(conn) conn:close() end)


but my understanding of LUA for now is too weak.
\