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

User avatar
By JeremiahLandi
#35067 Hello,

I am working on expanding the web server functionality that was provided on a prior thread (viewtopic.php?f=19&t=990&start=0). What I am wondering is how to increase the number of LEDs that can be controlled by the website. Can someone lend a suggestion or approach?

My idea is to add an id="led#" to the current submit buttons, so...:

Code: Select allconn:send('<input type="submit" name="pwmi" value="10" id="led5"/>')


Then figure a way to parse it in the following:

Code: Select allsubmittedValue={string.find(payload,"pwmi=")}
         --If POST value exist, set LED power
         if submittedValue[2]~=nil then
            print("Command received: " .. payload)
            ctrlpower(kdesi,payload)
            sendPage(conn)


Is this over complicated it? The idea is to create this web server so that other users (including me) would be able to scale the number of LEDs from one to a large number serving all needs. I think that this could go a long way allowing everyday users to just stand up the ESP8266 LUA and go.

Alternatively, I have noticed the conn:send command tends to be very slow to the buf = buf.. If someone would alternativly use that command let me know (http://randomnerdtutorials.com/esp8266-web-server/).

Here is the full code as it stands without my modifications to the HTML or logic around it.

Code: Select all--init.lua
    -- Code sample from https://www.domoticz.com/wiki/ESP8266_WiFi_module
    -- Only supports WPA2 by any 802.11g or better device
    wifi.sta.config("YOUR SSID","YOUR KEY")
    wifi.sta.connect()
    tmr.alarm(1, 1000, 1, function()
     if wifi.sta.getip()== nil then
     print("IP unavaiable, Waiting...")
    else
     tmr.stop(1)
    print("ESP8266 mode is: " .. wifi.getmode())
    print("The module MAC address is: " .. wifi.ap.getmac())
    print("Config done, IP is "..wifi.sta.getip())
    end
    end)

-- Select right IO index !! Here is settings for GPIO2 (Lua build 20141219)
outpinOne=4
outpinTwo=0

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)