NodeMCU ESP8266 Wifi Web Server

-- Node MCU Wifi Web Server - Nitin William 17/12/15

cfg={}

cfg.ssid="YourSSID"

cfg.pwd="YourPassword"

wifi.ap.config(cfg)

wifi.setmode(wifi.SOFTAP)

relay1 = 1

relay2 = 2

relay3 = 4

gpio.mode(relay1, gpio.OUTPUT)

gpio.mode(relay2, gpio.OUTPUT)

gpio.mode(relay3, gpio.OUTPUT)

gpio.write(relay1,gpio.LOW)

gpio.write(relay2,gpio.LOW)

gpio.write(relay3,gpio.LOW)

mysrv=net.createServer(net.TCP)

mysrv:listen(80,function(conn)

    conn:on("receive", function(conn,pload)

    print(pload)

    _,j=string.find(pload,"/?pin=")

    if j ~= nil then

        action=string.sub(pload,j+1,j+3)

        print (action)

        if action=="TR1" then gpio.write(relay1,gpio.HIGH)

        elseif action=="FL1" then gpio.write(relay1,gpio.LOW)

        elseif action=="TR2" then gpio.write(relay2,gpio.HIGH)

        elseif action=="FL2" then gpio.write(relay2,gpio.LOW)

        elseif action=="TR3" then gpio.write(relay3,gpio.HIGH)

        elseif action=="FL3" then gpio.write(relay3,gpio.LOW)

        end

    end

    volts=adc.read(0)

    conn:send('HTTP/1.1 200 OK\n\n')

    conn:send('<!DOCTYPE HTML>\n')

    conn:send('<html><meta http-equiv=refresh content=5>\n')

    conn:send('<body bgcolor="yellow"><center><head><meta  content="text/html; charset=utf-8">\n')

    conn:send('<title>NodeMCU - ESP8266</title></head>\n')

    conn:send('<body><h1>NodeMCU Webserver</h1>\n')

    conn:send('<h1>VU3GAO - Nitin William</h1>\n')

    conn:send('<hr/><p> Click the Buttons to turn On and OFF <p/><hr/>\n')

    conn:send('<hr/><p>ADC Reading :'..volts..'<p/><hr/>')

    conn:send('<input type=button value="Main Light ON" style=height:70px;width:300px onmousedown=location.href="/?pin=TR1">')

    conn:send('<input type=button value="Main Light OFF" style=height:70px;width:300px onmousedown=location.href="/?pin=FL1"><br/><br/>')

    conn:send('<input type=button value="Chandelier ON" style=height:70px;width:300px onmousedown=location.href="/?pin=TR2">')

    conn:send('<input type=button value="Chandelier OFF" style=height:70px;width:300px onmousedown=location.href="/?pin=FL2"><br/><br/>')

    conn:send('<input type=button value="Ceiling Fan ON" style=height:70px;width:300px onmousedown=location.href="/?pin=TR3">')

    conn:send('<input type=button value="Ceiling Fan OFF" style=height:70px;width:300px onmousedown=location.href="/?pin=FL3"><br/><br/>')

    conn:send('</body></html>')

    end)

    conn:on("sent",function(conn) conn:close() end)

    collectgarbage();

end)

 

           

   

Home