You are on page 1of 1

POST_54.lua — Printed on 1.1.

2015, 20:42:02 — Page 1


1
2 -- Demo server v.20150101 for POST method - tested with NodeMcu 0.9.4 build
20141230 and
3 -- Nokia XPRESS 5, FireFox 34, IE 11, Chrome 39, ANDROID 4.2.2 default browser
4 -- 1. Flash NodeMcu 0.9.2 build 20141219 or later to ESP module.
5 -- 2. Load program POST_54.lua to ESP with LuaLoader
6 -- 3. HW reset module
7 -- 4. Login module to your AP -
wifi.setmode(wifi.STATION),wifi.sta.config("yourSSID","yourPASSWORD")
8 -- 5. Run program POST_54.lua - dofile(POST_54.lua)
9 -- 6. Test IP address - wifi.sta.getip()
10 -- 7. Test it with your browser and true IP address of module.
11 -- 8. Keep happy new year 2015!
12 --------------------------------------------------------------------------------
13 outpin=4 -- Select right IO index !! Here is settings for GPIO2 (Lua build 20141219
or later)
14 -------------------------------------------------------------------------------
15 setpower=0
16 gpio.mode(outpin,gpio.OUTPUT)
17 gpio.write(outpin,gpio.LOW)
18 srv=net.createServer(net.TCP) srv:listen(80,function(conn)
19 conn:on("receive",function(conn,payload)
20 -- print(payload) -- for debugging only
21 function ctrlpower() -- Settings ON/OFF or PWM for GPIO
22 pwm.close(outpin)
23 gpio.mode(outpin,gpio.OUTPUT)
24 value=string.sub(payload,strstr[2],#payload)
25 if value=="ON" then gpio.write(outpin,gpio.HIGH)setpower=100 return end
26 if value=="OFF" then gpio.write(outpin,gpio.LOW)setpower=0 return end
27 if value=="FLC" then pwm.setup(outpin,2,100)pwm.start(outpin)setpower=10 return end
28 if value~=nil and tonumber(value)>0 and tonumber(value)<100 then setpower=tonumber(
value) else return end
29 pwm.setup(outpin,1000,setpower*10)
30 pwm.start(outpin)
31 end ----------------------------------------------------
32 --parse a position of GET variable/value from header
33 strstr={string.find(payload,"\r\n\r\npwmi=")}
34 if(strstr[1]~=nil) then strstr[1]=strstr[1]+4 strstr[2]=strstr[1]+5 end
35 if payload~=nil and string.sub(payload,1,5)=="pwmi=" then strstr[1]=1 strstr[2]=6
end
36 --If GET value exist, set LED power
37 if (strstr[2]~=nil)then ctrlpower()end
38
39 -- generates HTML web site
40 conn:send('HTTP/1.1 200 OK\r\n Content-type: text/html\r\n\r\n\
41 <!DOCTYPE HTML>\
42 <html>\
43 <head><meta content="text/html; charset=utf-8">\
44 <title>ESP8266</title></head>\
45 <body><h3>Sample GPIO output control</h3>\
46 <form action="" method="post">\
47 <input type="submit" name="pwmi" value="OFF">\
48 <input type="submit" name="pwmi" value="10">\
49 <input type="submit" name="pwmi" value="20">\
50 <input type="submit" name="pwmi" value="30">\
51 <input type="submit" name="pwmi" value="40">\
52 <input type="submit" name="pwmi" value="50">\
53 <input type="submit" name="pwmi" value="60">\
54 <input type="submit" name="pwmi" value="70">\
55 <input type="submit" name="pwmi" value="80">\
56 <input type="submit" name="pwmi" value="90">\
57 <input type="submit" name="pwmi" value="ON">\
58 <font color="red"><big><big>'..' '..setpower..' '..'% of power</big></big>\
59 </font><font color="black"><br></font><br>\
60 <input type="submit" name="pwmi" value="FLC"> HW blinker</form><br><br>\
61 <IMG SRC="http://esp8266.fancon.cz/common/led.gif" WIDTH="150" HEIGHT="101"
BORDER="1">\
62 </body></html>')
63 conn:on("sent",function(conn) conn:close() end)
64 end)
65 end)
66
67
68

C:\DATA\FOR_BACKUP_!!!\COM\ESP8266\LUA\Demo\Projects\P1_web_server_post_out\POST_54.lua — File date: 1.1.2015 — File time: 19:58:35