Simple wifi web server for GPIO output control.
Posted: Mon Dec 22, 2014 2:01 pm
1. Flash NodeMcu 0.9.2 build 20141219 to ESP module.
2. Load program srv41.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. Test IP address - wifi.sta.getip()
6. Run program srv41.lua - dofile(srv41.lua)
7. Test it with your browser.
I do not know why, but this server does not work with the latest version NodeMcu 0.9.3 build 20141222.
2. Load program srv41.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. Test IP address - wifi.sta.getip()
6. Run program srv41.lua - dofile(srv41.lua)
7. Test it with your browser.
I do not know why, but this server does not work with the latest version NodeMcu 0.9.3 build 20141222.
Code: Select all
--wifi.setmode(wifi.STATION)
--wifi.sta.config("yourSSID","yourPassword")
outpin=4 -- Select right IO index !! Here is settings for GPIO2 (Lua build 20141219)
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)
function ctrlpower()
pwm.close(outpin)
gpio.mode(outpin,gpio.OUTPUT)
dotaz=string.sub(payload,kdesi[2]+1,#payload)
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
--parse position POST value from header
kdesi={string.find(payload,"pwmi=")}
--If POST value exist, set LED power
if kdesi[2]~=nil then ctrlpower()end
conn:send('HTTP/1.1 200 OK\n\n')
conn:send('<!DOCTYPE HTML>\n')
conn:send('<html>\n')
conn:send('<head><meta content="text/html; charset=utf-8">\n')
conn:send('<title>ESP8266</title></head>\n')
conn:send('<body><h1>Sample GPIO output control</h1>\n')
conn:send('<h3>For Lua NodeMcu 0.9.2 build 20141219 only !!!</h3>\n')
conn:send('<h3>Tested with IE 11. and Chrome 39.</h3>\n')
conn:send('<IMG SRC="http://esp8266.fancon.cz/common/led.gif" WIDTH="150" HEIGHT="101" BORDER="1"><br><br>\n')
conn:send('<form action="" method="POST">\n')
conn:send('<input type="submit" name="pwmi" value="OFF">\n')
conn:send('<input type="submit" name="pwmi" value="10">\n')
conn:send('<input type="submit" name="pwmi" value="20">\n')
conn:send('<input type="submit" name="pwmi" value="30">\n')
conn:send('<input type="submit" name="pwmi" value="40">\n')
conn:send('<input type="submit" name="pwmi" value="50">\n')
conn:send('<input type="submit" name="pwmi" value="60">\n')
conn:send('<input type="submit" name="pwmi" value="70">\n')
conn:send('<input type="submit" name="pwmi" value="80">\n')
conn:send('<input type="submit" name="pwmi" value="90">\n')
conn:send('<input type="submit" name="pwmi" value="ON"> % of power<br><br>\n')
conn:send('<input type="submit" name="pwmi" value="FLC"> HW blinker</form>\n')
conn:send('</body></html>\n')
conn:on("sent",function(conn) conn:close() end)
end)
end)